models中字段用处总结

 字段用处:

1. admin中验证

2.obj.clean_fields()

3. (1) form组件验证

 (2)model组件操作数据

      username

      email

4.ModelForm

  ModelForm组件验证 

    用户models中的字段

    model组件操作数据

              username

              email

    Admin使用ModelForm

 

例子如下:
admin中代码:

from django.contrib import admin
from app01 import models
from django import forms
# Register your models here.
class TsetForm(forms.ModelForm):
username=forms.CharField(error_messages={'required':'用户名不能为空'})
email=forms.EmailField(error_messages={'invalid':'邮箱格式错误','required':'邮箱不能为空'})
age=forms.IntegerField(initial=1,error_messages={'required':'请输入数值',"invalid":"年龄必须为数值"})
class Meta:
model=models.Test
fields='__all__'

class TestFormAdmin(admin.ModelAdmin):
form=TsetForm
admin.site.register(models.Test,TestFormAdmin)

models中代码:
class Test(models.Model):
username=models.CharField(max_length=32,)
email=models.EmailField()



 

posted @ 2020-04-19 16:08  Stary_tx  阅读(165)  评论(0编辑  收藏  举报