Alex Mihoc
Alex Mihoc

Reputation: 80

Django Rest-Framework Serialization Error

So this is the model that I want to serialize:

 from django.db import models

 class Concurs(models.Model):
        name = models.CharField(max_length=50)
        date = models.DateTimeField(auto_now_add=True)
        bio = models.TextField(max_length=5000, blank=True, null=True)
        participants = models.PositiveIntegerField(blank=True, null=True)
        medals = models.PositiveIntegerField(blank=True, null=True)
        done = models.BooleanField()
        link = models.CharField(max_length=1000, blank=True, null=True)
    
        class Meta:
            verbose_name="Concurs"
            ordering = ['-date']
    
        def __str__(self):
            return self.name

This is the serialization process:

from rest_framework import serializers
from .models import Concurs

class ConcursSerializer(serializers.ModelSerializer):
    class Meta:
        model = "Concurs"
        fields = "__all__"

This is the views.py:

from django.shortcuts import render
from .models import Concurs
from .serializers import ConcursSerializer
from rest_framework import generics

class ConcursList(generics.ListCreateAPIView):
    queryset = Concurs.objects.all()
    serializer_class = ConcursSerializer

class ConcursDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Concurs.objects.all()
    serializer_class = ConcursSerializer

The error that I get whenever I navigate to the list or the detail view is:

'str' object has no attribute '_meta'

I think I have made a mistake in the serialization process, I am new to RESTFramework so I really do not know.

Upvotes: 0

Views: 128

Answers (1)

sonus21
sonus21

Reputation: 5398

from rest_framework import serializers
from .models import Concurs

class ConcursSerializer(serializers.ModelSerializer):
    class Meta:
        model = Concurs
        fields = "__all__"

You should use Concurs class reference instead of string name "Concurs"

Upvotes: 1

Related Questions