I have *App named Company* and in *models.py* there is a model named 
*CompanyProfile*

models.py
```python
class CompanyProfile(models.Model):
    id                  = models.UUIDField(default=uuid.uuid4,
                                            editable=False,
                                            primary_key=True,
                                            verbose_name='ID',)

    full_name            = models.CharField(max_length=255,
                                            unique = True,
                                            default='NA')

    contact_person_email = models.EmailField(unique=True,
                                            null=True,
                                            blank=True)
    def __str__(self):
        return str(self.full_name)
```

serializers.py
````python
class CompanyProfileSerializer(serializers.ModelSerializer):
    """List all the Company Profile"""
    class Meta:
        model = CompanyProfile
        fields = '__all__'
        read_only_fields = ['id']

```

views.py
````python
class CompanyProfileViewSet(viewsets.ModelViewSet):
    """Update and Detail for Company Profile"""

    queryset = CompanyProfile.objects.all()
    serializer_class = CompanyProfileSerializer
    permission_classes = (permissions.IsAuthenticated,)
    lookup_field = 'id'

    def update(self, request, id ,*args, **kwargs):
        instance = self.get_object()
        import pdb; pdb.set_trace()
        if instance.full_name != request.data.get("full_name"):
            instance.full_name = request.data.get("full_name")
        if instance.contact_person_email != 
request.data.get("contact_person_email"):
            instance.contact_person_email = 
request.data.get("contact_person_email")
        instance.save()
        return super().update(request, *args, **kwargs)
````


Now if you see above am actually comparing each field and checking weather 
update is possible there or if the field needs to be updated?

**1. Doing this one each field is quite cumbersome** as I have already set the 
required validation
But now I have to check on each field whether the user has updated them or not!


**2. If I don't have an update in any of the fields then it gives me this 
error** ``` already exists ``` and sometimes as some field are marked unique in 
that case ``` duplicate key value violates unique constraint ```

----------
I understand Technically it's correct is there any simple approach rather than 
this level of workaround.



-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-rest-framework+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/HK0PR01MB1905C791B0FF6D78571FAB35A7340%40HK0PR01MB1905.apcprd01.prod.exchangelabs.com.

Reply via email to