1👍
✅
you can use SerializerMethodField:
https://www.django-rest-framework.org/api-guide/fields/#serializermethodfield
class FarmerSerializer(serializers.ModelSerializer):
class Meta:
model = Farmer
fields = [
'id', 'username', 'cpf',
'email', 'age', 'phone',
'sex', 'active', 'password', 'property'
]
extra_kargs = {
'cpf': {'write_only': True},
'active': {'read_only': True}
}
property = serializers.SerializerMethodField()
def get_farmers_property(self, farmer_obj):
all_farmers_property = Property.ojbects.filter(farmer = farmer_obj)
return all_farmers_property
0👍
‘Farmer’ object has no attribute ‘property’, because it has ‘property_farmer’ 🙂
in your property model you should:
farmer = models.ForeignKey(
Farmer,
related_name='property',
on_delete=models.DO_NOTHING
)
Change related_name to ‘property’ so you can access property as farmer.property and not farmer.property_farmer.
- [Answered ]-Why CSS works with "python manage.py runserver" but not with Gunicorn?
- [Answered ]-Adding Custom Permissions in django.auth.models
Source:stackexchange.com