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')
score:8
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 = "[^/]+"
score:0
For me it is even working, when I am not setting the lookup_field in the Meta class of the serializer.
So try out only setting the lookup_field in your view set. That might be enough.
score:0
For me I only needed to update the ViewSet
. No need to update the Serializer
class SchemaViewSet(viewsets.ModelViewSet):
queryset = models.Schema.objects.all()
serializer_class = serializers.SchemaSerializer
lookup_field = "email"
Credit To: stackoverflow.com
Related Query
- How to change lookup field in Model.viewset to other unique parameter in Django Rest Framework?
- How do I change field or model attributes of a third-party Django app?
- How to change key of lookup kwargs field in RetrieveAPIView in Django REST Framework?
- How to access other model instance fields from to_python in a custom Django field implementation
- How to set Value of One field of Django model equal to Other field of other Django model
- How to use field from one model into other model while rendering a single view in Django
- How do something when i change a field on the django admin for a model
- How do I change the object name of my Django model by the data of a field of my database?
- How to set django model field value based-on value of other field in the same Model
- How can I change the value of a Django model field when a particular choice is selected?
- Django admin - How to calculate a field value depending on other model field values
- How to modify field rendering behaviour based on state of other fields of model in django
- How to tell Django model that only one or other field must be not empty?
- How to mention other field as Foreign Key in Django Model
- how to pass url to a model field lookup in django
- How to change value of the field in a proxy model in Django
- How do I create a Django model field that evaluates based on other fields?
- How to Change OneToOne model Field Default Message in Django Admin?
- how to change field type and retain choices defined in model django 3.0
- How to map url parameter to some other filter field in Django DRF?
- how to filter field values based on other field id of same model in Django
- How can I make a Model field value unique (unique=True) but only comparing it to other models from that User?
- Django : how to change the value of a field or update the field in model / Db .?
- Django - How to rename a model field using South?
- How to change field name in Django REST Framework
- How to use enums as a choice field in django model
- Generate unique id in django from a model field
- How to create Password Field in Model Django
- Change type of Django model field from CharField to ForeignKey
- How to filter model results for multiple values for a many to many field in django
More Query from same tag
- Abstract form or form composition in django?
- Database performance in Django/Postgres
- Django Add new record error duplicate key value violates unique constraint "" Django Id doesn't sync with database
- Django Get name of current view as a string
- Custom django-revproxy permissions
- Annotations are ignored in Django/Tastypie
- Creating blog on Django - URLs won't cooperate
- New version of django-model-utils doesn't have PassThroughManager
- Django model formset query generates extra object
- Django forms show cleaned data if validation fails
- Django elasticsearch transport error no query found
- Bad Gateway (502) on nginx > uwsgi > django, but only one SOME devices
- Django Celery Chord Not Executing
- How to override ModelChoiceField / ModelMultipleChoiceField default widget with a template for each choice
- How to fix "dictionary update sequence element # 0 has length 104; 2 is required" error