【问题标题】:Django REST Framework - Class UserSerializer missing "Meta.model" attribute [closed]Django REST Framework - 类 UserSerializer 缺少“Meta.model”属性 [关闭]
【发布时间】:2017-08-10 12:00:39
【问题描述】:

我正在关注 Django REST 框架的 tutorial,一切都很好,直到第 4 部分 - 身份验证和权限 -> Adding login to the Browsable API,我想浏览我通过 url http://localhost:8024/users/ 创建的用户。

但我收到此错误消息:

AssertionError at /users/
Class UserSerializer missing "Meta.model" attribute
Request Method: GET
Request URL:    http://localhost:8024/users/
Django Version: 1.10
Exception Type: AssertionError
Exception Value:    
Class UserSerializer missing "Meta.model" attribute
Exception Location: C:\Python27\lib\site-packages\rest_framework\serializers.py in get_fields, line 976
Python Executable:  C:\Python27\python.exe
Python Version: 2.7.11
Python Path:    
['D:\\github\\py2\\dj-rest\\tutorial',
 'C:\\Windows\\system32\\python27.zip',
 'C:\\Python27\\DLLs',
 'C:\\Python27\\lib',
 'C:\\Python27\\lib\\plat-win',
 'C:\\Python27\\lib\\lib-tk',
 'C:\\Python27',
 'C:\\Python27\\lib\\site-packages']
Server time:    Thu, 10 Aug 2017 11:41:38 +0000

但我的UserSerializer 类中确实有class Meta,如“serializers.py”文件中所示,请查看并给我帮助。

Serializer.py

​​>
# serializers.py
from rest_framework import serializers
from snippets.models import Snippet
from django.contrib.auth.models import User
class SnippetSerializer(serializers.ModelSerializer):
    # add a new field
    owner = serializers.ReadOnlyField(source='owner.username')

class Meta:
    model = Snippet
    fields = ('id', 'title', 'code', 'file',
              'linenos', 'language', 'style', 'owner')


class UserSerializer(serializers.ModelSerializer):
    """
    Because 'snippets' is a reverse relationship on the User model,
    it will not be included by default when using the ModelSerializer class,
    so we needed to add an explicit field for it.
    """
    snippets = serializers.PrimaryKeyRelatedField(many=True,


queryset=Snippet.objects.all())

    class Meta:
        module = User
        fields = ('id', 'username', 'snippets')

models.py

​​>
from __future__ import unicode_literals

from django.db import models
from pygments.lexers import get_all_lexers, get_lexer_by_name
from pygments.styles import get_all_styles
from pygments.formatters.html import HtmlFormatter
from pygments import highlight

LEXERS = [item for item in get_all_lexers() if item[1]]
LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])
STYLE_CHOICES = sorted((item, item) for item in get_all_styles())


# Create your models here.
class Snippet(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=100, blank=True, default='')
    code = models.TextField()
    file = models.FileField()
    linenos = models.BooleanField(default=False)
    language = models.CharField(choices=LANGUAGE_CHOICES, default='python', max_length=100)
    style = models.CharField(choices=STYLE_CHOICES, default='friendly', max_length=100)
    owner = models.ForeignKey('auth.User', related_name='snippets', on_delete=models.CASCADE)
    highlighted = models.TextField()

    class Meta:
        ordering = ('created',)

    def save(self, *args, **kwargs):
        """
        Use the `pygments` library to create a highlighted HTML
        representation of the code snippet.
        """
        lexer = get_lexer_by_name(self.language)
        linenos = self.linenos and 'table' or False
        options = self.title and {'title': self.title} or {}
        formatter = HtmlFormatter(style=self.style, linenos=linenos,
                                  full=True, **options)
        self.highlighted = highlight(self.code, lexer, formatter)
        super(Snippet, self).save(*args, **kwargs)

views.py

​​>
from snippets.models import Snippet
from snippets.serializers import SnippetSerializer, UserSerializer
from rest_framework import generics
from django.contrib.auth.models import User  # for authentication and permissions
from rest_framework import permissions

class SnippetList(generics.ListCreateAPIView):
    """
    get -> list;
    post -> create;
    """
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
    queryset = Snippet.objects.all()
    serializer_class = SnippetSerializer

    def perform_create(self, serializer):
        serializer.save(owner=self.request.user)


class SnippetDetail(generics.RetrieveUpdateDestroyAPIView):
    """
    get -> retrieve;
    put -> update;
    delete -> destroy;
    """
    permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
    queryset = Snippet.objects.all()
    serializer_class = SnippetSerializer


class UserList(generics.ListAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer


class UserDetail(generics.RetrieveAPIView):
    queryset = User.objects.all()
    serializer_class = UserSerializer

sn-ps/urls.py

​​>
from django.conf.urls import url
from rest_framework.urlpatterns import format_suffix_patterns
from snippets import views

urlpatterns = [
    url(r'^snippets/$', views.SnippetList.as_view()),
    url(r'^snippets/(?P<pk>[0-9]+)/$', views.SnippetDetail.as_view()),
    url(r'^users/$', views.UserList.as_view()),
    url(r'^users/(?P<pk>[0-9]+)/$', views.UserDetail.as_view()),
]

# Adding optional format suffixes to our URLs
urlpatterns = format_suffix_patterns(urlpatterns)

【问题讨论】:

  • 序列化器中应该是model,而不是module
  • 天啊,我犯了一个如此愚蠢的错误!但是我自己真的很难找到。非常感谢!

标签: python django rest


【解决方案1】:

如在评论中将module 替换为model

class UserSerializer(serializers.ModelSerializer):
    """
    Because 'snippets' is a reverse relationship on the User model,
    it will not be included by default when using the ModelSerializer class,
    so we needed to add an explicit field for it.
    """
    snippets = serializers.PrimaryKeyRelatedField(
        many=True, queryset=Snippet.objects.all())

    class Meta:
        model = User
        #^^^^^^

【讨论】:

    【解决方案2】:

    当您错过子类 Meta: 属性时,这很常见, 在我的情况下是

    class RegisterSerializer(serializers.ModelSerializer):
    class Meta:
    model: User
        fields: ('id', 'username', 'email', 'password')
        extra_kwargs = {'password': {'write_only': True}}
    

    我使用 colon(:) 而不是 equals to(=) 来设置子类 Meta 属性

    class RegisterSerializer(serializers.ModelSerializer):
    class Meta:
    model = User
        fields = ('id', 'username', 'email', 'password')
        extra_kwargs = {'password': {'write_only': True}}
    

    属性使用 (=) 而非 (:)

    分配变量

    【讨论】:

      猜你喜欢
      • 2019-11-22
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 2023-01-30
      • 1970-01-01
      • 2021-12-25
      • 2014-03-19
      • 2014-11-27
      相关资源
      最近更新 更多