I want to update records that are related with foreign Keys or related by any means. I know how to update single model records but I am not able to do the same in case of related models.
My Models :
class UserProfile(models.Model):
user = models.OneToOneField(User)
subject = models.ManyToManyField('Subjects')
phone = models.CharField(max_length=20)
address = models.TextField()
def __unicode__(self):
return self.user.username
class Subjects(models.Model):
name = models.CharField(max_length=100)
code = models.IntegerField()
def __unicode__(self):
return self.name
My serializers :
class UserSerializer(serializers.ModelSerializer):
class Meta:
model = User
fields = (
'id',
'first_name',
'username',
'email',
'is_active',
)
class SubjectSerializer(serializers.ModelSerializer):
class Meta:
model = Subjects
fields = (
'name',
'code',
)
class UserProfileSerializer(serializers.ModelSerializer):
user = UserSerializer()
subject = SubjectSerializer(many=True)
class Meta:
model = UserProfile
fields = (
'user',
'subject',
'phone',
'address',
)
Views :
class UserProfileList(viewsets.ModelViewSet):
serializer_class = UserProfileSerializer
queryset = UserProfile.objects.all()
urls
router.register(r'api/user/profile', UserProfileList)
I can see the first record as /api/user/profile/1/
But when I'll try to update record from Browsable Rest Api it gives me the error user with same username already exist.
Edit :
I want to update UserProfile
Model using UserProfileSerializer
. Simple create new records , Update existing one and delete.
You do have a constraint the username that makes it impossible to create again. You need to remove it by altering validators
UserSerializer.username
. Make sure you don't remove others constraints by printing the serializer:As you can notice, we do have a
UniqueValidator
on the username as well as aRegexValidator
You'll need to alter the class:
Note that the validator here is taken out of the Django definition of that field (https://github.com/django/django/blob/master/django/contrib/auth/models.py#L309)
Now if you print again the serializer you should see that the unique constraint is removed: