Opened 15 years ago
Last modified 10 years ago
#11448 closed
Defining relationships after querying a model does not add a reverse lookup to the referenced model — at Initial Version
Reported by: | Dennis Kaarsemaker | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | dennis.kaarsemaker@…, jrose@…, Anton Strogonoff | Triage Stage: | Accepted |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
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(c2pk=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)
Note:
See TracTickets
for help on using tickets.
Minimal project to demonstrate the bug