On Tue, Apr 14, 2009 at 9:03 AM, snorkel wrote:
>
> I am new to Django and this is driving me crazy... excuse my
> inexperience
> anyway
>
> I have 3 classes
> class Job():
> . some stuff .
>
> class Sequence()
>... more stuff ...
>job=models.ForeignKey('Job')
>element_type=models.ManyToMany('ElementType')
>
> class ElementType()
> ... some charfields etc .
>
> what I want to do is go to the job see what sequences it has and then
> return a queryset of all the element types belonging to each of the
> sequences ... I can't do it.
> Here is a list the returns what I want but I need a queryset
>
> def get_element_types(job):
>sequences=job.sequence.all()
>for s in sequences:
>et+=list(s.element_types.all())
>return et
>
> I need a queryset because I am using it in a ModelForm
>
> class SequenceForm(ModelForm):
>def __init__(self,*args,**kwargs)
>self.fields['element_types'].queryset = et
>super(SequenceForm,self).__init__(*args,**kwargs)
>
>class Meta:
>model=Sequence
>
>
> Am I doing this completely the wrong way?
>
>
> >
>
You can rewrite that query as:
ElementType.objects.filter(sequence__in=job.sequences.all())
Which is both a queryset, and more efficient since it will only do 1 SQL
query.
Alex
--
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero
--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---