Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Sept. 19, 2005 12:35:14

m.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

meta.OneToOneField() problemo


hi:

i just built a test app (with revision 645) using the 121 example found
at as i
am interested in using the 121 functionality in a project.

my model is an exact copy of the one in the docs - eg.
-----------------------------------------
class Place(meta.Model):
name = meta.CharField(maxlength=50)
address = meta.CharField(maxlength=80)

def __repr__(self):
return self.name

class META:
db_table="places"


class Restaurant(meta.Model):
place = meta.OneToOneField(Place)
serves_hot_dogs = meta.BooleanField()
serves_pizza = meta.BooleanField()

def __repr__(self):
return self.get_place().name

class META:
db_table="restaurants"
admin = meta.Admin()

-----------------------------------------
if i follow these steps in admin:
i. add a restaurant
ii. the ui gives you the option to add a place using the green '+' sign
next to the places dropdown.
iii. i add a place, which saves ok and redisplays in the dropdown
iv. i save a restaurant and get this error.

-----------------------------------------
D:\bin\dev\python\lib\site-packages\django\core\meta\__init__.py", line
1290, in _parse_lookup
_throw_bad_kwarg_error(kwarg)


D:\bin\dev\python\lib\site-packages\django\core\meta\__init__.py", line
1181, in _throw_bad_kwarg_error
raise TypeError, "got unexpected keyword argument '%s'" % kwarg

TypeError: got unexpected keyword argument place__pk'
------------------------------------------

i tried having a look at in winpdb but could not tell what was going on
if someone can give me an indication of what the problem is that would
be so lovely.

thanks
mark

Offline

#2 Sept. 19, 2005 15:21:37

Ian M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

meta.OneToOneField() problemo


Sorry Mark,

But I am going to add to your problem since I encountered something
this weekend that was similar. If I add a Waiter class to this example:

class Waiter(meta.Model):
name = meta.CharField(maxlength=50)
restaurant = meta.ForeignKey(Restaurant)

def __repr__(self):
return "%s waits at %s" % (self.name, self.get_restaurant())

But, I have a multitude of problems that I don't believe I had before
the recent model overhaul. Here are some example things I tried doing
with my Waiter class that I believe should work:

from django.models.places import places, restaurants, waiters

# Works Fine
p = places.Place(name='Demon Dogs', address='944 W. Fullerton')
p.save()

# Works Fine
r = restaurants.Restaurant(place=p, serves_hot_dogs=True,
serves_pizza=False)
r.save()

# This Fails:
w = r.add_waiter(name="John Doe")
# Traceback (most recent call last):
# File "test_places.py", line 13, in ?
# w = r.add_waiter(name="John Doe")
# File "C:\www\django\utils\functional.py", line 3, in _curried
# return args(*(args+moreargs), **dict(kwargs.items() +
morekwargs.items()))
# File "C:\www\django\core\meta\__init__.py", line 944, in
method_add_related
# obj = rel_mod.Klass(**init_kwargs)
# File "C:\www\django\utils\functional.py", line 3, in _curried
# return args(*(args+moreargs), **dict(kwargs.items() +
morekwargs.items()))
# File "C:\www\django\core\meta\__init__.py", line 755, in
method_init
# raise TypeError, "Invalid value: %r should be a %s instance,
not a %s" % (f.name, f.rel.to, type(rel_obj))
# TypeError: Invalid value: 'restaurant' should be a <Options for
restaurants> instance, not a <class 'django.models.places.Restaurant'>

# So Does This:
w = waiters.Waiter(restaurant=r, name="John Doe")
# Traceback (most recent call last):
# File "test_places.py", line 27, in ?
# w = waiters.Waiter(restaurant=r, name="John Doe")
# File "C:\www\django\utils\functional.py", line 3, in _curried
# return args(*(args+moreargs), **dict(kwargs.items() +
morekwargs.items()))
# File "C:\www\django\core\meta\__init__.py", line 755, in
method_init
# raise TypeError, "Invalid value: %r should be a %s instance,
not a %s" % (f.name, f.rel.to, type(rel_obj))
# TypeError: Invalid value: 'restaurant' should be a <Options for
restaurants> instance, not a <class 'django.models.places.Restaurant'>

# This Hack Works
w = waiters.Waiter(restaurant_id=r.place_id, name="John Doe")
w.save()

# But Even with a Correct Data Structure this Fails
print w.get_restaurant()
# Traceback (most recent call last):
# File "test_places.py", line 43, in ?
# print w.get_restaurant()
# File "C:\www\django\utils\functional.py", line 3, in _curried
# return args(*(args+moreargs), **dict(kwargs.items() +
morekwargs.items()))
# File "C:\www\django\core\meta\__init__.py", line 873, in
method_get_many_to_one
# retrieved_obj = mod.get_object(**{'%s__exact' %
field_with_rel.rel.field_name: val})
# File "C:\www\django\utils\functional.py", line 3, in _curried
# return args(*(args+moreargs), **dict(kwargs.items() +
morekwargs.items()))
# File "C:\www\django\core\meta\__init__.py", line 1083, in
function_get_object
# obj_list = function_get_list(opts, klass, **kwargs)
# File "C:\www\django\core\meta\__init__.py", line 1123, in
function_get_list
# return list(function_get_iterator(opts, klass, **kwargs))
# File "C:\www\django\core\meta\__init__.py", line 1105, in
function_get_iterator
# select, sql, params = function_get_sql_clause(opts, **kwargs)
# File "C:\www\django\core\meta\__init__.py", line 1302, in
function_get_sql_clause
# tables2, join_where2, where2, params2, _ =
_parse_lookup(kwargs.items(), opts)
# File "C:\www\django\core\meta\__init__.py", line 1231, in
_parse_lookup
# _throw_bad_kwarg_error(kwarg)
# File "C:\www\django\core\meta\__init__.py", line 1181, in
_throw_bad_kwarg_error
# raise TypeError, "got unexpected keyword argument '%s'" %
kwarg
# TypeError: got unexpected keyword argument 'place__exact'

Originally, I was going to ditch the use of OneToOne because I thought
I was simply using it incorrectly in my design... but after seeing
Mark's post and whipping up this example, I believe there is a bug.

I also hope my post clarifies not confuses Mark's post... because I
believe they are related issues.

regards,
-ian

Offline

#3 Sept. 19, 2005 15:33:51

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

meta.OneToOneField() problemo


On 9/19/05, Ian Maurer <> wrote:
> But, I have a multitude of problems that I don't believe I had before
> the recent model overhaul. Here are some example things I tried doing
> with my Waiter class that I believe should work:

Hey Ian,

It'd be a huge help if you could add some unit tests to
tests/testapp/models/one_to_one.py and post them back to the list.

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#4 Sept. 19, 2005 16:48:30

Ian M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

meta.OneToOneField() problemo


First, I am using 'svn diff' for submitting my patches, let me know if
there is a preferred way... especially when posting to Trac.

First, I ran into an issue with runtests.py since I use MySQL. The DB
connection autocommit requires an integer value and the current code
throws a TypeError for me. So the real brain-dead way around this was:

Index: runtests.py
===================================================================
--- runtests.py (revision 647)
+++ runtests.py (working copy)
@@ -97,6 +97,8 @@
db.connection.autocommit()
except AttributeError:
pass
+ except TypeError: # For MySQL Users
+ db.connection.autocommit(1)
self.output(1, "Creating test database")
try:
cursor.execute("CREATE DATABASE %s" %
TEST_DATABASE_NAME)
@@ -183,6 +185,8 @@
db.connection.autocommit()
except AttributeError:
pass
+ except TypeError: # For MySQL Users
+ db.connection.autocommit(1)
else:
time.sleep(1) # To avoid "database is being accessed
by other users" errors.
cursor.execute("DROP DATABASE %s" % TEST_DATABASE_NAME)


Here are my modifications to one-to-one.py:

Index: one_to_one.py
===================================================================
--- one_to_one.py (revision 647)
+++ one_to_one.py (working copy)
@@ -23,6 +23,13 @@
def __repr__(self):
return "%s the restaurant" % self.get_place().name

+class Waiter(meta.Model):
+ restaurant = meta.ForeignKey(Restaurant)
+ name = meta.CharField(maxlength=50)
+
+ def __repr__(self):
+ return "%s the waiter at %s" % (self.name,
self.get_restaurant())
+
API_TESTS = """
# Create a couple of Places.
>>> p1 = places.Place(name='Demon Dogs', address='944 W. Fullerton')
@@ -61,4 +68,10 @@
Demon Dogs the restaurant
>>> restaurants.get_object(pk=1)
Demon Dogs the restaurant
+
+# Add Waiter to Restaurant.
+>>> w = r.add_waiter(name="Joe")
+>>> w.save()
+>>> w
+Joe the waiter at Demon Dogs the restaurant
"""

My tests failed spectacularly...

Running tests with database 'mysql'

'one_to_one' module: API test raised an exception
=================================================
Code: 'w = r.add_waiter(name="Joe")'
Line: 40
Exception: File "C:\www\downloads\trunk\tests\doctest.py", line 1243,
in __run
compileflags, 1) in test.globs
File "<doctest one_to_one>", line 1, in ?
w = r.add_waiter(name="Joe")
File "C:\www\django\utils\functional.py", line 3, in _curried
return args(*(args+moreargs), **dict(kwargs.items() +
morekwargs.items()))
File "C:\www\django\core\meta\__init__.py", line 944, in
method_add_related
obj = rel_mod.Klass(**init_kwargs)
File "C:\www\django\utils\functional.py", line 3, in _curried
return args(*(args+moreargs), **dict(kwargs.items() +
morekwargs.items()))
File "C:\www\django\core\meta\__init__.py", line 755, in method_init
raise TypeError, "Invalid value: %r should be a %s instance, not a
%s" % (f.name, f.rel.to, type(rel_obj))
TypeError: Invalid value: 'restaurant' should be a <Options for
restaurants> instance, not a <class 'django.models.one_to_one.Rest
aurant'>


'one_to_one' module: API test raised an exception
=================================================
Code: 'w.save()'
Line: 41
Exception: File "C:\www\downloads\trunk\tests\doctest.py", line 1243,
in __run
compileflags, 1) in test.globs
File "<doctest one_to_one>", line 1, in ?
w.save()
NameError: name 'w' is not defined


'one_to_one' module: API test raised an exception
=================================================
Code: 'w'
Line: 42
Exception: File "C:\www\downloads\trunk\tests\doctest.py", line 1243,
in __run
compileflags, 1) in test.globs
File "<doctest one_to_one>", line 1, in ?
w
NameError: name 'w' is not defined

Let me know if I can improve on how I submit my future contributions...

regards,
Ian


Adrian Holovaty wrote:
> On 9/19/05, Ian Maurer <> wrote:
> > But, I have a multitude of problems that I don't believe I had before
> > the recent model overhaul. Here are some example things I tried doing
> > with my Waiter class that I believe should work:
>
> Hey Ian,
>
> It'd be a huge help if you could add some unit tests to
> tests/testapp/models/one_to_one.py and post them back to the list.
>
> Adrian
>
> --
> Adrian Holovaty
> holovaty.com | djangoproject.com | chicagocrime.org

Offline

#5 Sept. 20, 2005 02:04:52

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

meta.OneToOneField() problemo


On 9/19/05, Ian Maurer <> wrote:
> First, I am using 'svn diff' for submitting my patches, let me know if
> there is a preferred way... especially when posting to Trac.

"svn diff" is great, in most cases. I just created this page, which
gives more details:http://code.djangoproject.com/wiki/PatchGuidelines> First, I ran into an issue with runtests.py since I use MySQL. The DB
> connection autocommit requires an integer value and the current code
> throws a TypeError for me.

Hmmm, that shouldn't be happening. Which versions of MySQL and MySQLdb
(the Python bindings) are you using? The unit tests work here, with
MySQL 3.23.58 and MySQLdb 0.9.2.

> Here are my modifications to one-to-one.py:

Thanks for those unit tests! I'm rolling them in now...

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#6 Sept. 23, 2005 15:07:32

Ian M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

meta.OneToOneField() problemo


> "svn diff" is great, in most cases. I just created this page, which
> gives more details:
>
>http://code.djangoproject.com/wiki/PatchGuidelinesGreat, thanks... I felt dumb not knowing. Glad I asked.

> > First, I ran into an issue with runtests.py since I use MySQL. The DB
> > connection autocommit requires an integer value and the current code
> > throws a TypeError for me.
>
> Hmmm, that shouldn't be happening. Which versions of MySQL and MySQLdb
> (the Python bindings) are you using? The unit tests work here, with
> MySQL 3.23.58 and MySQLdb 0.9.2.

I guess that's our issue... not sure what has changed or why. I
haven't had a chance to investigate it.

MySQL 4.0.24
MySQLdb 1.2
Python 2.4

> > Here are my modifications to one-to-one.py:
>
> Thanks for those unit tests! I'm rolling them in now...

And I watching this ticket...http://code.djangoproject.com/ticket/527thanks!

Ian

Offline

#7 Oct. 8, 2005 21:58:09

Adrian H.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

meta.OneToOneField() problemo


On 9/19/05, Ian Maurer <> wrote:
> First, I ran into an issue with runtests.py since I use MySQL. The DB
> connection autocommit requires an integer value and the current code
> throws a TypeError for me. So the real brain-dead way around this was:

Hey Ian,

Turns out the other database wrappers (Postgres and SQLite) don't mind
if you pass an integer value to autocommit(), so I've added that to
the code in revision 805. Thanks again for the heads-up on this!

Adrian

--
Adrian Holovaty
holovaty.com | djangoproject.com | chicagocrime.org

Offline

#8 Oct. 27, 2005 19:17:32

Ian M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

meta.OneToOneField() problemo


Adrian,http://code.djangoproject.com/ticket/527Any progress with this issue? I am going to try and dig through the
code myself this weekend but I wanted to make sure I wasn't repeating
any effort. Also, let me know if there are any tips or ideas on where
to start.... obviously meta/__init__.py is probably a good place ;).

thanks-
Ian

On 9/19/05, Adrian Holovaty <> wrote:
>
> On 9/19/05, Ian Maurer <> wrote:
> > But, I have a multitude of problems that I don't believe I had before
> > the recent model overhaul. Here are some example things I tried doing
> > with my Waiter class that I believe should work:
>
> Hey Ian,
>
> It'd be a huge help if you could add some unit tests to
> tests/testapp/models/one_to_one.py and post them back to the list.
>
> Adrian
>
> --
> Adrian Holovaty
> holovaty.com | djangoproject.com | chicagocrime.org
>

Offline

Board footer

Moderator control

Enjoy the 18th of November
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support