'Update in Nested serializer. not turning off validators. E: this slug already exists

I've made a nested serializers which has slugs in both models. Now I learn that in order to be able to do the update on (unique=True) I need to turn off the validator. But somehow I can't seem to turn it off and it'S still throwing the same error. Any way I should be approaching on this problem?

serializers.py

from rest_framework import serializers

from .models import Question, Answer



class AnswerSerializer(serializers.ModelSerializer):
    """Serialize Answer model"""

    class Meta:
        model = Answer
        fields = ('title', 'body', 'slug', 'author', 'question')
        # https://stackoverflow.com/questions/57249850/overwriting-nested-serializers-create-method-throws-typeerror-create-got-mul
        read_only_fields = ('question',)
        lookup_field = 'slug'
        

# https://stackoverflow.com/questions/55031552/how-to-access-child-entire-record-in-parent-model-in-django-rest-framework
class QuestionSerializer(serializers.ModelSerializer):
    """Serialize Question model"""

    #This answer variable and the fields 'answer' you refer to has to be the SAME but
    #These can change exp: 'aaa'

    answer = AnswerSerializer(read_only=False, source='answers', many=True,)

    class Meta:
        model = Question 
        fields = ('title', 'body', 'slug', 'author', 'category', 'answer',)
        lookup_field = 'slug'


    def create(self, validated_data):

        answers_data = validated_data.pop('answers')

        question = Question.objects.create(**validated_data)

        for answer_data in answers_data:
            #The above stackoverflow link at the answer serializers is realted to this
            Answer.objects.create(question=question, **answer_data)
        return question


    def update(self, instance, validated_data):
        instance.title = validated_data.get('title', instance.title)
        instance.body = validated_data.get('body', instance.body)
        instance.slug = validated_data.get('slug', instance.slug)
        instance.author = validated_data.get('author', instance.author)
        instance.category = validated_data.get('category', instance.category)
        instance.save()
        
        # https://django.cowhite.com/blog/create-and-update-django-rest-framework-nested-serializers/
        answers_data = validated_data.pop('answers')
        aas = (instance.answers).all()
        print("@@@@@@")
        print(aas)
        print("@@@@@@")
        aas2 = list(aas)

        for answer_data in answers_data:
            aas3 = aas2.pop(0)
            aas3.title= answer_data.get('title', aas3.title)
            aas3.body= answer_data.get('body', aas3.body)
            aas3.slug= answer_data.get('slug', aas3.slug)
            aas3.author= answer_data.get('author', aas3.author)
            aas3.question= answer_data.get('question', aas3.question)
            aas3.save()

        return instance

views.py

from django.shortcuts import render
from .models import Question, Answer
from django.conf import settings
from rest_framework import viewsets
from rest_framework.authentication import TokenAuthentication

from .serializers import QuestionSerializer
#from .permissions import UpdateOwnPrice
from rest_framework.permissions import IsAdminUser



class QuestionViewSet(viewsets.ModelViewSet):
    """CRUD """
    serializer_class = QuestionSerializer
    queryset = Question.objects.all()
    authentication_classes = (TokenAuthentication,)
    #permission_classes = (UpdateOwnPrice,)
    lookup_field = 'slug'
    extra_kwargs = {'slug': {'validators': []}}


models.py

from django.db import models
from django.conf import settings
from django.db.models import Q
from django.utils import timezone
from django.urls import reverse



class Category(models.Model):
    name= models.CharField(max_length=100)

    def __str__(self):
        return self.name



class Question(models.Model):
    title= models.CharField(max_length= 100)
    body= models.TextField()
    slug= models.SlugField(unique= True)
    date_posted= models.DateTimeField(default=timezone.now)
    author= models.ForeignKey(settings.AUTH_USER_MODEL, on_delete= models.CASCADE, related_name = 'questions')
    category= models.ForeignKey(Category, on_delete= models.CASCADE, related_name = 'questions')

    #objects= QnAManager()
    
    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('home')

    

class Answer(models.Model):
    title= models.CharField(max_length= 100)
    body= models.TextField()
    slug= models.SlugField(unique= True)
    date_posted= models.DateTimeField(default=timezone.now)
    author= models.ForeignKey(settings.AUTH_USER_MODEL, on_delete= models.CASCADE, related_name = 'answers')
    question= models.ForeignKey(Question, on_delete= models.CASCADE, related_name = 'answers')
    
    #objects= QnAManager()

    def __str__(self):
        return self.title

    def get_absolute_url(self): 
        return reverse('home')



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source