DRF - 将不同型号序列化为一体 [英] DRF - serialize different models into one

查看:123
本文介绍了DRF - 将不同型号序列化为一体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个型号的用户作为外键。例如,我有模型个人资料,模型 profilePic 和模型 userQuestions - 所有模型中的用户都是外键。

I have several models that have a user as a foreign key. For example, I have model Profile, model profilePic and model userQuestions - in all that models user is foreign key.

有没有办法可以得到一个个人资料 ,一个对应给给定用户的 profilePic userQuestions

Is the there a way a can get a profile, a profilePic and userQuestions that corresponds to given user in one single json response ?

我的models.py正在追踪

my models.py is following

class Profile(models.Model):
   user = models.OneToOneField(User, on_delete=models.CASCADE)
   gender = models.CharField(max_length=2)
   name = models.CharField(max_length=200)
   birthday = models.DateField(auto_now=False, auto_now_add=False)
   weight = models.IntegerField(default=0)
   heigth = models.IntegerField(default=0)
   sign = models.CharField(max_length=200, choices=SIGNS_CHOICES, default='E')
   orientation = models.CharField(max_length=200, choices=ORIENTATION_CHOICES, default='E')
   bodytype = models.CharField(max_length=200, choices=BODYTYPE_CHOICES, default='E')
   education = models.CharField(max_length=200, choices=EDUCATION_CHOICES, default='E')
   religion = models.CharField(max_length=200, choices=RELIGION_CHOICES, default='E')
   smoking = models.CharField(max_length=200, choices=SMOKING_CHOICES, default='E')
   alcohol = models.CharField(max_length=200, choices=ALCOHOL_CHOICES, default='E')
   kids = models.CharField(max_length=200, choices=KIDS_CHOICES, default='E')
   pets = models.CharField(max_length=200, choices=KIDS_CHOICES, default='E')
   location = models.CharField(max_length=100)
   latitude = models.FloatField()
   longtitude = models.FloatField()

class ProfileFields(models.Model):
   user = models.ForeignKey(User, on_delete=models.CASCADE)
   title = models.CharField(max_length=200)
   text = models.TextField()
   order = models.IntegerField(default=0)

class ProfilePic(models.Model):
   user = models.OneToOneField(User, on_delete=models.CASCADE)
   profilePic = models.ImageField(upload_to='Images/', default='Images/None/No-img.jpg')

class Pics(models.Model):
   user = models.ForeignKey(User, on_delete=models.CASCADE)
   pic = models.ImageField(upload_to='Images/', default='Images/None/No-img.jpg')

UPD

尝试添加

def to_representation(self, data):
    profile_info = {kv: data[kv] for kv in data if kv in list(ProfileSerializer.Meta.fields)}
    profile_pic_info = {kv: data[kv] for kv in data if kv in list(ProfilePicSerializer.Meta.fields)}

    return super(DeviceInfoSerializer, self).to_representation({
    'profile': profile_info,
    'profile_pic': profile_pic_info,
    })

但现在它表示对象'User'不可迭代

but now it says object 'User' is not iteratable

推荐答案

您可以将它们添加为嵌套序列化器到您的 UserSerializer

You can add them as nested serializers to your UserSerializer. Something like this.

class ProfileSerializer(serializers.ModelSerializer)
    class Meta:
        model = Profile
        fields = '__all__'

class ProfilePicSerializer(serializers.ModelSerializer)
    class Meta:
        model = ProfilePic
        fields = '__all__'

class UserQuestionsSerializer(serializers.ModelSerializer)
    class Meta:
        model = UserQuestions
        fields = '__all__'

class UserSerializer(serializers.ModelSerializer)
    profile = ProfileSerializer(many=False)
    profilepic = ProfilePicSerializer(many=False)
    user_questions = UserQuestionsSerializer(mane=True)

    class Meta:
        model = User
        fields = '__all__'

这篇关于DRF - 将不同型号序列化为一体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆