On Sun, Apr 12, 2009 at 8:24 PM, jrs_66 <jrs...@yahoo.com> wrote:

>
> Thanks Karen,
>
> models are as follows...
>
> class MediaAndContainer(models.Model):
>    media = models.ForeignKey(MediaLocal)
>    order = models.FloatField(null=True, blank=True)
>    ...
>
> class MediaLocal(models.Model):
>    name = models.CharField(max_length=255, blank=True)
>    height = models.IntegerField(null=True, editable=False)
>    width = models.IntegerField(null=True, editable=False)
>    ...
>
> I'm trying to get everything from the MediaLocal model and the 'order'
> field from the MediaAndContainer model...
>

For which you were trying:

media = MediaLocal.objects.select_related('order').filter(
               mediaandcontainer__media__exact = 1
           )

and expecting media.order to then exist.

What you are trying has a few problems.

First, select_related('order') on a MediaLocal QuerySet doesn't do anything,
since MediaLocal doesn't have 'order' as a ForeignKey field.  (I'm a little
surprised it doesn't raise an error.)  At any rate, be aware select_related
is purely a performance optimization, and does not affect what related
objects you can access from your Python code -- it just affects how many SQL
queries are executed under the covers when you actually access the related
objects.  For reference, the complete doc for select_related is here:
http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4.

Second, 'media' in the above code is the result of filter() on a QuerySet,
and thus is a QuerySet itself (see
http://docs.djangoproject.com/en/dev/ref/models/querysets/#queryset-methods-that-return-new-querysets).
Trying to access something like media.order implies you are trying to treat
the filter() result as an instance of one of your model objects, but it
won't be, it will be a QuerySet.  If you want a model instance, you need to
retrieve a specific instance from a QuerySet, possibly by using get()
instead of filter().

Third, as you have these models defined, any particular MediaLocal instance
may have many associated MediaAndContainer instances pointing to it.  So
there are potentially many MediaAndContainer.order values for a given
MediaLocal instance.  Yet your question seems to imply there is only one?
So I'm confused.  In case it helps, I'll note you can access the set of
MediaAndContainer.order values for a given MediaLocal instance via the
order_set attribute of a MediaLocal model instance.  So if you had:

media = MediaLocal.objects.get(pk=1)

then you could iterate through media.order_set.all() and get the related
MediaAndContainer instances, each with its own order field.  But since you
seem to be expecting to be able to get a single order value, I'm not sure
these models are accurately reflecting your data.  Perhaps your media
ForeignKey in the MediaAndContainer model should be a OneToOneField (
http://docs.djangoproject.com/en/dev/ref/models/fields/#onetoonefield)
instead?  Or possibly you have the ForeignKey in the wrong model, I'm not
sure.

(BTW the 'pk=1' above is, I believe, equivalent to what you were trying to
get at with 'mediaandcontainer__media__exact = 1', see
http://docs.djangoproject.com/en/dev/topics/db/queries/#the-pk-lookup-shortcut.
I am not sure why you were using the much longer version?)

Karen

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to