首页 > 解决方案 > Django 模型将 2 个 Foreinkeys 保存到同一个模型

问题描述

按照此链接和文档,它仍然无法以某种方式保存具有 2 个 Foreinkeys 的对象。

class Photo(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, null=True) #User.photo_set.all() returns all Photo objects of the photo
    photoURL = models.CharField(max_length=256, null=True)
    secondPhoto = models.OneToOneField('self', on_delete=models.PROTECT, null=True, blank=True)
    timestamp = models.DateTimeField(auto_now_add=True)
    description = models.CharField(max_length=1000, null=True)
    is_private = models.BooleanField(default=False)



class Clash(models.Model):
    win_photo = models.ForeignKey(Photo,on_delete=models.PROTECT, related_name="wins", null=True)
    loss_photo = models.ForeignKey(Photo,on_delete=models.PROTECT, related_name="losses", null=True)
    is_private = models.BooleanField(default=False) #we will filter those out for user quality calculations

在此处输入图像描述

标签: django-modelsforeign-keys

解决方案


推荐阅读