0

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.

1
  • loose these "" in the line : model = "Concurs", write just: model = Concurs Commented Dec 9, 2020 at 20:43

1 Answer 1

1
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"

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.