python - 如何根据其他非相关模型过滤django模型
问题描述
请参考下面的代码
交易模型
class Transaction(models.Model)
current_product_code = models.CharField(....)
previous_product_code = models.CharField(....)
@property
def status(self):
c_price = Product.objects.get(code=self.current_product_code).price
p_price = Product.objects.get(code=self.previous_product_code).price
if c_price == p_price:
return "Due"
elif c_price > p_price:
return "Upgrade"
else:
return "Downgrade"
产品型号
class Product(models.Model):
code = models.CharField(....)
price = models.DecimalField(....)
我的问题:如何获取/过滤具有升级/降级/到期状态的交易。我正在尝试创建一个自定义管理过滤器,该过滤器根据交易状态过滤交易,但我无法在 .filter() 中放入什么,请检查以下方法
def queryset(self, request, queryset):
value = self.value()
if value == 'Upgrade':
return queryset.filter(***** HERE *****)
elif value == 'Downgrade':
return queryset.filter(***** HERE *****)
elif value == 'Unknown':
return queryset.filter(***** HERE *****)
return queryset
解决方案
你真的应该使用ForeignKey
between Product
and Transaction
(for both: current_product_code
and previous_product_code
)。这将允许您轻松地在查询集中使用这些关系。
我提出的模型结构如下所示:
class Product(models.Model):
code = models.CharField(....)
price = models.DecimalField(....)
class Transaction(models.Model)
# You have to define related_name for at least one of relations below.
# Without that, automatically generated ones will clash.
# Also don't foget to change `on_delete` to suit your needs.
current_product = models.ForeignKey(Product, related_name="current_transactions", on_delete=models.CASCADE)
previous_product = models.ForeignKey(Product, related_name="previous_transactions", on_delete=models.CASCADE)
@property
def status(self):
# also, no need to do additional queries here manually. You can improve
# it further by using `select_related` when querying for transactions.
c_price = self.current_product.price
p_price = self.previous_product.price
if c_price == p_price:
return "Due"
elif c_price > p_price:
return "Upgrade"
else:
return "Downgrade"
使用该模型结构,查找特定类型的交易将更容易:
upgrade_transactions = Transaction.objects.filter(current_product__price__gt=F('previous_product__price'))
downgrade_transactions = Transaction.objects.filter(current_product__price__lt=F('previous_product__price'))
due_transactions = Transaction.objects.filter(current_product__price=F('previous_product__price'))
推荐阅读
- wordpress - 临时 dns 错误 inet_e_resource_not_found
- asp.net-mvc - 如何在 .net mvc 中禁用模型视图复选框?
- ionic-framework - 当离子搜索栏上没有文本时,列表应该隐藏吗?
- swift - 编辑 tableView 后如何更新模型
- windows - 获取总数的百分比
- vba - Application.Cells VS Application.ActiveSheet.Cells
- css - 离子4改变输入颜色
- docker - 不能在 docker pull/push 上包含客户端证书
- javascript - 推送到 JS 对象
- ios - 如何提高 Tesseract 结果质量?