Can we get field from some some other model model having OneToOneField relion with that field using SerializerMethodField?
Thank you in advance fellow programmer.
Yes you can, its simple. Ex.
#Example account model which maps to user via one to one.
class Account(models.Model):
user=models.OneToOneField(User)
... other fields
#Example account serializer which has email field as SerializerMethodField
class AccountSerializer(serializer.Serializer):
# other serializer fields
email = serializer.SerializerMethodField()
def get_email(self, obj)
return obj.user.email
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.