我在django模型中遇到关系概念的问题。
让我们看看我的例子:
我将这个表/类放入models.py:
class PacketEventsInformation(models.Model):
ID_IP_Source = models.<Relationship>(Ips, on_delete=models.CASCADE)
ID_IP_Dest = models.<Relationship>(Ips, on_delete=models.CASCADE)
ID_Source_Port = models.<Relationship>(Ports, on_delete=models.CASCADE)
ID_Dest_Port = models.<Relationship>(Ports, on_delete=models.CASCADE)
Protocol = models.CharField(max_length=20)
ID_Source_MAC = models.<Relationship>(Macs, on_delete=models.CASCADE)
ID_Dest_MAC = models.<Relationship>(Macs, on_delete=models.CASCADE)
RAW_Info = models.TextField()
TAG = models.ForeignKey(Tags, on_delete=models.CASCADE)
def __str__(self):
return '%s' % self.id
此时,我定义了所有ID字段和其他表/类(Pkey)的ID字段之间的关系,如ForeignKey。
好吧,如果我执行迁移到我的终端,我明白了:
./manage.py migrate
app.PacketEventsInformation.ID_Dest_MAC: (fields.E304) Reverse accessor for 'PacketEventsInformation.ID_Dest_MAC' clashes with reverse accessor for 'PacketEventsInformation.ID_Source_MAC'.
......
我理解ManyToMany,OneToOne和OnetoMany(外键)的定义,但我不知道为什么不能这样做。也许答案可能是创建具有该Fkeys的中间表或在该ID之间放置OneToOne关系....
感谢您的回答=)
PD:
ID_IP_Source / Dest可以是相同的
ID_Source / Dest_Port可以是相同的
ID_Source / Dest_MAC可以是相同的
答案 0 :(得分:2)
在django中,如果有多个外键指向同一个模型,则需要使用related_name
来区分它们:
ID_IP_Source = models.<Relationship>(Ips, on_delete=models.CASCADE, related_name="id_ip_source")
ID_IP_Dest = models.<Relationship>(Ips, on_delete=models.CASCADE, related_name="id_ip_dest")