#11448: Defining relationships after querying a model does not add a reverse 
lookup
to the referenced model
-------------------------------------+-------------------------------------
               Reporter:  Dennis     |          Owner:  nobody
  Kaarsemaker                        |         Status:  new
                   Type:  Bug        |      Component:  Database layer
              Milestone:             |  (models, ORM)
                Version:  SVN        |       Severity:  Normal
             Resolution:             |       Keywords:
           Triage Stage:  Accepted   |      Has patch:  1
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  1          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
Changes (by kmtracey):

 * ui_ux:   => 0


Old description:

> Ok, that sounds vague but I don't know how to better describe it.
> Basically it boils down to having this in models.py:
>
> {{{
> from django.db import models
> import os
>
> class c1(models.Model):
>     name = models.CharField("Name", max_length=30)
>
> default_c1 = c1.objects.get(name="non_existant")
>
> class c2(models.Model):
>     other = models.ForeignKey(c1, default=default_c1)
> }}}
>
> Querying c1 later with c1.objects.filter(c2__pk=0) will fail:
> FieldError: Cannot resolve keyword 'c2' into field. Choices are: id, name
>
> Minimal testcase project attached (models.py is slightly bigger than
> above). You can reproduce the problem with:
>
> {{{
> # Create the database (clobbers test.db in the current dir)
> ./manage.py syncdb
> # See that without querying in between it works
> echo -e "from proj1.app1.models import c1\nc1.objects.filter(c2__pk=0)" |
> ./manage.py shell
> # See that with querying in between it fails
> echo -e "from proj1.app1.models import c1\nc1.objects.filter(c2__pk=0)" |
> BREAK_ME=1 ./manage.py shell
> }}}
>
> Found on 1.0.2, confirmed with trunk (fresh checkout, less than 30
> minutes ago)

New description:

 Ok, that sounds vague but I don't know how to better describe it.
 Basically it boils down to having this in models.py:

 {{{
 from django.db import models
 import os

 class c1(models.Model):
     name = models.CharField("Name", max_length=30)

 default_c1 = c1.objects.get(name="non_existant")

 class c2(models.Model):
     other = models.ForeignKey(c1, default=default_c1)
 }}}

 Querying c1 later with `c1.objects.filter(c2__pk=0)` will fail:
 {{{
 FieldError: Cannot resolve keyword 'c2' into field. Choices are: id, name
 }}}
 Minimal testcase project attached (models.py is slightly bigger than
 above). You can reproduce the problem with:

 {{{
 # Create the database (clobbers test.db in the current dir)
 ./manage.py syncdb
 # See that without querying in between it works
 echo -e "from proj1.app1.models import c1\nc1.objects.filter(c2__pk=0)" |
 ./manage.py shell
 # See that with querying in between it fails
 echo -e "from proj1.app1.models import c1\nc1.objects.filter(c2__pk=0)" |
 BREAK_ME=1 ./manage.py shell
 }}}

 Found on 1.0.2, confirmed with trunk (fresh checkout, less than 30 minutes
 ago)

--

-- 
Ticket URL: <https://code.djangoproject.com/ticket/11448#comment:18>
Django <https://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 django-updates@googlegroups.com.
To unsubscribe from this group, send email to 
django-updates+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en.

Reply via email to