假设你为Transaction、Category和TransactionDetail定义了模型,如下所示:
class Transaction(models.Model):
details = models.ForeignKey(TransactionDetails, verbose_name="Transaction Details", on_delete=models.PROTECT, default='',null=True)
category = models.ForeignKey(Category, verbose_name="Transaction Category", on_delete=models.PROTECT, default='')
class Category(models.Model):
name = models.CharField(max_length=255, verbose_name="Category Name", default="")
class TransactionDetails(models.Model):
value = models.CharField(max_length=255, verbose_name="Transaction Detail", default="")
在www.example.com中views.py我试图弄清楚如何过滤附加到Transaction.details字段并具有特定Transaction.Category.name的TransactionDetails对象。换句话说,我想返回一个transactionDetails的查询集,它与Transaction对象相关,category.name为“uncategorized”。
我理解如何使用对象管理器过滤器的相反方式:
uncatTransactions = Transaction.objects.filter(category__name="Uncategorized")
但我不确定这将如何为TransactionDetails完成:
uncategorizedTrxDetails = TransactionDetails.objects.filter(referenceToParentUsingForeignKey__category__name="Uncategorized")
1条答案
按热度按时间bjp0bcyl1#
您使用以下过滤器: