直接上代码:
models.py
from django.forms import ModelForm from django.db import models TITLE_CHOICES = ( ("MR","Mr.‘), ("MRS","Mrs."), ("MS",‘Ms."), ) class Author(models.Model): name = models.CharField(max_length=100) title = models.CharField(max_length=3,choices=TITLE_CHOICES) birth_date = models.DateField(blank=True,null=True) def __str__(self): return self.name class Book(models.Model): name = models.CharField(max_length=100) authors = models.ManyToManyField(Author) class AuthorForm(ModelForm): class Meta: model = Author fields = [‘name‘,‘title‘,‘birth_date‘] class BookForm(ModelForm): class Meta: model = book fields = [‘name‘,‘authors‘]
时间: 2024-10-29 10:46:51