1

I am trying to implement Rest api using Django framework. But when I click on the url on the default index page it gives me an assertion error at/languages/ Class LanguageSerializer missing meta.model attribute

I made all the migrations after changes in models.py but it did nothing

urls.py

from django.urls import path, include
from . import views
from rest_framework import routers

router = routers.DefaultRouter()
router.register('languages', views.LanguageView)

urlpatterns = [
    path('', include(router.urls))
]

models.py

from django.db import models

class Language(models.Model):
    name = models.CharField(max_length=50)
    paradigm = models.CharField(max_length=50)

serializers.py

from rest_framework import serializers
from .models import Language

class LanguageSerializer(serializers.ModelSerializer):
    class Meta:
        fields = ('id', 'name', 'paradigm')

views.py

from django.shortcuts import render
from rest_framework import viewsets
from .models import Language
from .serializers import LanguageSerializer

class LanguageView(viewsets.ModelViewSet):
    queryset = Language.objects.all()
    serializer_class = LanguageSerializer

I have no clue where am I going wrong

1 Answer 1

3

You need to specify what model you want to serialize in the Meta class of your serializer, like:

from rest_framework import serializers
from .models import Language

class LanguageSerializer(serializers.ModelSerializer):

    class Meta:
        model = Language  # specify the model
        fields = ('id', 'name', 'paradigm')

otherwise the serializer can not determine the fields of that model, and how it will serialize the data from these fields.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.