1. 程式人生 > >django Form模塊實例(二)

django Form模塊實例(二)

modelform

直接上代碼:

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‘]


本文出自 “13010688” 博客,請務必保留此出處http://13020688.blog.51cto.com/13010688/1941359

django Form模塊實例(二)