admin.py:
from django.contrib import admin
from .forms import SignUpForm
from .models import SignUp
class SignUpAdmin(admin.ModelAdmin):
list_display = ["__unicode__", 'timestamp', 'updated']
form = SignUpForm
admin.site.register(SignUp)
models.py:
from __future__ import unicode_literals
from django.db import models
class SignUp(models.Model):
full_name = models.CharField(max_length=120, blank=True, null=True)
email = models.EmailField()
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __unicode__(self):
return self.email
forms.py:
from django import forms
from .models import SignUp
class SignUpForm(forms.ModelForm):
class Meta:
model = SignUp
fields = ['full_name', 'email']
def clean_email(self): # It seems that this clean_email method doesn't work
email = self.cleaned_data.get('email')
email_base, provider = email.split('@')
domain, extension = provider.split('.')
if not extension == 'edu':
raise forms.ValidationError("Please use a valid .EDU email address")
return email
如果我们在管理面板中添加用户并且电子邮件没有“edu”扩展名,则应该引发ValidationError。但这不会发生。为什么呢?
答案 0 :(得分:0)
您还没有告诉管理员使用您的自定义ModelAdmin类。
这样做:
admin.site.register(SignUp, SignUpAdmin)