Opened 15 years ago

Closed 14 years ago

Last modified 14 years ago

#13241 closed (fixed)

order_with_respect_to fails on a relationship on a model that has not yet been defined

Reported by: Goldan Owned by: Gabriel Grant
Component: Database layer (models, ORM) Version: dev
Severity: Keywords: order_with_respect_to
Cc: Goldan Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Ramiro Morales)

If I specify 'order_with_respect_to' on a field linked to a model that hasn't been defined yet, I get the following error:

<...>File "/usr/lib/python2.6/dist-packages/django/db/models/base.py", line 223, in _prepare
    setattr(opts.order_with_respect_to.rel.to, 'get_%s_order' % cls.__name__.lower(), curry(method_get_order, cls))
AttributeError: 'str' object has no attribute 'get_location_order'

When I swap declarations of Location and City models, the error disappears.

Here is the code:

class Location(TimeStampedModel):
    '''Single location on a map, e.g. building.'''
    address = models.CharField(u"адрес", max_length=75, help_text=u"Улица, корпус, дом")
    city = models.ForeignKey('City', related_name="locations", verbose_name=u"город")
    metro_stations = models.ManyToManyField('MetroStation', related_name="locations", verbose_name=u"ближайшие станции метро", blank=True, null=True)

    class Meta:
        verbose_name = u"адрес"
        verbose_name_plural = u"адреса"
        order_with_respect_to = 'city'

class City(TimeStampedModel):
    '''A city.'''
    name = models.CharField(u"название", max_length=30)

    class Meta:
        verbose_name = u"город"
        verbose_name_plural = u"города"

Attachments (5)

13241.diff (3.1 KB ) - added by Gabriel Grant 14 years ago.
Patch (with test) fixing the problem
13241_unittest.diff (3.5 KB ) - added by Gabriel Grant 14 years ago.
doctest moved to unittest at the request of Alex Gaynor
13241_unittest.2.diff (3.6 KB ) - added by Gabriel Grant 14 years ago.
unittestified *with 4-space indent*
t13241.diff (3.3 KB ) - added by Alex Gaynor 14 years ago.
A few line length fixes.
t13241.2.diff (4.1 KB ) - added by Alex Gaynor 14 years ago.
Missing tests :/

Download all attachments as: .zip

Change History (11)

comment:1 by Ramiro Morales, 15 years ago

Description: modified (diff)

(reformatted description, please use the 'Preview' button)

comment:2 by Russell Keith-Magee, 15 years ago

Triage Stage: UnreviewedAccepted

by Gabriel Grant, 14 years ago

Attachment: 13241.diff added

Patch (with test) fixing the problem

comment:3 by Gabriel Grant, 14 years ago

Has patch: set
Owner: changed from nobody to Gabriel Grant
Status: newassigned

I've attached a patch with a test which fixes the bug, but since this is the first patch I've submitted to django, I'm not sure what the next step is to move it forward...

If anyone has any comments on the patch, I'd love to hear them.

Thanks!
-Gabriel

by Gabriel Grant, 14 years ago

Attachment: 13241_unittest.diff added

doctest moved to unittest at the request of Alex Gaynor

by Gabriel Grant, 14 years ago

Attachment: 13241_unittest.2.diff added

unittestified *with 4-space indent*

by Alex Gaynor, 14 years ago

Attachment: t13241.diff added

A few line length fixes.

comment:4 by Alex Gaynor, 14 years ago

Triage Stage: AcceptedReady for checkin

by Alex Gaynor, 14 years ago

Attachment: t13241.2.diff added

Missing tests :/

comment:5 by Alex Gaynor, 14 years ago

Resolution: fixed
Status: assignedclosed

(In [14045]) Fixed #13241. order_with_respect_to now works with ForeignKeys who refer to their model lazily (i.e. with a string). Thanks to Gabriel Grant for the patch.

comment:6 by Alex Gaynor, 14 years ago

(In [14046]) [1.2.X] Fixed #13241. order_with_respect_to now works with ForeignKeys who refer to their model lazily (i.e. with a string). Thanks to Gabriel Grant for the patch. This is a backport of [14045].

Note: See TracTickets for help on using tickets.
Back to Top