如何查找不在OneToOneField右侧的项目?

时间:2019-04-05 15:06:22

标签: python django

我有以下两种模型:

class Location(models.Model):
    plate = models.OneToOneField(Plate, default=None, on_delete=models.CASCADE, null=True, blank=True)

class Plate(models.Model):
    id = models.CharField(primary_key=True, max_length=100)

如您所见,Plate上只能连接一个Location。但是如何查找未分配给Plate的所有Location呢?

我可以这样数:

models.Plate.objects.count() - models.Location.objects.filter(plate__isnull=False).count()

但是我需要Plate的列表,而不仅仅是数字。我尝试在线搜索文档中的内容,但实际上我不知道要搜索什么。而且,我没有看到如何使用Q对象或filter方法来做到这一点。

0 个答案:

没有答案