I want to create a new multiple choice field that has choices based on
data in the database, but has a query that is too complicated to put
into a simple queryset.  I don't see how to do this.  The code below
is what I have attempted to do.  It first selects the students that
have the active field set to what the user passes in.  Then it checks
to see if any of those students have a program associated with them.
If they do, then the student is added to the choices.  I'm still new
to Django, so maybe there is a way to do this query in a single
queryset, but I just don't see it.  Otherwise, how do you do this?


class
StudentWithProgramMultipleChoiceField(f.ModelMultipleChoiceField):
    def __init__(self, active=None, widget=f.CheckboxSelectMultiple,
*args, **kwargs):
        self.active = active
        f.ModelMultipleChoiceField.__init__(self, self.myqueryset,
widget=widget, *args, **kwargs)

    @property
    def myqueryset(self):
        choices = []
        if self.active is not None:
            students = Student.objects.filter(active=self.active)
        else:
            students = Student.objects.all()
        for student in students:
            if Program.objects.filter(student=student).count():
                choices.append((student.id, student))
        return choices


--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to