class Photo(models.Model):
image = models.ImageField(...)
etc. etc.
In the 'articles' app, I have the following:
from myproject.stockphoto.models import Photo
class Article(models.Model):
title = models.CharField(maxlength=200, unique=True)
slug = models.SlugField(prepopulate_from=['title'])
body = models.TextField()
picture = models.ForeignKey(Photo, blank=True,null=True)
In the Admin, everything works fine and dandy. When adding new
Articles, I can either point them at existing pictures, or add new
pictures.
And instances of 'Article' do what you would expect.
Instances of 'Photo' do not though. I *should* be able to do something like:
>>> x = Photo.objects.all()[0]
>>> articles = x.article_set.all()
(I want to be able to find all the articles a given picture has been used in).
This is decidedly not working. The reason being is that my instances
of Photo *never* have an 'article_set' attribute.
The only reason I can guess is that because the two models are defined
in separate applications.
Am I missing something here?
Jay P.
PS. An interesting (and perhaps related) sidenote: In my
INSTALLED_APPS, I *have* to have the 'stockphoto' app appear before
the 'articles' app, otherwise I get the following when trying to
import 'Article' in IPython:
ImportError: No module named model
The only reason I can guess is that because the two models are defined
in separate applications.
Am I missing something here?
PS. An interesting (and perhaps related) sidenote: In my
INSTALLED_APPS, I *have* to have the 'stockphoto' app appear before
the 'articles' app, otherwise I get the following when trying to
import 'Article' in IPython:
ImportError: No module named model
Well, from the shell, it seems to work like a charm. It only seems to
broken at the Admin level.
My Photo model is here:
http://svn.jayparlar.com/website/trunk/awwca/stockphoto/models.py
(This is almost identical to the models.py that ships with the
'stockphoto' application)
My Article model is here:
http://svn.jayparlar.com/website/trunk/awwca/articles/models.py
(Although for these tests today, I stripped out all the fields except
'title' and 'picture')
Here's what I tried: Added a Photo and Article from the admin. In the
shell, there was no 'article_set' attribute.
However, staying in the shell, I created (and saved) a *new* Article
instance, giving it the Picture instance that was created in the
Admin. After that, I *would* have an 'article_set' attribute, that
would show both the Article created in the Admin, and the one created
in the shell.
Jay P.
Hi Jay
I had a similar problem recently. It turned out to have something to do
with the blank=True and null=True arguments.
picture = models.ForeignKey(Photo, blank=True,null=True)
Maybe, for testing, try removing those and see it the article_set is
available as expected without them.
picture = models.ForeignKey(Photo)
If it is, it may be a bug in django.
I don't know if this is really related to your problem, just thought
I'll drop a note here as the symptoms sound familiar.
No such luck, I'm still seeing the problem. I appreciate the thought though!
Jay P.
"The answer lies in the INSTALLED_APPS setting. The first time any
model is loaded, Django iterates over every model in INSTALLED_APPS
and creates the backward relationships in memory as needed.
Essentially, one of the functions of INSTALLED_APPS is to tell Django
the entire model domain."
Because 'stockphoto' appears before 'articles' in INSTALLED_APPS,
could it be preventing the ORM from seeing the 'Article' model?
That doesn't feel quite right, simply because someone would have hit
it before this, but it's a possibility.
Jay P.
Because 'stockphoto' appears before 'articles' in INSTALLED_APPS,
could it be preventing the ORM from seeing the 'Article' model?
Alright, that's what I thought.
I'll keep playing with it. This really *feels* like I've just screwed
up something somewhere. I'll update if I find a solution.
Of course, any more suggestions (from anyone) are always welcome :)
Jay P.
Here's what I tried: Added a Photo and Article from the admin. In the
shell, there was no 'article_set' attribute.
I'll keep playing with it. This really *feels* like I've just screwed
up something somewhere. I'll update if I find a solution.
Sounds good, thanks so much!
Here's something I *just* found though: If instead of:
>>> from testproject.mytest.models import Photo
>>> Photo.objects.all()[0].article_set.all()
you do:
>>> from testproject.mytest.models import Photo
>>> x = Photo.objects.get(id=1).article_set.all()
it all works perfectly! So if you do a get() instead of an all(), the
'article_set' attribute gets created.
I've created a patch (http://code.djangoproject.com/ticket/2684) that
mentions all of this.
Jay P.
The ticket now has a patch to solve the problem. This was a nasty
little edge case; details attached to the patch.
Ordinarily, I would just commit the patch, but there may still be a
development moratorium on django.db.models if effect at the moment
(pending some refactoring from Adrian); I'll try and get confirmation
if this moratorium is still in effect.
Yours
Russ Magee %-)
Perfect. I'll keep my eye on the ticket. Thanks for all the help on this.
Jay P.