table of Contents
basic concepts
Base table: abstract form, is designed to inherit, to provide the public field, itself does not complete database migration
class BaseModel(models.Model):
is_delete = models.BooleanField(default=False)
created_time = models.DateTimeField(auto_now_add=True)
class Meta:
# 基表,为抽象表,是专门用来和继承,提供公有字段,自身不会完成数据库迁移
abstract = True
Off the table associated relationship
- It will not affect the operational efficiency even table query
- Will improve operational efficiency even table CRUD
- Easy to reconstruct the late database table
- Disadvantage that the database itself is not detected even table, easily dirty data is present, the parameters necessary to avoid a dirty by strict logical
- A dependent B, A first insertion recording, the recording of the record corresponding to the United States B produced in the absence of the associated case, the operation can be achieved, but the data is dirty; data B is then added and then, the dirty data to be processed. After the first turn operation B procedure A, satisfies more logical thinking, the same can be performed. By the logic will be even table lookup table AB, there will not be any exception
Establish table relationships
- Book tables and tables are many Publish: foreign key in a multi-party Book
- And many-Book Author: foreign key in one of the high frequency queries Book
- Author and AuthorDetail one: To establish a foreign key in the right position according to the actual needs of AuthorDetail
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)
Foreign key field attribute
- Related_name foreign key in the foreign key field name in the reverse lookup: Forward search field names, check the reverse related_name
- on_delete foreign key must be provided to indicate to cascade, in Django 1.x, the system provides default (value models.CASCADE), the Django 2.x, must be manually clear
- CASCADE: The default value, cascade
- DO_NOTHING: foreign keys are not concatenated, it is assumed in Table A Table B dependent, record deletion B, Table A foreign key field without any treatment
- SET_DEFAULT: Suppose dependent Table B Table A, B record deletion, Table A foreign key field to the value of the default attribute settings, default attribute must be used with
- SET_NULL: Suppose dependent Table B Table A, B record deletion, Table A foreign key field value is null, null = True must be used to use properties
- On_delete-many fields can not be set to cascade, cascading default, if you want to deal with a cascade relationship, you need to manually clear the relationship between tables, table handling multiple foreign keys
- db_constraint the foreign key table associated with the control, indicates that the associated default is True, False represented disassociation provided
## sequences of:
- It can only be used in serialization
- Field name must be the foreign key field
- With respect to the custom sequence Outsider key fields, custom serialization fields are not involved in anti-serialized, and the child must be serialized foreign key name, so we can not put in storage
- Foreign key association data is redundant, need to be clear
many=True
- Sequences of a one-way operation, because as a type of sub-sequence must be written at the top, it can not produce the reverse direction of the sequence
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']