More Django tips. Check out the previous post in the series here: http://mixedquantum.blogspot.com/2015/01/python-django-tips.html
This is actually well-documented but including here nonetheless.
(This is a method of admin.ModelAdmin, override in your custom admin class)
Original docs: https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_formsets_with_inlines
When you want to limit the model instances that are seen by users depending on some property in the request object (or always). Override get_queryset. We have used this before but that was to annotate the QuerySet.
(When you have to register the model but not show it - for example, in case of django-polymorphic, you have to register child models, but not necessarily show them in admin).
Do last minute changes when a model is saved: save_model (this is also available in admin)
The advantage of doing this in admin is that you get access to the request / form / change objects, based on which you may do modifications before saving the model instance.
Exclude Inlines when creating an object.
This is actually well-documented but including here nonetheless.
(This is a method of admin.ModelAdmin, override in your custom admin class)
inlines = [MyInline] def get_formsets_with_inlines(self, request, obj=None): for inline in self.get_inline_instances(request, obj): if isinstance(inline, MyInline) and obj is None: continue yield inline.get_formset(request, obj), inline
Original docs: https://docs.djangoproject.com/en/1.7/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_formsets_with_inlines
More control in Django admin.
When you want to limit the model instances that are seen by users depending on some property in the request object (or always). Override get_queryset. We have used this before but that was to annotate the QuerySet.
class MyModelAdmin(admin.ModelAdmin): def get_queryset(self, request): if not request.user.is_superuser: return MyModel.objects.filter(some_property=10) return super(MyModelAdmin, self).get_queryset(request)
Modifying Fieldsets
When you need to change fieldsets (choose which fields to display / how to display), override get_fieldsets.
class MyModelAdmin(admin.ModelAdmin): fieldsets = [ (None, { 'fields': ['a', 'b', 'c', 'd'] }) ] def _get_add_fieldsets(self): return [(None,{'fields':['a', 'b', 'c']})] def get_fieldsets(self, request, obj=None): if not request.user.is_superuser: self.exclude = ('a') if not obj: return self._get_add_fieldsets() return super(MyModelAdmin, self).get_fieldsets(request, obj=obj)
Modifying QuerySet for individual fields (foreign keys)
We override get_form
def get_form(self, request, obj=None, **kwargs): f = super(MyModelAdmin, self).get_form(request, obj=obj, **kwargs) if not request.user.is_superuser: # a is a foreign key here, whose model is A f.base_fields['a'].queryset = A.objects.filter(some_property=1) return f
For changing the QuerySet of a field in an Inline admin class
We will override formfield_for_foreignkey and / or formfield_for_manytomany:
class InlineAdmin(admin.StackedInline): model = MyModel def formfield_for_manytomany(self, db_field, request, **kwargs): if db_field.name == 'a': kwargs['queryset'] = A.objects.filter(some_property=2) return super(InlineAdmin, self).formfield_for_manytomany( db_field, request, **kwargs)
Trickery to hide models in Admin
(When you have to register the model but not show it - for example, in case of django-polymorphic, you have to register child models, but not necessarily show them in admin).
def get_model_perms(self, request): return {}
Model functions:
Do last minute changes when a model is saved: save_model (this is also available in admin)
def save_model(self, request, obj, form, change): if not request.user.is_superuser: obj.some_property = 1337 obj.save()
The advantage of doing this in admin is that you get access to the request / form / change objects, based on which you may do modifications before saving the model instance.
No comments:
Post a Comment