ORM自定義第三張表註意
阿新 • • 發佈:2017-09-22
members cas eas let sca aso ember tom 通過
from django.db import models
from django.db import models
class Person(models.Model):
‘‘‘人‘‘‘
name = models.CharField(max_length=50)
class Group(models.Model):
‘‘‘組‘‘‘
name = models.CharField(max_length=128)
members = models.ManyToManyField(
Person,
through=‘Membership‘,
through_fields=(‘group‘, ‘person‘),
)
class Membership(models.Model):
‘‘‘
會員
‘‘‘
group = models.ForeignKey(Group, on_delete=models.CASCADE)
person = models.ForeignKey(Person, on_delete=models.CASCADE)
inviter = models.ForeignKey(
Person,
on_delete=models.CASCADE,
related_name="membership_invites",
)
invite_reason = models.CharField(max_length=64)
through_fields=(‘group‘, ‘person‘), 通過後端返回的是Membership 註意
(‘group‘, ‘person‘) 第一個字段指向自己。第二字段指向別人
ORM自定義第三張表註意