首先,我们来看看modelform的实现
model.py class Book(models.Model): title=models.CharField(max_length=32)
price=models.DecimalField(max_digits=8,decimal_places=2)
pub_date=models.DateField()
publish=models.ForeignKey("Publish")
authors=models.ManyToManyField("Author")
def __str__(self): return self.title
forms.py
#Modelform将一个model转化成一个form组件
class BookModelForm(forms.ModelForm):
class Meta:
model=models.Book
fields="__all__"
这一步做的事情相当于下面的代码
'''
class BookModelForm(form.Form):
title=forms.CharField(max_length=32)
price=forms.IntegerField()
pub_date=forms.DateField()
'''
下面看看forms组件里面的内容:里面包含了很多的类和插件,并且包含了modelform组件
""" Django validation and HTML form handling. #Django验证和HTML表单处理
"""
from django.core.exceptions import ValidationError # NOQA
from django.forms.boundfield import * # NOQA
from django.forms.fields import * # NOQA
from django.forms.forms import * # NOQA
from django.forms.formsets import * # NOQA
from django.forms.models import * # NOQA
from django.forms.widgets import * # NOQA
下面定位到modelform:它继承了BaseModelForm
class ModelForm(six.with_metaclass(ModelFormMetaclass, BaseModelForm)): pass