2

I am messed up with serializing multiple objects. I tried some solutions from stackoverflow but none of them worked for me. please help me to solve this.

models.py

class NewsCategories(models.Model):

    category = models.CharField(default="",max_length=30)
    def __str__(self):
        return self.category

class TrendingTopics(models.Model):
    
    topics = models.CharField(default="",max_length=30)
    def __str__(self):
        return self.topics


class UserNewsIntrests(models.Model):
    user = models.OneToOneField(NewsreaderUserModel,on_delete=models.CASCADE)
    user_news_categories = models.ManyToManyField(NewsCategories)
    user_topics = models.ManyToManyField(TrendingTopics)
    def __str__(self):
        return self.user.user_email

serializers.py

class NewsCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = NewsCategories
        fields = '__all__'

class TrendingTopicsSerializer(serializers.ModelSerializer):
    class Meta:
        model = TrendingTopics
        fields = '__all__'
        
class NewsIntrestSerializer(serializers.ModelSerializer):
    user_news_categories = NewsCategorySerializer(many=True) 
    user_topics = TrendingTopicsSerializer(many=True)
        
    class Meta:
        model = UserNewsIntrests
        fields = ['user_news_categories','user_topics','user']
        
    

        

what i m getting this time on read operation

{
    "profile": {
        "user_email": "[email protected]",
        "first_name": "test",
        "last_name": "user",
        "age": 21,
        "last_active": "2021-04-28T14:21:08.613142Z",
        "is_active": true,
        "profile_image": "/media/profile_images/default/profile.png"
    },
    "news_intrest": {
        "user_news_categories": [
            {
                "id": 1,
                "category": "Technology"
            },
            {
                "id": 2,
                "category": "Business"
            }
        ],
        "user_topics": [
            {
                "id": 1,
                "topics": "Putin"
            }
        ],
        "user": "[email protected]"
    }
}

What i want in create request?

Actually i want to create a newsintrest for a user. All the news categories and topics are already in database. i can assign categories and topics to a user via admin pannel but i want it to be done on user side also. I am totally confused how to do it please help me.

What i am passing (json)?

{
    'user_news_category': [{'category': 'Business'}, {'category': 'Sports'}, {'category': 'Health'}],
    'user_topics': [{'topics': 'Narendra Modi'}], 
    'user': '[email protected]'
}

I want this json data to be created in newsintreset but i am stucked how to do it.

admin pannel view

1 Answer 1

1

if you use modelViewSet you must be pass data like this

{
    'user_news_category': [1,2,3], # id of category
    'user_topics': [1], 
    'user': 1 # if email is your primary key pass email
}

but if you use APIView i must be see your code in view side

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.