Hi!,

A few days ago I posted a ticket (#2217) describing this problem. The quick
and dirty fix was to use the primary key of the related object when
filtering/getting.

However, I found out that the current behavior breaks the get_or_create()
functionality: the get-part needs the primary key, but the create-part needs
the object. Because of this, get_or_create() won't work.

An example using the models Author and Book from the ticket:

>>> foo = Author(name="Foo")
>>> foo.save()
>>> Book.objects.get_or_create(title="Bar", author=foo)

This would result in ProgrammingError: ERROR: syntax error at or near
"object" at character 152. The get-fails because for it work, it would be
required to use author=foo.id instead of author=foo

Second try, using the primary key:

>>> foo = Author(name="Foo")
>>> foo.save()
>>> Book.objects.get_or_create(title="Bar", author=foo.id)

Now, the get-part works fine, but as there isn't a Book with title of "Bar"
and author of foo.id, it tries to create one -- and it fails because I can't
say author=foo.id when creating objects. The creation process results to
"TypeError: Invalid value: 'author' should be a <class
'theproject.theapp.models.Author'> instance, not a <type 'int'>"

As said in the ticket, the current behavior would probably be quite trivial
to change and would result in more natural way to filter/get objects -- not
to mention, it would allow the get_or_create() to work properly.

- Mikko Nylén


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers
-~----------~----~----~----~------~----~------~--~---

Reply via email to