day 75 base table foreign key field of the parameter sequence

Base table

  • Base table only provides the public field, not involved in database migration
  • In the configuration of the base class table class Meta:settings: abstract = True, indicates that the list is an abstract representation, does not participate in the database migration
# 基表
class Base(models.Model):  
    is_delete = models.BooleanField(default=False)
    created_time = models.DateTimeField(auto_now=True)
    
    class Meta:
        # 表明该表为抽象表, 只提供公有字段, 不参与数据库迁移 
        abstract = True

Foreign key field parameters

Database associated db_constraint

  • Off related: db_constraint=Falseboth the database does not establish a foreign key relationship, Django achieve two related tables in the code level
  • advantage
    • It will not affect the operational efficiency even table query
    • Additions and deletions can enhance efficiency even table
    • Easy to reconstruct the late database table
  • Shortcoming
    • Database table itself is not even detected, prone dirty data (need to avoid a strict logical management dirty data when necessary)
class Book(Base):
    name = models.CharField(max_length=64)
    price = models.DecimalField(max_digits=6, decimal_places=2)
    press = models.ForeignKey(to='Press', related_name='books', db_constraint=False, on_delete=models.SET_NULL, null=True)
    authors = models.ManyToManyField(to='Author', related_name='books', db_constraint=False)


class Press(Base):
    name = models.CharField(max_length=64)
    addr = models.CharField(max_length=64)


class Author(Base):
    name = models.CharField(max_length=64)
    # 逻辑不合理
    # author_detail = models.OneToOneField(to='AuthorDetail')


class AuthorDetail(Base):
    mobile = models.CharField(max_length=64)
    author = models.OneToOneField(to=Author, related_name='detail', db_constraint=False, on_delete=models.CASCADE)
"""
在外键字段中设置related_name: 
1.为反向查询的辅助字段名(正向查询按字段属性, 反向查询按related_name)
2.related_name可以作为字段名参加序列化类中的fields设置
"""

on_delete cascade relationship

"""
A表依赖B表, b记录删除,
    on_delete=models.CASCADE                    a记录也会被删除
    on_delete=models.DO_NOTHING                 a记录对应外键字段不受影响
    on_delete=models.SET_DEFAULT, default=1     a记录对应外键字段变为默认值
    on_delete=models.SET_NULL, null=True        a记录对应外键字段变为null
    注:多对多外键字段不能修改级联关系, 默认是 on_delete=models.CASCADE
"""

Subsequence of

  • Can only be used in the sequence of the class
  • Sequences of the field name must be a foreign key field (forward-reverse may include the related_name)
  • When the sequence of fields associated with a plurality of data need to be clearly many = True
  • Unidirectional sequence of the simple reason that, in one position, a lower position
class BookModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.Book
        fields = ['name']


class PressModelSerializer(serializers.ModelSerializer):
    # 子序列化字段
    books = BookMethodSerializer(many=True)

    class Meta:
        model = models.Press
        fields = ['name', 'addr', 'books']


        
# 使用子序列化的返回结果
{
    "status": 0,
    "msg": "ok",
    "result": [
        {
            "name": "东方出版社",
            "addr": "上海",
            "books": [
                {
                    "name": "三体"
                },
                {
                    "name": "球状闪电"
                }
            ]
        },
        {
            "name": "北方出版社",
            "addr": "北京",
            "books": [
                {
                    "name": "今日简史"
                }
            ]
        }
    ]
}


-----------------------------------------------------------------------------------------------------


# 未使用子序列化的返回结果
{
    "status": 0,
    "msg": "ok",
    "result": [
        {
            "name": "东方出版社",
            "addr": "上海",
            "books": [
                1,
                3
            ]
        },
        {
            "name": "北方出版社",
            "addr": "北京",
            "books": [
                2
            ]
        }
    ]
}        

Guess you like

Origin www.cnblogs.com/colacheng0930/p/12117258.html