0

I have the following models:

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

How do I access the Property_Value.amount via Property on the admin page?

I have this so far...

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)
H C
  • 1,138
  • 4
  • 21
  • 39

1 Answers1

0

You're interacting, in that instance, with a Property object (since you defined that in the Meta) - syntax is the same as elsewhere in Django. So it would be obj.property_value.amount. If you're using PyCharm, you can get autocomplete for the field by telling PyCharm what 'obj' is, like so:

def property_value(self, obj: Property):
Jens Astrup
  • 2,415
  • 2
  • 14
  • 20