I am using Modelviewset in django rest framework. I want to change lookup field to email(unique) instead of id. I have tried adding
lookup_field = 'email'
inside my Schema viewset but it is not working. This is what i am getting
{
"detail": "Not found."
}
How do I resolve this.
my Views.py:
class SchemaViewSet(mixins.CreateModelMixin, mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet):
queryset = models.Schema.objects.all()
serializer_class = serializers.SchemaSerializer
lookup_field = 'email'
my models.py:
class Schema(models.Model):
"""Database model for Schema """
name= models.TextField()
version = models.TextField()
email = models.EmailField(unique = True )
def __str__(self):
return self.email
my serializers.py:
class SchemaSerializer(serializers.ModelSerializer):
"""Serializes Schema"""
class Meta:
model = models.Schema
fields = ( 'id', 'name', 'version', 'email')
APIView allow us to define functions that match standard HTTP methods like GET, POST, PUT, PATCH, etc. Viewsets allow us to define functions that match to common API object actions like : LIST, CREATE, RETRIEVE, UPDATE, etc.
A ViewSet class is simply a type of class-based View, that does not provide any method handlers such as . get() or . post() , and instead provides actions such as . list() and . create() .
The simplest way to filter the queryset of any view that subclasses GenericAPIView is to override the . get_queryset() method. Overriding this method allows you to customize the queryset returned by the view in a number of different ways.
Django REST framework allows you to combine the logic for a set of related views in a single class, called a ViewSet. In other frameworks you may also find conceptually similar implementations named something like 'Resources' or 'Controllers'.
Now we can use the serializers to write views. Django Rest-Framework supports two different types of views. We’ll use ViewSets (Class Based Views). ViewSets works exactly same as Generic Views. The only difference is using ViewSet you don’t have to create separate views for getting list of objects and detail of one object.
Generic views. Django’s generic views... were developed as a shortcut for common usage patterns... They take certain common idioms and patterns found in view development and abstract them so that you can quickly write common views of data without having to repeat yourself.
The generic views provided by REST framework allow you to quickly build API views that map closely to your database models. If the generic views don't suit the needs of your API, you can drop down to using the regular APIView class, or reuse the mixins and base classes used by the generic views to compose your own set of reusable generic views.
Update your code as follow:
class SchemaSerializer(serializers.ModelSerializer):
"""Serializes Schema"""
class Meta:
model = models.Schema
fields = ("id", "email")
lookup_field = "email"
class SchemaViewSet(viewsets.ModelViewSet):
queryset = models.Schema.objects.all()
serializer_class = serializers.SchemaSerializer
lookup_field = "email"
lookup_value_regex = "[^/]+"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With