How do I filter ForeignKey choices in a Django ModelForm? How do I filter ForeignKey choices in a Django ModelForm? python python

How do I filter ForeignKey choices in a Django ModelForm?


ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet. See the reference for ModelChoiceField.

So, provide a QuerySet to the field's queryset attribute. Depends on how your form is built. If you build an explicit form, you'll have fields named directly.

form.rate.queryset = Rate.objects.filter(company_id=the_company.id)

If you take the default ModelForm object, form.fields["rate"].queryset = ...

This is done explicitly in the view. No hacking around.


In addition to S.Lott's answer and as becomingGuru mentioned in comments, its possible to add the queryset filters by overriding the ModelForm.__init__ function. (This could easily apply to regular forms) it can help with reuse and keeps the view function tidy.

class ClientForm(forms.ModelForm):    def __init__(self,company,*args,**kwargs):        super (ClientForm,self ).__init__(*args,**kwargs) # populates the post        self.fields['rate'].queryset = Rate.objects.filter(company=company)        self.fields['client'].queryset = Client.objects.filter(company=company)    class Meta:        model = Clientdef addclient(request, company_id):        the_company = get_object_or_404(Company, id=company_id)        if request.POST:            form = ClientForm(the_company,request.POST)  #<-- Note the extra arg            if form.is_valid():                form.save()                return HttpResponseRedirect(the_company.get_clients_url())        else:            form = ClientForm(the_company)        return render_to_response('addclient.html',                                   {'form': form, 'the_company':the_company})

This can be useful for reuse say if you have common filters needed on many models (normally I declare an abstract Form class). E.g.

class UberClientForm(ClientForm):    class Meta:        model = UberClientdef view(request):    ...    form = UberClientForm(company)    ...#or even extend the existing custom initclass PITAClient(ClientForm):    def __init__(company, *args, **args):        super (PITAClient,self ).__init__(company,*args,**kwargs)        self.fields['support_staff'].queryset = User.objects.exclude(user='michael')

Other than that I'm just restating Django blog material of which there are many good ones out there.


This is simple, and works with Django 1.4:

class ClientAdminForm(forms.ModelForm):    def __init__(self, *args, **kwargs):        super(ClientAdminForm, self).__init__(*args, **kwargs)        # access object through self.instance...        self.fields['base_rate'].queryset = Rate.objects.filter(company=self.instance.company)class ClientAdmin(admin.ModelAdmin):    form = ClientAdminForm    ....

You don't need to specify this in a form class, but can do it directly in the ModelAdmin, as Django already includes this built-in method on the ModelAdmin (from the docs):

ModelAdmin.formfield_for_foreignkey(self, db_field, request, **kwargs)¶'''The formfield_for_foreignkey method on a ModelAdmin allows you to    override the default formfield for a foreign keys field. For example,    to return a subset of objects for this foreign key field based on the   user:'''class MyModelAdmin(admin.ModelAdmin):    def formfield_for_foreignkey(self, db_field, request, **kwargs):        if db_field.name == "car":            kwargs["queryset"] = Car.objects.filter(owner=request.user)        return super(MyModelAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

An even niftier way to do this (for example in creating a front-end admin interface that users can access) is to subclass the ModelAdmin and then alter the methods below. The net result is a user interface that ONLY shows them content that is related to them, while allowing you (a super-user) to see everything.

I've overridden four methods, the first two make it impossible for a user to delete anything, and it also removes the delete buttons from the admin site.

The third override filters any query that contains a reference to (in the example 'user' or 'porcupine' (just as an illustration).

The last override filters any foreignkey field in the model to filter the choices available the same as the basic queryset.

In this way, you can present an easy to manage front-facing admin site that allows users to mess with their own objects, and you don't have to remember to type in the specific ModelAdmin filters we talked about above.

class FrontEndAdmin(models.ModelAdmin):    def __init__(self, model, admin_site):        self.model = model        self.opts = model._meta        self.admin_site = admin_site        super(FrontEndAdmin, self).__init__(model, admin_site)

remove 'delete' buttons:

    def get_actions(self, request):        actions = super(FrontEndAdmin, self).get_actions(request)        if 'delete_selected' in actions:            del actions['delete_selected']        return actions

prevents delete permission

    def has_delete_permission(self, request, obj=None):        return False

filters objects that can be viewed on the admin site:

    def get_queryset(self, request):        if request.user.is_superuser:            try:                qs = self.model.objects.all()            except AttributeError:                qs = self.model._default_manager.get_queryset()            return qs        else:            try:                qs = self.model.objects.all()            except AttributeError:                qs = self.model._default_manager.get_queryset()            if hasattr(self.model, ‘user’):                return qs.filter(user=request.user)            if hasattr(self.model, ‘porcupine’):                return qs.filter(porcupine=request.user.porcupine)            else:                return qs

filters choices for all foreignkey fields on the admin site:

    def formfield_for_foreignkey(self, db_field, request, **kwargs):        if request.employee.is_superuser:            return super(FrontEndAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)        else:            if hasattr(db_field.rel.to, 'user'):                kwargs["queryset"] = db_field.rel.to.objects.filter(user=request.user)            if hasattr(db_field.rel.to, 'porcupine'):                kwargs["queryset"] = db_field.rel.to.objects.filter(porcupine=request.user.porcupine)            return super(ModelAdminFront, self).formfield_for_foreignkey(db_field, request, **kwargs)