无法查询“ ABC”:必须是“ Image”实例

时间:2018-10-10 16:56:46

标签: django django-models

我在model.py中遇到一条错误消息。如果你们能在这方面给我一些帮助,我将不胜感激。以下是model.py的一部分:

class WorkJob(models.Model):
    id = models.AutoField(primary_key=True)
    share = models.ForeignKey(FShare, on_delete=models.PROTECT)
    aftId = models.ForeignKey(AftId, null=True, blank=True, on_delete=models.PROTECT)
    history = HistoricalRecords()

    def __str__(self):
        if self.aftId:
            return self.aftId.aft
        else:
            return str('AFT-NA')

class Image(models.Model):
    id = models.AutoField(primary_key=True)
    imagingJob = models.OneToOneField(WorkJob, on_delete=models.PROTECT)
    md5 = models.CharField(max_length=32, null=True, blank=True)
    originalCopy = models.ForeignKey(Disc, related_name='originalCopy', null=True, blank=True, on_delete=models.PROTECT)
    workingCopy = models.ForeignKey(Disc, related_name='workingCopy', null=True, blank=True, on_delete=models.PROTECT)
    history = HistoricalRecords()

    def __str__(self):
        return self.imagingJob.fileShare.identifier

class Copy(models.Model):
    id = models.AutoField(primary_key=True)
    image = models.ForeignKey(Image, on_delete=models.PROTECT)
    disc = models.ForeignKey(Disc, on_delete=models.PROTECT, related_name='copy')
    history = HistoricalRecords()

    def aftId(self):
        return self.image.imagingJob.aftId.aft

下一堂课是我遇到的问题。

class TFI(models.Model):
    id = models.AutoField(primary_key=True)
    createDate = models.DateTimeField(auto_now_add=True, null=True)
    status = models.IntegerField(choices=STATUS_OPTIONS, default=0)
    history = HistoricalRecords()

    def check_third(self):
        if self.status == 5:
            im = 0
            third_imajob = WorkJob.objects.filter(share=self.share)
            for ima in third_imajob:
                if Copy.objects.filter(image__exact=ima.aftId).exists():
                    # some code blablabla
                else:
                    break

错误消息指出有问题的行是:

if Copy.objects.filter(image__exact=ima.aftId).exists():

我不确定为什么要说实例必须与Image一起使用。该行显然是从类Copy和WorkJob中提取的。我确实看到Copy.image具有对类Image的外键引用,但是我不确定如何解决此问题。预先感谢!

编辑:以下也是代码的一部分,并且上面的代码也已添加。

class AftId(models.Model):
    id = models.AutoField(unique=True, primary_key=True)
    aft = models.CharField(unique=True, max_length=30)
    assignedTo = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.PROTECT)
    history = HistoricalRecords()

    def __str__(self):
        return self.aft

1 个答案:

答案 0 :(得分:0)

您正在尝试比较一些aftID实例和Image

我看不到您的aftId模型,但我想它具有图像外键字段,因此您的查询应该为Copy.objects.filter(image__exact=ima.aftId.image).exists()