6

I have written basic model serializers in Django where the api mimics the data model. I now have a requirement to store User Preference in database. The api contains an array.

My User Models :

class User(models.Model):
    email_id = models.EmailField(max_length=80, blank=True, primary_key=True)

class UserPreference(models.Model)
    email_id = models.ForeignKey('User')
    preference = models.CharField(maxlength=20)

An ideal json post request would look something like this

{
    email:"[email protected]"
    preference : [ 'books', 'food', 'lifestyle', 'travel']
}

I wish to save this json schema to the UserPreference model. This requires multiple inserts for preference. What will be a good serializer design for it ?

I tried

class UserPreferenceSerializer(serializers.ModelSerializer):
    class Meta:
        model = UserPreference
        fields = ('email_id', 'preference')
5
  • 1
    Is the Customer model related to User in any way? Commented Apr 6, 2015 at 22:04
  • @mariodev Thanks for suggesting. I have edited customer to User. Commented Apr 7, 2015 at 6:41
  • You should write own model for preferences and use many-to-many field from UserPreference, or ForeignKey from the preference model. Commented Apr 7, 2015 at 19:22
  • @jaakko That's the correct behaviour I believe but I have stripped down my actual large use case to the part where I'm having a problem. Commented Apr 9, 2015 at 2:25
  • Did either of the answers work? Commented Apr 10, 2015 at 21:17

2 Answers 2

9

you could use StringRelatedField of Django Rest Framework.

Make below changes and you will get the response in the way you want.

models.py (put related_name there)

class UserPreference(models.Model):
    email_id = models.ForeignKey(User, related_name='preference')
    preference = models.CharField(maxlength=20)

serializers.py

class UserSerializer(serializers.ModelSerializer):
    preference = serializers.StringRelatedField(many=True)

    class Meta:
        model = User
        fields = ('email_id', 'preference')
Sign up to request clarification or add additional context in comments.

Comments

7

You could make your model like this:

class UserPreference(models.Model)
    email_id = models.ForeignKey('User')
    preference = models.ManyToManyField('Preference') #create model for preferences

Then add custom create method to your serializer:

def create(self, validated_data):
    user = self.context.get('user') #you can pass context={'user': self.request.user} in your view to the serializer
    up = UserPreference.objects.create(email_id=user)
    up.save()        
    preference = validated_data.get('preference', [])
    up.preference.add(*preference)
    up.save()
    return up

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.