[英]Django filtering on foreign key
嗨,謝謝您的閱讀。
以下是我的數據模型的相關部分。 我想提取論壇中給定部分的所有主題。 但是我正在努力使它起作用。 這是數據模型:
class ForumSections(models.Model):
heading = models.CharField(max_length=200)
icon = models.CharField(max_length=50)
hits = models.IntegerField(default=0)
def __str__(self):
return "Section: %s" % (self.heading)
class ForumThread(models.Model):
heading = models.ForeignKey(ForumSections, on_delete=models.CASCADE)
threadTitle = models.CharField(max_length=200)
threadStatus = models.BooleanField(default=True)
def __str__(self):
return "Thread: %s Under Section: %s" % (self.threadTitle, self.heading
所以我想我想做些類似的事情:
ForumThread.objects.filter(ForumSections__heading=heading)
但是,這將返回錯誤:
django.core.exceptions.FieldError: Cannot resolve keyword 'ForumSections' into field
非常感謝您的幫助-我被困在這里。
謝謝! 湯米
這應該是
ForumThread.objects.filter(heading__heading=heading)
標題為ForumThread模型中的字段 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.