2016-10-13 63 views
0

我有幾個模型將用戶作爲外鍵。例如,我有型號Profile,型號profilePic和型號userQuestions - 在所有型號中,用戶都是外鍵。DRF - 將不同的模型序列化爲一個

有沒有一種方法可以得到一個profileprofilePicuserQuestions對應於給定的用戶在一個單一的JSON響應?

我的models.py是繼

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, 
    }) 

,但現在它說對象 '用戶' 是不是可重複的

+0

是的,但你必須張貼您的models.py – itzMEonTV

+0

@itzmeontv做 –

回答

0

您可以添加他們爲nested serializers到您的UserSerializer。像這樣的東西。

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__' 
+0

試圖獲得價值領域'profile_pic'上串時,我得到的「GOT AttributeError的錯誤'UserSerializer'。 串行器領域可能會被命名爲不正確並且不匹配'User'實例上的任何屬性或鍵 原始異常文本是:「User」對象沒有屬性「profile_pic」 –

+0

@AlexeyK嘗試'profilepic'而不是'profile_pic' –

+0

試過了,結果是一樣的 –

0

用大約一個月同樣的問題掙扎前並沒有發現很好的答案上SO。 Sardorbek的答案是完全正確的,如果你不在乎JSON響應是否平坦。通過該代碼創建JSON是一樣的東西:

{ 
    "profile": {"name": ..., "gender": ..., "birthday": ...,}, 
    "profile_pic": {"profilePic": ...., }, 
    "user_questions": [{...}, {...}, ...] 
    User model attributes go here... 
} 

,但如果你希望它是平的,如:

{ 
    "name": ..., 
    "gender": ..., 
    "birthday": ..., 
    .... 
    "profilePic": ..., 
    "user_questions": [{...}, {...}, ...], 
    User model attributes go here... 
} 

你需要在這裏一點點的黑客。覆蓋to_internal_valueUserSerializer類是這樣的:

def to_internal_value(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_internal_value({ 
     'profile': profile_info, 
     'profile_pic': profile_pic_info, 
    }) 

您可能需要重寫to_representation也有類似的想法。

+0

試圖將此添加到序列化程序,但它只返回元類 –

+0

中指定的用戶模型字段也嘗試覆蓋to_representation - 添加問題 –

相關問題