我正在尝试使用以下代码在模型中创建新条目:
ContactRelation.objects.create(
lead=True, event=request.event, contact=contact
)
但是,我总是遇到这样的错误信息:
duplicate key value violates unique constraint "contacts_contactrelation_pkey" DETAIL: Key (id)=(14) already exists.
我以为Django会自己数pk。pk=14已经存在。如果我写信
ContactRelation.objects.create(
pk=199, lead=True, event=request.event, contact=contact
)
然后我的条目被成功创建。ContactRelation
与extra fields充当多对多关系表。你明白Django为什么要这样处理pks
吗?你知道吗
class ContactRelation(TimeStampedModel):
"""
Stores data about individual events the contact interacted with.
:param event: ForeignKey to the event.
:type organizer: Event
:param contact: ForeignKey to the contact.
:type contact: Contact
:param lead: If true, contact used the sign up form.
:type lead: bool
:param attendee: Contact assigned to ticket as attendee.
:type attendee: bool
:param purchaser: Contact made an order for the respective event.
:type purchaser: bool
"""
event = models.ForeignKey(
'events.Event', related_name='contact_relation', on_delete=models.CASCADE
)
contact = models.ForeignKey(
Contact, related_name='contact_relation', on_delete=models.CASCADE
)
lead = models.BooleanField(
verbose_name='Lead', default=False
) # Contact who 'Signed Up'
attendee = models.BooleanField(
verbose_name='Attendee', default=False
) # Contact assigned to ticket
purchaser = models.BooleanField(
verbose_name='Purchaser', default=False
) # Contact made the order
class Meta:
unique_together = [['event', 'contact']]
目前没有回答
相关问题 更多 >
编程相关推荐