Ticket 10405 and documentation about using 'app.model' notation in FKs to other apps models

3 views
Skip to first unread message

Ramiro Morales

unread,
Mar 10, 2009, 8:59:25 AM3/10/09
to django-d...@googlegroups.com
Hi,

While trying to get a grip on this [1]ticket about problems experienced
when using the 'otherapp.model' notation to the target model of a
Foreign Key when that model is located on another application. I went to
the relevant [2]docs:

"""
If you need to create a relationship on a model that has not yet been
defined, you can use the name of the model, rather than the model object
itself::

class Car(models.Model):
manufacturer = models.ForeignKey('Manufacturer')
# ...

class Manufacturer(models.Model):
# ...

Note, however, that this only refers to models in the same models.py
file -- you cannot use a string to reference a model defined in another
application or imported from elsewhere.

Changed in Django 1.0: Refering models in other applications must
include the application label.

To refer to models defined in another application, you must instead
explicitly specify the application label. For example, if the
Manufacturer model above is defined in another application called
production, you'd need to use...
""""

It seems the two last paragraphs (from "Changed in Django 1.0"
onwards) contradict the paragraph inmediately above them.

Supporting this usage might mean we need to insert at least one
additional django.deb.models.get_apps() call (some of the places where
it has already been inserted so far have associated comments describing
doing that as a hack.)

Namely, from the cases reported in the ticket and associated threads it
seems it would need to be inserted in

django.core.urlresolvers._get_urlconf_module()

But if we don't need to support it we just can change this ticket
component to Documentation and correct the docs to delete the two last
paragraphs.

Am I missing something here?.

Regards,

--
Ramiro Morales
http://rmorales.net

1. http://code.djangoproject.com/ticket/10405
2. http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey

Ramiro Morales

unread,
May 1, 2009, 9:24:10 PM5/1/09
to django-d...@googlegroups.com
On Tue, Mar 10, 2009 at 9:59 AM, Ramiro Morales <cra...@gmail.com> wrote:
> [...]

> I went to the relevant [2]docs:
>
> """
> If you need to create a relationship on a model that has not yet been
> defined, you can use the name of the model, rather than the model object
> itself::
>
>    class Car(models.Model):
>        manufacturer = models.ForeignKey('Manufacturer')
>        # ...
>
>    class Manufacturer(models.Model):
>        # ...
>
> Note, however, that this only refers to models in the same models.py
> file -- you cannot use a string to reference a model defined in another
> application or imported from elsewhere.
>
> Changed in Django 1.0: Refering models in other applications must
> include the application label.
>
> To refer to models defined in another application, you must instead
> explicitly specify the application label. For example, if the
> Manufacturer model above is defined in another application called
> production, you'd need to use...
> """"
>
> It seems the two last paragraphs (from "Changed in Django 1.0"
> onwards) contradict the paragraph inmediately above them.

Jacob clarified this via IRC: Currently the only scenario when we support
the target model of a a relationship located in another application being
specified in string form is when there are mutually pointing relationships
between that model and the current model (it's the way out of a circular
import trap).

This isn't what one would interpret from reading the above quoted
fragment so, I will open another (but Component=Documentation) ticket
with a patch to fix this.

>
> Supporting this usage might mean we need to insert at least one
> additional django.deb.models.get_apps() call (some of the places where
> it has already been inserted so far have associated comments describing
> doing that as a hack.)
>
> Namely, from the cases reported in the ticket and associated threads it
> seems it would need to be inserted in
>
> django.core.urlresolvers._get_urlconf_module()

Seems the solution is harder that this, and the behavior has changed
since I tested things back in March 6th. Will keep trying to wrap my head
around this, hopefully will be able to attach a patch.

> 2. http://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ForeignKey

Reply all
Reply to author
Forward
0 new messages