简体   繁体   中英

Django/Python - AssertionError: Class ThreadSerializer missing "Meta.model" attribute

I've been trying to build the backend of a forum board for a mobile application and I've been running into an issue when trying to test the API endpoints over Postman. It says that my ThreadSerializer class is missing the "Meta.model" attribute.

My serializer code:

from rest_framework import serializers
from forum.models import Thread
from forum.models import Post

class ThreadSerializer(serializers.ModelSerializer):
    class Meta:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        Thread_Model = Thread
        Thread_Fields = ['id', 'thread_id', 'title', 'desc', 'created_at']


class PostSerializer(serializers.ModelSerializer):
    class Meta:
        Post_Model = Post
        Post_Fields = ['id', 'post_id', 'post_content', 'post_time', 'thread_id']
        
        

Model code ---

from django.db import models
from django.conf import settings
from Authentication.models import User
# Create your models here.
class Thread(models.Model):
    userid = models.ForeignKey(User, on_delete=models.CASCADE)
    thread_id = models.AutoField(primary_key=True)
    title = models.CharField(max_length=100)
    desc = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    
    
class Post(models.Model):
    userid = models.ForeignKey(User, on_delete=models.CASCADE)
    thread_id = models.ForeignKey(Thread, on_delete=models.CASCADE)
    post_id = models.AutoField(primary_key=True)
    post_content = models.TextField()
    post_time = models.DateTimeField(auto_now_add=True)
    

Any advice on how to fix this?

This is the error I got on VSC: 错误 VSC

and Postman: 错误邮递员

change

from rest_framework import serializers
from forum.models import Thread
from forum.models import Post

class ThreadSerializer(serializers.ModelSerializer):
    class Meta:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        Thread_Model = Thread
        Thread_Fields = ['id', 'thread_id', 'title', 'desc', 'created_at']


class PostSerializer(serializers.ModelSerializer):
    class Meta:
        Post_Model = Post
        Post_Fields = ['id', 'post_id', 'post_content', 'post_time', 'thread_id']

to:

from rest_framework import serializers
from forum.models import Thread
from forum.models import Post

class ThreadSerializer(serializers.ModelSerializer):
    class Meta:                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                
        model = Thread
        fields = ['id', 'thread_id', 'title', 'desc', 'created_at']


class PostSerializer(serializers.ModelSerializer):
    class Meta:
        model = Post
        fields = ['id', 'post_id', 'post_content', 'post_time', 'thread_id']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM