简体   繁体   中英

SerializerMethodField: Get field from some other Model

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.

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