1

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.

User007
  • 1,519
  • 6
  • 22
  • 35
  • `clean_is_active` doesn't have to return `self.cleaned_data["is_active"]`. Did you watch the console? Do you get an "I am here..." message there? Cause clean methods doesn't have to return nothing. They just need to raise a `ValidationError` if they find some inconsistence in the object you are saving – marianobianchi Jul 08 '12 at 15:46
  • @marianobianchi Thank you. you are right, i confused myself with `clean`. But anyhow. No, I didn't see any print statement in my console. That's the weird part :( I've even implemented a clean method that just said `k = 1`, and it still able to change the flag!!! This means Django is not picking up my custom class :( – User007 Jul 08 '12 at 16:34

1 Answers1

2

Maybe the problem is that you should inherit your custom MyUserAdmin from UserAdmin from 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)

Community
  • 1
  • 1
marianobianchi
  • 8,238
  • 1
  • 20
  • 24
  • Thanks. Initially I have `UserAdmin` actually. But it didn't work. I changed it to `admin.ModelAdmin`. In any case, I've attached the full code. BUt none of the options can solve my problem. So weird. By default, in urls.py there is the `autodiscover` already. I purposely setup a new 1.4 just to test this out. – User007 Jul 08 '12 at 17:53
  • Ah. I think the method is overriding the edit page. In other words, you have click on the editpage to see the effect of my fail code. So it doesn't trigger if you are on the list view page. – User007 Jul 08 '12 at 18:18
  • Ohh, i see your problem now. I didn't know you were using the list view to edit records of your database. Maybe [this](http://stackoverflow.com/questions/10524794/preventing-edit-conflict-in-djangos-list-editable-with-multiple-users) can help you. – marianobianchi Jul 10 '12 at 13:01