'How do you resolve unique field clashes in django?
I have a model that looks like this:
class Foo(models.Model):
unique_field = models.URLField(unique=True)
another_field = models.TextField()
# etc...
and its corresponding ViewSet looks like this (bound to /foo/):
class FooViewSet(
viewsets.GenericViewSet,
mixins.ListModelMixin,
mixins.CreateModelMixin,
mixins.DestroyModelMixin,
):
queryset = Foo.objects.all()
serializer_class = FooSerializer
When I make a POST request to /foo/, if there's no clash I just want the default behaviour (create a new row in the database and return its serialized form as a json blob).
The problem is that when there is a clash the server just throws an error. What I'd rather have it do is return the json blob for the existing data, rather than crashing. Or at the very least return some kind of descriptive error.
Is there a way to get this behaviour without manually overriding create()?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
