Todd O'Bryan wrote:
>The tutorial explains how to get objects based on field values, but I
>need to get a subset of the objects in a OneToMany relationship based
>on one of their values. Here's an example:
>
>BRANCH_KINDS = ((0, 'Main'), (1, 'Auxiliary'), (2, 'Dead'),)
>
>class Trunk(meta.Model):
> name = meta.CharField(max_length=10)
>
>class Branch(meta.Model):
> trunk = meta.ForeignKey(Trunk)
> kind = meta.IntegerField(choices=BRANCH_KINDS)
>
>Say I have a Trunk object and want to get all of its Auxiliary
>branches. How the heck do I do that?
>
>
Since your DB doesn't know anything about BRANCH_KINDS values you should
manually find a number corresponding a value and use it for lookup:
from myproject.myapp.models import BRANCH_KINDS
index = [bk[1] for bk in BRANCH_KINDS].index('Auxillary')
trunks.get_branch_list(kind__exact=BRANCH_KINDS[index][0])
But it anyway looks strange that you need to make a DB lookup based on
values intended only for display purposes and that can be changed any time.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users
-~----------~----~----~----~------~----~------~--~---