Opened 7 years ago

Closed 6 years ago

Last modified 6 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: master
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: UI/UX:

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

Download all attachments as: .zip

Change History (11)

comment:1 Changed 7 years ago by Ramiro Morales

Description: modified (diff)

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

comment:2 Changed 7 years ago by Russell Keith-Magee

Triage Stage: UnreviewedAccepted

Changed 6 years ago by Gabriel Grant

Attachment: 13241.diff added

Patch (with test) fixing the problem

comment:3 Changed 6 years ago by Gabriel Grant

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

Changed 6 years ago by Gabriel Grant

Attachment: 13241_unittest.diff added

doctest moved to unittest at the request of Alex Gaynor

Changed 6 years ago by Gabriel Grant

Attachment: 13241_unittest.2.diff added

unittestified *with 4-space indent*

Changed 6 years ago by Alex Gaynor

Attachment: t13241.diff added

A few line length fixes.

comment:4 Changed 6 years ago by Alex Gaynor

Triage Stage: AcceptedReady for checkin

Changed 6 years ago by Alex Gaynor

Attachment: t13241.2.diff added

Missing tests :/

comment:5 Changed 6 years ago by Alex Gaynor

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 Changed 6 years ago by Alex Gaynor

(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