Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change lookup field in Model.viewset to other unique parameter in Django Rest Framework?

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')

enter image description here enter image description here

This is what i want but instead from mail id

like image 466
yatharth meena Avatar asked Apr 27 '20 05:04

yatharth meena


People also ask

What is difference between Api_view and Viewset?

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.

What is Viewset in Django REST framework?

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() .

How do I filter Queryset in Django REST framework?

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.

What is viewset in Django?

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'.

How to write views in Django REST-framework?

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.

What are generic views in Django?

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.

What are the generic views in REST framework?

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.


1 Answers

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 = "[^/]+"  
like image 190
Sumeet Kumar Avatar answered Nov 15 '22 01:11

Sumeet Kumar