5

I have a big misunderstanding with DRF nested serializers. I read docs about this and found out that I need to provide my own update method. So, here it is:

class SkillsSerializer(serializers.ModelSerializer):

class Meta:
    model = Skills

class ProfileSerializer(serializers.ModelSerializer):
    skills = SkillsSerializer(many=True)

    class Meta:
        model = Profile
        fields = ('user', 'f_name', 'l_name', 'bd_day', 'bd_month', 'bd_year', 'spec', 'company', 'rate', 'skills', 'bill_rate', 'website', 'about', 'city', 'avatar', 'filled')

    def update(self, instance, validated_data):
        instance.user_id = validated_data.get('user', instance.user_id)
        instance.f_name = validated_data.get('f_name', instance.f_name)
        instance.l_name = validated_data.get('l_name', instance.l_name)
        instance.bd_day = validated_data.get('bd_day', instance.bd_day)
        instance.bd_month = validated_data.get('bd_month', instance.bd_month)
        instance.bd_year = validated_data.get('bd_year', instance.bd_year)
        instance.spec = validated_data.get('spec', instance.spec)
        instance.company = validated_data.get('company', instance.company)
        instance.rate = validated_data.get('rate', instance.rate)
        instance.website = validated_data.get('website', instance.website)
        instance.avatar = validated_data.get('avatar', instance.avatar)
        instance.about = validated_data.get('about', instance.about)
        instance.city = validated_data.get('city', instance.city)
        instance.filled = validated_data.get('filled', instance.filled)
        instance.skills = validated_data.get('skills', instance.skills)
        instance.save()
        return instance

I compared it with docs and didn't found any difference. But in this case, when I try to update skills, it doesn't work. And there is a real magic: when I put this

instance.skills = validated_data.get('bd_day', instance.skills)

It works PERFECTLY WELL! For ex., if I put bd_day = 12, update method saves instance with skills with ID's 1 and 2. So, it seems like serializer ignores skills from AJAX data and still thinking, that skills serializer is read_only. So, what is a point of this logic and how I can finally update my skills?

UPDATE

My models:

class Skills(models.Model):
    tags = models.CharField(max_length='255', blank=True, null=True)

    def __unicode__(self):
        return self.tags


class Profile(models.Model):
     user = models.OneToOneField(User, primary_key=True)
     ...
     skills = models.ManyToManyField(Skills, related_name='skills')
     ...

UPDATE2

Still doesn't have any solution for this case! I tried this and this - the same result. It seems that serializer ignored JSON data at all.

4
  • What are the fields of the model Skills and what are the data types of each field? Commented Nov 8, 2015 at 17:19
  • @ArpitGoyal - I updated my question. Commented Nov 8, 2015 at 17:23
  • What data are you sending to the API point ? Commented Nov 8, 2015 at 17:59
  • @Linovia Actually I tried to use several ways, but in the end I send just a string with skills ID's. Like this: 123, where id1=1, id2=2 etc. Commented Nov 8, 2015 at 18:10

2 Answers 2

3

I had to update the answer, you did it inefficient way, so see the solution, it's far better

class ProfileSerializer(serializers.ModelSerializer):

    class Meta:
        model = Profile
        fields = ('user', 'f_name', ... 'skills', ... 'filled')
        depth = 1

class ProfileUpdateSerializer(serializers.ModelSerializer):
    skills = serializers.PrimaryKeyRelatedField(many=True, queryset=Skills.objects.all(), required=False)

    class Meta:
        model = Profile
        fields = ('user', 'f_name', ... 'skills', ... 'filled')
    
    def update(self, instance, validated_data):
        user = validated_data.pop('user', {})
        for attr, value in validated_data.items():
            setattr(instance, attr, value)
        instance.save()

        if user:
            User.objects.filter(id=self.context['request'].user.id).update(**user)


        return instance

But after that I had another issue. I can received only one element from array of skills. And I found solution here:

 $.ajax({
    url: myurl,
    type: 'PUT',
    dataType: 'json',
    traditional: true,<-----THIS!
    data: data,

And that's it! It works like a charm!

I hope, my solution will be useful!

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

1 Comment

I am stuck with the same problem, but I don't think creating two serializers is ideal according to DRY. Any new ideas?
1

You have an issue here as you're providing non model data.

this:

    instance.skills = validated_data.get('skills', instance.skills)

Will not provide Skill model instances but a dictionary. You need to get the skills instance first and then inject them back to the instance.skills.

8 Comments

Could you please give an example how it must to be looks like?
The documentation already provides one: django-rest-framework.org/api-guide/relations/…
It is, but I didn't found an example for update method for many-to-many without through table.
That's because there are too many use case. Would you rather update or create a new related instance when some data change ? What about the removal policy ? and so on
I still doesn't have any solution for this case. I tried to use this advice link, but it didn't help. I can't understood how I can get Skills() instance if I can't receive it from JSON?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.