Django has list_editable. I need to edit is_active flag.
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.contrib import admin
from django.db.models import Q
from django import forms
class MyUserAdminForm(forms.ModelForm):
class Meta:
model = User
#def clean_is_active(self):
# do something that validates your data
# print ' I am here... '
# print self.cleaned_data
# print self
def clean(self):
k = 1
class MyUserAdmin(UserAdmin):
list_display = ["id", "username", "email", "is_staff", "is_superuser", "is_active", "date_joined"]
search_fields = ["username", "email"]
list_display_links = ["id", "username"]
list_editable = ("is_active",)
list_per_page = 50
form = MyUserAdminForm
# Had to unregister the User so it could be registered with MyUserAdmin
admin.site.unregister(User)
admin.site.register(User, MyUserAdmin)
I put this in admin.py, and I’ve registered my admin.ModelAdmin code. The display is fine. So MyUserAdmin is working fine.
This code should fail because of the clean method. But it didn’t. It isn’t using MyUserAdminForm apparently. I need to clean the is_active.
Thanks.
Note I have inherited from admin.ModelAdmin rather than UsreAdmin. I have used all the combinations I can have……
Update
I think this piece of code is only useful for the edit page, not for the list_editable option.
Maybe the problem is that you should inherit your custom
MyUserAdminfromUserAdminfrom django’s admin models…Here you can find an example of how to do that: Customizing an Admin form in Django while also using autodiscover
When you add a change like this, you should restart your server (restart manage.py or apache or whatever you are using)