django - 在管理视图中显示内联项的计数
问题描述
我想在 django 中列出的父项中显示相关项的计数。但我不断收到错误消息。
模型.py
class TblHoldings(models.Model):
item_code = models.CharField(unique=True, max_length=5)
product_name = models.CharField(max_length=45)
service_provider = models.ForeignKey(TblHoldingsServiceProviders,on_delete=models.CASCADE, related_name='service_provider',db_column='service_provider')
account_details = models.CharField(max_length=100)
purchase_cost = models.IntegerField()
current_value = models.IntegerField()
power_of = models.CharField(max_length=45, blank=True, null=True)
purchase_date = models.DateTimeField(blank=True, null=True)
goal_term = models.CharField(max_length=40, default="M",choices=FIN_GOAL_TERM)
created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
updated_at = models.DateTimeField(auto_now=True, null=True, blank=True)
def __str__(self):
return self.product_name + ' at ' + self.service_provider.provider_name
class Meta:
verbose_name = 'Holding'
verbose_name_plural = 'Holdings'
managed = True
db_table = 'tbl_holdings'
class TblHoldingsSubProducts(models.Model):
item_code = models.CharField(max_length=5, blank=True, null=True)
holding_id = models.ForeignKey(TblHoldings, default= 1,on_delete = models.CASCADE,related_name='holding_id', db_column='holding_id' )
product_name = models.CharField(max_length=45)
account_details = models.CharField(max_length=100)
purchase_cost = models.IntegerField()
current_value = models.IntegerField()
power_of = models.CharField(max_length=45)
purchase_date = models.DateTimeField(blank=True, null=True)
maturity_date = models.DateTimeField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True, null=True, blank=True)
updated_at = models.DateTimeField(auto_now=True, null=True, blank=True)
def __str__(self):
return self.product_name
class Meta:
verbose_name = 'Holdings Sub Product'
verbose_name_plural = 'Holdings Sub Products'
managed = True
db_table = 'tbl_holdings_sub_products'
管理员.py
class HoldingsSubProductsInline(admin.TabularInline):
model = TblHoldingsSubProducts
@admin.register(TblHoldings)
class HoldingsAdmin(admin.ModelAdmin):
list_display = ('item_code', 'product_name', 'service_provider', 'power_of','current_value', 'purchase_date', 'subproduct_count')
list_filter = ('goal_term', 'power_of', 'product_name', 'service_provider')
ordering = ('purchase_date',)
search_fields = ('product_name',)
inlines = [
HoldingsSubProductsInline,
]
def subproduct_count(self, obj):
return obj.tblholdingssubproducts_set.count()
错误
/admin/app_fin/tblholdings/ 'TblHoldings' 对象的 AttributeError 没有属性 'tblholdingssubproducts_set'
解决方案
推荐阅读
- nativescript - Nativescript Audio:跨页面访问播放器?
- linux - 为什么 yasm 会生成错误的调试信息?
- python - 在pygame中进行全屏显示时是否可以阻止屏幕闪烁?
- kotlin - 满足提供的条件时如何从传递给递归函数的 lambda 中断
- python - Django 中的密码更改 URL 可通过直接输入 URL 访问,但单击“更改密码”按钮时失败
- r - tidyeval:将字符串转换为 dplyr 函数中的变量
- python - 如何在目录中移动文件而不是文件夹?
- c# - 如何从 C# 调用角度包文件?
- javascript - Nodejs + Express:如何获取发出 HTTP GET 请求的元素?
- python - 从整个画布中保存一个大的海龟图形图像,而不仅仅是窗口