2016-09-27 71 views
0

我有以下型号:如何在admin.py中显示来自相关模型的字段?

class Property(models.Model): 
    user = models.ForeignKey(User) 
    id = models.CharField(max_length=20, null=True) 

class Property_Value(models.Model): 
    id = models.ForeignKey(Property) 
    amount = models.DecimalField(max_digits = 10, decimal_places 

如何通过属性来访问Property_Value.amount管理页面上?

我有这个迄今为止...

class PropertyAdmin(admin.ModelAdmin): 
    list_display = ('id', 'user', 'property_value') 

    def property_value(self, obj): 
     return obj.id.amount 

    class Meta: 
     model = Property 

admin.site.register(Property, PropertyAdmin) 

回答

0

你互动,在这情况下,与Property对象(因为你定义在Meta) - 语法是一样的在Django别处。所以它会是obj.property_value.amount。如果您使用的是PyCharm,您可以通过告诉PyCharm“obj”是什么来获得域的自动完成功能,如下所示:

def property_value(self, obj: Property): 
相关问题