How to use custom AdminSite class?

The Problem Using a custom class derived from django.contrib.admin.AdminSite for the admin site of a project, without having to write custom registration code to register models with the new class. When I use 3rd party apps with their own models, I’d rather not have to edit custom registration code only because models were added or … Read more

Django admin – inline inlines (or, three model editing at once)

You need to create a custom form and template for the LinkSectionInline. Something like this should work for the form: LinkFormset = forms.modelformset_factory(Link) class LinkSectionForm(forms.ModelForm): def __init__(self, **kwargs): super(LinkSectionForm, self).__init__(**kwargs) self.link_formset = LinkFormset(instance=self.instance, data=self.data or None, prefix=self.prefix) def is_valid(self): return (super(LinkSectionForm, self).is_valid() and self.link_formset.is_valid()) def save(self, commit=True): # Supporting commit=False is another can of worms. … Read more

Add custom form fields that are not part of the model (Django)

Either in your admin.py or in a separate forms.py you can add a ModelForm class and then declare your extra fields inside that as you normally would. I’ve also given an example of how you might use these values in form.save(): from django import forms from yourapp.models import YourModel class YourModelForm(forms.ModelForm): extra_field = forms.CharField() def … Read more

How to change site title, site header and index title in Django Admin?

As of Django 1.7 you don’t need to override templates. You can now implement site_header, site_title, and index_title attributes on a custom AdminSite in order to easily change the admin site’s page title and header text. Create an AdminSite subclass and hook your instance into your URLconf: admin.py: from django.contrib.admin import AdminSite from django.utils.translation import … Read more

Django — Conditional Login Redirect

Create a separate view that redirects user’s based on whether they are in the admin group. from django.shortcuts import redirect def login_success(request): “”” Redirects users based on whether they are in the admins group “”” if request.user.groups.filter(name=”admins”).exists(): # user is an admin return redirect(“admin_list”) else: return redirect(“other_view”) Add the view to your urls.py, url(r’login_success/$’, views.login_success, … Read more