django 在/API/v1/companies/中出现Assert错误

0ve6wy6x  于 2023-10-21  发布在  Go
关注(0)|答案(1)|浏览(106)

Class CompanySerializers missing“Meta.model”attribute我一直在使用Django Rest Api Framework制作API,每当我运行我的应用程序时,它都会显示上述错误。

我的Serializers.py:

from rest_framework import serializers
from api.models import Company

# Create Serializers here

class CompanySerializers(serializers.HyperlinkedModelSerializer):
    class Meta:
        model: Company
        fields = "__all__"

我的model.py:

from django.db import models

# Create your models here.
#Creating Company model
class Company(models.Model):
    company_id = models.AutoField(primary_key= True)
    name = models.CharField(max_length=50)
    location = models.CharField(max_length= 50)
    about = models.TextField()
    type = models.CharField(max_length= 100, choices=
                            (('IT','IT'), ('Non IT', 'Non_IT'), ('Mobiles_phones', 'Mobiles_phones'))) 
    added_Date = models.DateTimeField(auto_now=True)
    active = models.BooleanField(default=True)

我的views.py:

from django.shortcuts import render
from rest_framework import viewsets
from api.models import Company  
from api.serializers import CompanySerializers 
# Create your views here.
class CompanyViewSet(viewsets.ModelViewSet):
    queryset = Company.objects.all()
    serializer_class = CompanySerializers

我已经尝试了一切,但无法解决它。请大家看看这个,这将是非常有帮助的。

m4pnthwp

m4pnthwp1#

你有

class Meta:
    model: Company
    fields = "__all__"

它是一个具有fields属性和注解的类,该注解将属性model指定为Company类型。
你需要

class Meta:
    model = Company
    fields = "__all__"

所以有一个类有两个属性modelfields
IOW,一个单个字符的错别字::应该是=

相关问题