实现环境表结构:
models.py表单创建与代码
from django.db import models
# Create your models here.
class Publisher(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=64,null=False,unique=True)
def __str__(self):
return "publisher_name:{}".format(self.name)
class Book(models.Model):
id = models.AutoField(primary_key=True)
title = models.CharField(max_length=128,null=False)
publisher = models.ForeignKey(to=Publisher) #外键关联
def __str__(self):
return "book_title:{}".format(self.title)
class Author(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=16,null=False)
book = models.ManyToManyField(to="Book") #跟BOOK多对多关系
def __str__(self):
return "author_name:{}".format(self.name)
原文地址:https://blog.51cto.com/12965094/2392068
时间: 2024-10-08 20:16:19