How to create a POST endpoint in Django with Tastypie
I'm learning Django and I watched and finished a tutorial (Mosh Hamedani and I purchased his videos) that created a database API GET
method.
I was curios how can I create a POST
endpoint to add some new data.
In the tutorial, he used tastypie
module, if matters.
In the tastypie
's documents, I see it says how to add authorization, but it does not say anything about the POST
.
Please help me what's needed to add to and edit the question.
This is api/models.py:
from django.db import models
from tastypie.resources import ModelResource
from movies.models import Movie
class MovieResource(ModelResource):
class Meta:
queryset = Movie.objects.all()
resource_name = 'movies'
excludes = ['date_created']
This is also movies/models:
from django.db import models
from django.utils import timezone
class Genre(models.Model):
name = models.CharField(max_length=255, default='Comedy')
def __str__(self):
return self.name
class Movie(models.Model):
title = models.CharField(max_length=255, default='Bararreh')
release_year = models.IntegerField(default=2008)
number_in_stock = models.IntegerField(default=100)
daily_rate = models.IntegerField(default=150000)
genre = models.ForeignKey(Genre, on_delete=models.CASCADE, default='Comedy')
date_created = models.DateTimeField(default=timezone.now)
These are the fields I have in the admin area to create a new movie.
The main urls.py
:
from django.contrib import admin
from django.urls import path, include
from api_v1.models import MovieResource
from . import views
movie_resource = MovieResource()
urlpatterns = [
path('', views.home),
path('admin/', admin.site.urls),
path('movies/', include('movies.urls')),
path('api/v1/', include(movie_resource.urls)),
]