Django管理字段类覆盖验证

时间:2014-02-06 10:25:23

标签: python django django-admin

我正在尝试在Django管理员中实现手机价值验证。为此我想使用已经存在的Field类localflavor.lt.forms.LTPhoneField。但是使用简化的代码示例,波纹管验证不起作用:

# from model.py
class Person(models.Model):
    name = models.CharField(max_length=50)
    phone = models.CharField(max_length=15)

# from admin.py
from django.contrib import admin
from localflavor.lt.forms import LTPhoneField

class PersonAdmin(admin.ModelAdmin):
    phone = LTPhoneField()
admin.site.register(Person, PersonAdmin)

编辑已解决。看看我自己的答案。

2 个答案:

答案 0 :(得分:1)

我通过添加其他forms.ModelForm类解决了这个问题:

class PersonForm(forms.ModelForm)
    phone = LTPhoneField()

class PersonAdmin(admin.ModelAdmin):
    form = PersonForm

admin.site.register(Person, PersonAdmin)

答案 1 :(得分:0)

尝试一些事情:

class PersonAdmin(admin.ModelAdmin):
    phone = LTPhoneField()
    def clean_phone(self):
        return self.cleaned_data["phone"]
admin.site.register(Person, PersonAdmin)

* *未经测试