标签:publish 表关联 key ons length toc 假设 自身 情况
基表:为抽象表,是专门用来继承,提供公有字段,自身不会完成数据库迁移
class BaseModel(models.Model):
is_delete = models.BooleanField(default=False)
created_time = models.DateTimeField(auto_now_add=True)
class Meta:
# 基表,为抽象表,是专门用来和继承,提供公有字段,自身不会完成数据库迁移
abstract = True
class Book(BaseModel):
name = models.CharField(max_length=64)
price = models.DecimalField(max_digits=10, decimal_places=2)
publish = models.ForeignKey(to='Publish', related_name='books', db_constraint=False, on_delete=models.DO_NOTHING, null=True)
authors = models.ManyToManyField(to='Author', related_name='books', db_constraint=False)
def __str__(self):
return self.name
class Publish(BaseModel):
name = models.CharField(max_length=64)
address = models.CharField(max_length=64)
class Author(BaseModel):
name = models.CharField(max_length=64)
class AuthorDetail(BaseModel):
phone = models.CharField(max_length=64)
author = models.OneToOneField(to=Author, related_name='detail', on_delete=models.CASCADE, db_constraint=False)
## 子序列化:
many=True
from rest_framework import serializers
from . import models
class BookModelSerializers(serializers.ModelSerializer):
class Meta:
model = models.Book
fields = '__all__'
class PublishModelSerializers(serializers.ModelSerializer):
books = BookModelSerializers(many=True)
class Meta:
model = models.Publish
# fields = '__all__'
fields = ['name', 'address', 'books']
标签:publish 表关联 key ons length toc 假设 自身 情况
原文地址:https://www.cnblogs.com/setcreed/p/12104906.html