其他分享
首页 > 其他分享> > django-ForeignKey,OneToOneField,ManyToManyField

django-ForeignKey,OneToOneField,ManyToManyField

作者:互联网

进入到django自带的related.py中,可以看到

1.ForeignKey

初始化的参数有:

to, on_delete, related_name=None, 
related_query_name=None,limit_choices_to=None,
parent_link=False, to_field=None,
db_constraint
=True, **kwargs

假设我们有:

class Author(models.Model):
    author = models.CharField(max_length=250)
 
class Books(models.Model): 
    book = models.ForeignKey(to='Author',on_delete=models.CASCADE,to_field='id')

to:被关联表的名称

on_delete:删除带有外键的信息时,定义了删除的操作:

to_field:被关联的字段,一般是主键,也可以是值唯一的字段

2.OneToOne

初始化参数有:

to, on_delete, to_field=None, **kwargs

3.ManyToMany

初始化参数有:

to, related_name=None, related_query_name=None,
limit_choices_to=None, symmetrical=None, through=None,
through_fields=None, db_constraint=True, db_table=None,
swappable=True, **kwargs

 

 

标签:None,name,删除,ForeignKey,models,related,ManyToManyField,True,OneToOneField
来源: https://www.cnblogs.com/xiximayou/p/11778048.html