简体   繁体   English

SerializerMethodField:从其他一些 Model 获取字段

[英]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?我们可以使用 SerializerMethodField 从其他一些 model model 获得与该字段具有 OneToOneField 相关性的字段吗?

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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