Hi all,

is it possible to obtain the model instance for a ModelChoiceField in the constructor of the Form?

Here is a short example that illustrates the problem:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

class Department(models.Model):
    pass


class Staff(models.Model):
    departments = models.ManyToManyField(Department)


class SetDepForStaff_Form(forms.Form):
St = forms.ModelChoiceField(queryset=Staff.objects.all(), widget=forms.HiddenInput())
    Dep = forms.ModelChoiceField(queryset=Department.objects.all())

    def __init__(self, *args, **kwargs):
        user = kwargs.pop('user')
        super(SetDepForStaff_Form, self).__init__(*args, **kwargs)

# Working, but not what we need: passing in data via the form constructor.
        self.fields['Dep'].queryset = user.department_set.all()

        # Wanted: How can we access the model instance for the St field?
        # It would be the initial value if the form is created with
        #     SetDepForStaff_Form(initials=...)
        # or the selected value if created with
        #     SetDepForStaff_Form(request.POST)
self.fields['Dep'].queryset = model_instance_of(self.fields['St']).departments.all()

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Any help would be very much appreciated!!  :-)

Best regards,
Carsten

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


Reply via email to