#10913: Default related_name is broken
------------------------------------------+---------------------------------
Reporter: neithere | Owner: nobody
Status: new | Milestone:
Component: Database layer (models, ORM) | Version: SVN
Keywords: orm, related_name, | Stage: Unreviewed
Has_patch: 0 |
------------------------------------------+---------------------------------
The documentation [http://docs.djangoproject.com/en/dev/topics/db/queries
/#backwards-related-objects states] that by default the "backward"
relationship Manager ''"is named {{{FOO_set}}}, where {{{FOO}}} is the
source model name, lowercased"''. However, in the latest SVN snapshot the
behaviour seems to be inconsistent.
'''models.py'''
{{{
from django.db import models
class Author(models.Model):
name = models.CharField(max_length=255)
class Book(models.Model):
title = models.CharField(max_length=255)
author = models.ForeignKey(Author) # by default,
related_name='book_set'
}}}
The {{{related_name}}} argument is commented out. If not, everything works
fine, but let's see what happens now:
'''In shell after syncdb:'''
{{{
>>> from fooapp.models import Author, Book
>>> Author.book_set
<django.db.models.fields.related.ForeignRelatedObjectsDescriptor object at
0x27f4e90>
>>> Author.book
Traceback (most recent call last):
File "<console>", line 1, in <module>
AttributeError: type object 'Author' has no attribute 'book'
>>> a=Author.objects.create(name='John')
>>> b=Book.objects.create(title='My Book', author=a)
>>> Author.objects.filter(book_set__exact=1)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/dist-
packages/Django-1.1_beta_1-py2.6.egg/django/db/models/manager.py", line
129, in filter
return self.get_query_set().filter(*args, **kwargs)
File "/usr/local/lib/python2.6/dist-
packages/Django-1.1_beta_1-py2.6.egg/django/db/models/query.py", line 466,
in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/usr/local/lib/python2.6/dist-
packages/Django-1.1_beta_1-py2.6.egg/django/db/models/query.py", line 484,
in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/usr/local/lib/python2.6/dist-
packages/Django-1.1_beta_1-py2.6.egg/django/db/models/sql/query.py", line
1613, in add_q
can_reuse=used_aliases)
File "/usr/local/lib/python2.6/dist-
packages/Django-1.1_beta_1-py2.6.egg/django/db/models/sql/query.py", line
1511, in add_filter
negate=negate, process_extras=process_extras)
File "/usr/local/lib/python2.6/dist-
packages/Django-1.1_beta_1-py2.6.egg/django/db/models/sql/query.py", line
1676, in setup_joins
"Choices are: %s" % (name, ", ".join(names)))
FieldError: Cannot resolve keyword 'book_set' into field. Choices are:
book, id, name
>>> Author.objects.filter(book__exact=1)
[<Author: John>]
}}}
--
Ticket URL: <http://code.djangoproject.com/ticket/10913>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" 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-updates?hl=en
-~----------~----~----~----~------~----~------~--~---