Opened 5 years ago

Closed 5 years ago

Last modified 5 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: gg
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)

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

Download all attachments as: .zip

Change History (11)

comment:1 Changed 5 years ago by ramiro

  • Description modified (diff)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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

comment:2 Changed 5 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

Changed 5 years ago by gg

Patch (with test) fixing the problem

comment:3 Changed 5 years ago by gg

  • Has patch set
  • Owner changed from nobody to gg
  • Status changed from new to assigned

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 5 years ago by gg

doctest moved to unittest at the request of Alex Gaynor

Changed 5 years ago by gg

unittestified *with 4-space indent*

Changed 5 years ago by Alex

A few line length fixes.

comment:4 Changed 5 years ago by Alex

  • Triage Stage changed from Accepted to Ready for checkin

Changed 5 years ago by Alex

Missing tests :/

comment:5 Changed 5 years ago by Alex

  • Resolution set to fixed
  • Status changed from assigned to closed

(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 5 years ago by Alex

(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