1 2 3 4 5 6 7 8 | class MyAdmin(admin.ModelAdmin):
def get_form(self, request, obj=None, **kwargs):
form = super(MyAdmin,self).get_form(self,request, obj,**kwargs)
# form class is created per request by modelform_factory function
# so it's safe to modify
#we modify the the queryset
form.base_fields['foreign_key_field].queryset = form.base_fields['foreign_key_field].queryset.filter(user=request.user)
return form
|
More like this
- ForeignKey filterspec by luc_j 2 years, 8 months ago
- Bind Administration by ashcrow 4 years, 8 months ago
- Exclusive boolean field by anentropic 3 years, 5 months ago
- SnippySnip by youell 4 years, 10 months ago
- Closure for FieldListFilter classes with custom sets of ranges by ssokolow 11 months ago
Comments
For those unaware of formfield_for_foreignkey (as I was when I came upon this snippet), the docs are here.
#
Thanks for the snippet was exactly what I was looking for
I found a couple typos in implementing a revised example is below
#