简体   繁体   English

Django readonly_fields 不显示数据

[英]Django readonly_fields not showing data

My Orders model has a rate and total_amount field.我的Orders model 有一个ratetotal_amount字段。 I had to derive the rate from the different models so I created unit_rate .我必须从不同的模型中得出速率,所以我创建了unit_rate

Similarly, I wanted my total_amount fields to be a multiplication of quantity and unit_rate , which I tried by doing total_pay .同样,我希望我的total_amount字段是quantityunit_rate的乘积,我通过执行total_pay进行了尝试。

Here is my code:这是我的代码:

class OrderAdmin(admin.ModelAdmin):

    def unit_rate(self,obj):
        self.rate = Product.objects.get(pk=obj.order_id)
        return self.rate.price

    def total_pay(self,obj):
        self.rate = Product.objects.get(pk=obj.order_id)
        self.total_amount = self.rate.price * obj.quantity
        return self.total_amount

    list_display = ('order_id', 'order_item', 'order_status', 'delivery_address', 'customer',
        'quantity','unit_rate','total_pay')

    readonly_fields = ('total_pay','unite_rate')

admin.site.register(Orders,OrderAdmin)

As total_pay and unit_rate are obtained from other columns, the admin doesn't need to enter them.由于total_payunit_rate是从其他列中获取的,因此管理员无需输入它们。 That's why I kept them on readonly_fields .这就是我将它们保留在readonly_fields上的原因。

The problem is that, whenever it creates an order through the admin interface, total_pay and unit_rate do not show up in the admin.问题是,每当它通过管理界面创建订单时, total_payunit_rate不会显示在管理中。 It just shows a dash like this: - .它只显示这样的破折号: -

It has been doing this for a while, so I could really use your help.它已经这样做了一段时间,所以我真的可以使用你的帮助。 Thanks.谢谢。

Actually digging little bit more for hours i found the solution.实际上挖掘了几个小时我找到了解决方案。

class OrderAdmin(admin.ModelAdmin):

    list_display = ('order_id', 'order_item', 'order_status', 'delivery_address', 'customer',
        'quantity','unit_rate','total_pay')

    def unit_rate(self,obj):
        rate = Product.objects.get(pk=obj.order_item.product_id)
        return rate.price

    def total_pay(self,obj):
        rate = Product.objects.get(pk=obj.order_item.product_id)
        total_amount = rate.price * obj.quantity
        return total_amount

admin.site.register(Orders,OrderAdmin)

Actually i did not know how to pass product_id along with order_item .实际上我不知道如何将product_idorder_item一起传递。 and i think even documentation is also does not help much.而且我认为即使是文档也无济于事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM