python - Django Rest Framework: Implementing Many-to-Many relationship in Serializer -
i'm using django rest framework create custom api movies model defined follows:
models.py
from django.db import models class genres(models.model): genre = models.charfield(max_length = 128, unique = true) class movies(models.model): popularity = models.floatfield() director = models.charfield(max_length = 128) genres = models.manytomanyfield(genres, related_name = 'movies') imdb_score = models.floatfield() movie_name = models.charfield(max_length = 500)
now, in application, each genre
can have multiple movie
instances , vice versa. result, there many-to-many relationship between genres
model , movies
model. want allow admin chose multiple genres particular movie while posting new movies
instances @ api endpoint.
serializers.py
from shoppy.models import movies, genres rest_framework import serializers class genresserializer(serializers.modelserializer): class meta: model = genres class moviesserializer(serializers.modelserializer): class meta: model = movies fields = ('popularity', 'director', 'imdb_score', 'genres', 'movie_name')
views.py
from shoppy.models import movies rest_framework import viewsets shoppy.serializers import moviesserializer class movieviewset(viewsets.modelviewset): """ api endpoint allows users viewed or edited. """ queryset = movies.objects.all() serializer_class = moviesserializer
urls.py
from django.conf.urls import url, include rest_framework import routers shoppy import views router = routers.defaultrouter() router.register(r'movies', views.movieviewset) # wire our api using automatic url routing. # additionally, include login urls browsable api. urlpatterns = [ url(r'^', include(router.urls)), url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')) ]
however, i'm encountering following error:
exception type: operationalerror @ /movies/ exception value: no such table: shoppy_genres
what seems wrong code?
Comments
Post a Comment