title: DJango REST Framework: How to write Create, List, Update and Delete with ModelViewSet
body: <p>I'm still new to python/django and currently I'm trying to add REST API using django rest framework. This is currently what I have:</p>
  
  <p>urls.py</p>
  
  <pre class="lang-py prettyprint-override"><code>actions_url = {'actions': {'get': 'list', 'post': 'create'}}
  actions_obj = {'actions': {'get': 'retrieve', 'delete': 'destroy', 'put': 'update'}}
  
  urlpatterns = [
     path('book', BookViewSet.as_view(**actions_url)),
     path('book/&lt;str:isbn&gt;', BookViewSet.as_view(**actions_obj))
  ]
  </code></pre>
  
  <p>models.py (The model doesn't have PK)</p>
  
  <pre class="lang-py prettyprint-override"><code>class Book(models.Model):
     BNAME = models.CharField(
         db_column='BNAME',
         max_length=30,
         default='')
  
     BISBN = models.CharField(
         db_column='BISBN',
         max_length=10,
         default='')
  </code></pre>
  
  <p>views.py</p>
  
  <pre class="lang-py prettyprint-override"><code>class BookSerializer(serializers.ModelSerializer):
     name = serializers.CharField(source="BNAME")
     isbn = serializers.CharField(source="BISBN")
  
         class Meta:
             model = models.Book
             fields = ('name', 'isbn')
  
  
  class BookViewSet(viewsets.ModelViewSet)
     queryset = Book.objects.all()
     serializer_class = BookSerializer
  
     def list(self, request):
         return queryset
  
     def create(self, request):
         pass
  
     def retrieve(self, request, *args, **kwargs):
         pass
  
     def update(self, request, *args, **kwargs):
         pass
  
     def destroy(self, request, *args, **kwargs):
         pass
  </code></pre>
  
  <p>So now my questions are:</p>
  
  <ol>
  <li>If I'm not including <code>def create(self, request)</code> in the <code>BookViewSet</code> for example, I assume when I make a <code>POST /book</code> it will do nothing?</li>
  <li>I'm struggling to understand what to put inside <code>create</code>, <code>retrieve</code>, <code>update</code>, <code>destroy</code>. For example if I make a <code>PUT /book/123</code> with params: <code>{ isbn: '123', name: 'test'}</code>, what do I need to do in <code>def update</code> to retrieve those data from request then update data to database?</li>
  <li>I want to validate <code>isbn</code> and make sure it is unique/no duplicate before saving a record/update it to database, what should I do? I tried reading about Validator: <a href="https://www.django-rest-framework.org/api-guide/validators/" rel="nofollow noreferrer">https://www.django-rest-framework.org/api-guide/validators/</a> and looks like I need to update serializer:</li>
  </ol>
  
  <pre class="lang-py prettyprint-override"><code>class BookSerializer(serializers.ModelSerializer):
      name = serializers.CharField(source="BNAME")
      isbn = serializers.CharField(source="BISBN", validators=[&lt;UniqueValidator(queryset=Book.objects.all())&gt;])
  
          class Meta:
              model = models.Book
              fields = ('name', 'isbn')
  </code></pre>
  
  <p>Is this correct?</p>
  
  <ol start="4">
  <li>Similar to previous question, but now I want to make sure both <code>isbn</code> and <code>name</code> are unique together, I assume this is what I need to do:</li>
  </ol>
  
  <pre class="lang-py prettyprint-override"><code>class BookSerializer(serializers.ModelSerializer):
      name = serializers.CharField(source="BNAME")
      isbn = serializers.CharField(source="BISBN")
  
          class Meta:
              model = models.Book
              fields = ('name', 'isbn')
  
          validators = [
              UniqueTogetherValidator(
                  queryset=Book.objects.all(),
                  fields=('name', 'isbn')
              )
          ]
  </code></pre>
  
  <p>Is this correct?</p>
  
  <p>Thanks</p>