Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Direct assignment to the reverse side of a related set is prohibited. Use addresses.set() instead

I am going to create and update nested relationships but it is not working as expected I have a three models User, Profile and Address. Profile model is a FK in Address model

profiles/models.py

class Address(models.Model):
   profile = models.ForeignKey(Profile, related_name='addresses', on_delete=models.CASCADE)
   country = models.CharField(max_length=255)
   city = models.CharField(max_length=255)
   state = models.CharField(max_length=100)
   zip_code = models.CharField(max_length=50)
   address = models.CharField(max_length=1000)

profiels/serializers.py

class ProfileSerializer(serializers.ModelSerializer):
    addresses = AddressSerializer(many=True)

class Meta:
    model = Profile
    fields = ['gender', 'date_of_birth', 'hometown', 'phone', 'addresses']

def create(self, validated_data):
    addresses_data = validated_data.pop('addresses')
    profile = Profile.objects.create(**validated_data)
    for address_data in addresses_data:
        Address.objects.create(profile=profile, **address_data)
    return profile

users/serializers.py

class UserSerializer(serializers.HyperlinkedModelSerializer):
profile = ProfileSerializer(required=True)
class Meta:
    model = User
    fields = ['id', 'url', 'first_name', 'last_name', 'email', 'password', 'profile']
    extra_kwargs = {'password': {'write_only': True}}

def create(self, validated_data):
    profile_data = validated_data.pop('profile')
    password = validated_data.pop('password')
    user = User(**validated_data)
    user.set_password(password)
    user.save()
    Profile.objects.create(user=user, **profile_data)
    return user

def update(self, instance, validated_data):
    profile_data = validated_data.pop('profile')
    profile = instance.profile
    instance.email = validated_data.get('email', instance.email)
    instance.first_name = validated_data.get('first_name', instance.first_name)
    instance.last_name = validated_data.get('last_name', instance.last_name)
    instance.save()

    profile.gender = profile_data.get('gender', profile.gender)
    profile.date_of_birth = profile_data.get('date_of_birth', profile.date_of_birth)
    profile.hometown = profile_data.get('hometown', profile.hometown)
    profile.phone = profile_data.get('phone', profile.phone)
    profile.addresses = profile_data.get('addresses', profile.addresses)
    profile.save()

It is working very well with user and profile I can create and update but when I add addresses it gives me error above shown in the title. I do not know yet how to solve nested inside nested relationships. Any help would be appreciated) Thanks beforehand!

like image 574
coder Avatar asked Aug 08 '19 06:08

coder


1 Answers

Your issue is with this line

profile.addresses = profile_data.get('addresses', profile.addresses)

As the error message suggests, direct assignment to the reverse side of a relation is not allowed. What you want to do is use set() instead.

profile.addresses.set([list of new addresses])

https://docs.djangoproject.com/en/dev/ref/models/relations/#django.db.models.fields.related.RelatedManager.set

like image 154
jpm Avatar answered Nov 04 '22 13:11

jpm