(1)model的知识点

# 继承auth_user表
class UserInfo(AbstractUser):
    tel=models.CharField(max_length=32)
    gender=models.IntegerField(choices=((1,""),(2,"")),default=1)

apollo =UserInfo.objects.get(pk=1)
# 使用了choices参数的field在其model示例里,可以用"get_field的名字_display"方法显示choices的显示字串(就是2元元组的第二个数据) apollo.get_gender_display()

 

(2)modelform使用

# model.py:
class
Book(models.Model): nid=models.AutoField(primary_key=True) title=models.CharField(max_length=32) price=models.DecimalField(max_digits=8,decimal_places=2) # 999999.99 pub_date=models.DateTimeField() # "2012-12-12" # comment_count=models.IntegerField(default=100) # poll_count=models.IntegerField(default=100) publish=models.ForeignKey(to="Publish",on_delete=models.CASCADE) # 级联删除 authors=models.ManyToManyField(to="Author") def __str__(self): return self.title
form.py文件:
# 构建modelform
class BookModelForm(forms.ModelForm):
    class Meta:
        model=Book
        fields="__all__"
#############################################################################           
'''
BookModelForm等同于:
class BookForm(forms.Form):
    title=forms.CharField(max_length=32)
    price=forms.IntegerField()
    pub_date=forms.DateField(widget=widgets.TextInput(attrs={"type":"date"}))
    
    #publish=forms.ChoiceField(choices=[(1,"AAA"),(2,"BBB")])
    publish=forms.ModelChoiceField(queryset=Publish.objects.all())
    authors=forms.ModelMultipleChoiceField(queryset=Author.objects.all())
'''