我有两个 Django 模型:Match 和 MatchRegister。我想获取不在 MatchRegister 对象中的所有匹配的列表,但我无法这样做。你能帮我实现它吗?
在我的两个类(class)下面
class Match(models.Model):
"""Model representing Match object"""
match_number = models.CharField(
max_length=10
)
home_team = models.ForeignKey(
Team,
on_delete=models.SET_NULL,
null=True,
related_name='home_team'
)
away_team = models.ForeignKey(
Team,
on_delete=models.SET_NULL,
null=True,
related_name='away_team'
)
match_category = models.ForeignKey(
MatchCategory,
on_delete=models.SET_NULL,
null=True
)
date_time = models.DateTimeField(
default=timezone.now
)
notes = models.TextField(
max_length=1000,
blank=True
)
last_update = models.DateTimeField(
auto_now=timezone.now
)
class MatchRegister(models.Model):
match = models.ForeignKey(
Match,
on_delete=models.SET_NULL,
null=True
)
最佳答案
您可以使用__isnull
使用正确的 lated_query_name
进行过滤(默认为小写模型名称):
Match.objects.filter(matchregister__isnull=True)
关于python - Django查询获取不在其他对象中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53961465/