Re: Adding a custom validation to Django's auth_user in admin

26 views
Skip to first unread message

dd

unread,
Jul 8, 2012, 2:19:34 PM7/8/12
to django...@googlegroups.com

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. Does anyone know what to do with the editable submission?
It should use the same clean_ method, shouldn't it??



On Sunday, July 8, 2012 9:33:49 AM UTC-4, dd wrote:

Django has list_editable. I need to edit is_active flag.

class MyUserAdminForm(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

class MyUserAdmin(admin.ModelAdmin):
 
.... SOME CODE ....
 form
= MyUserAdminForm


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.

The code above should fail because it doesn't return self.cleaned_data["is_active"]. But when I reload the development server, and changed the flag on some users, Django didn't complain, so this piece of code is not used.

My requirement is to check that the user under change is not a superuser. I have other things to do within that validation, but you get the idea.

Why is this?

Thanks.

Tomas Neme

unread,
Jul 8, 2012, 8:38:22 PM7/8/12
to django...@googlegroups.com
> 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. Does anyone know what to do with
> the editable submission?
> It should use the same clean_ method, shouldn't it??

if you want this validation everywhere you should probably do
something in the model's .save() method, or even better (to avoid
monkey patching) register into the pre_save signal

--
"The whole of Japan is pure invention. There is no such country, there
are no such people" --Oscar Wilde

|_|0|_|
|_|_|0|
|0|0|0|

(\__/)
(='.'=)This is Bunny. Copy and paste bunny
(")_(") to help him gain world domination.
Reply all
Reply to author
Forward
0 new messages