将不在模型中的字段添加到 Django REST 框架中的序列化程序

Posted

技术标签:

【中文标题】将不在模型中的字段添加到 Django REST 框架中的序列化程序【英文标题】:Adding field that isn't in model to serializer in Django REST framework 【发布时间】:2013-08-28 10:48:42 【问题描述】:

我有一个模型评论,在创建时可能会也可能不会创建新用户。出于这个原因,我的 API 在创建新评论时需要密码字段。这是我的评论模型:

class Comment(models.Model):
    commenter = models.ManyToManyField(Commenter)
    email = models.EmailField(max_length=100)
    author = models.CharField(max_length=100)
    url = models.URLField(max_length=200)
    content = models.TextField(blank=True, null=True)
    ip = models.IPAddressField(max_length=45)
    date = models.DateTimeField(default=datetime.now)
    post_title = models.CharField(max_length=200)
    post_url = models.URLField(max_length=200)
    rating = models.IntegerField(max_length=10, default=0)

这是我的 API 视图:

class CommentNewView(CreateAPIView):
    model = Comment
    serializer_class = CommentCreateSerializer

这是我的序列化器:

class CommentCreateSerializer(serializers.ModelSerializer):
    commenter_pw = serializers.CharField(max_length=32, required=False)

    class Meta:
        model = Comment
        fields = ('email', 'author', 'url', 'content', 'ip', 'post_title', 'post_url', 'commenter_pw')

这是我得到的错误:

Environment:


Request Method: POST
Request URL: http://127.0.0.1:8000/api/comment/create/

Django Version: 1.5.2
Python Version: 2.7.2
Installed Applications:
('commentflow.apps.dashboard',
 'commentflow.apps.commenter',
 'commentflow.apps.comment',
 'rest_framework',
 'rest_framework.authtoken',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django.contrib.admin',
 'django.contrib.admindocs')
Installed Middleware:
('django.middleware.common.CommonMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.locale.LocaleMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware')


Traceback:
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  115.                         response = callback(request, *callback_args, **callback_kwargs)
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/django/views/generic/base.py" in view
  68.             return self.dispatch(request, *args, **kwargs)
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/django/views/decorators/csrf.py" in wrapped_view
  77.         return view_func(*args, **kwargs)
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  327.             response = self.handle_exception(exc)
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/rest_framework/views.py" in dispatch
  324.             response = handler(request, *args, **kwargs)
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/rest_framework/generics.py" in post
  372.         return self.create(request, *args, **kwargs)
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/rest_framework/mixins.py" in create
  50.         if serializer.is_valid():
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/rest_framework/serializers.py" in is_valid
  479.         return not self.errors
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/rest_framework/serializers.py" in errors
  471.                 ret = self.from_native(data, files)
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/rest_framework/serializers.py" in from_native
  867.         instance = super(ModelSerializer, self).from_native(data, files)
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/rest_framework/serializers.py" in from_native
  324.             return self.restore_object(attrs, instance=getattr(self, 'object', None))
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/rest_framework/serializers.py" in restore_object
  852.             instance = self.opts.model(**attrs)
File "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/django/db/models/base.py" in __init__
  415.                 raise TypeError("'%s' is an invalid keyword argument for this function" % list(kwargs)[0])

Exception Type: TypeError at /api/comment/create/
Exception Value: 'commenter_pw' is an invalid keyword argument for this function

【问题讨论】:

【参考方案1】:

您可以做的是覆盖pre_savecreate 函数并从发送的数据字段中取出commenter_pw(不确定,但您可以从request.POST 或之后取出它你已经序列化了它),所以框架不应该出现错误。

另外,如果你有额外的逻辑,你可以在保存之前实现它(例如用于检查是否必须创建用户或什么)。

【讨论】:

【参考方案2】:

如果有人好奇,解决方法是重写 restore_object 方法,并在实例化后将额外的实例变量添加到注释对象:

def restore_object(self, attrs, instance=None):
        if instance is not None:
            instance.email = attrs.get('email', instance.email)
            instance.author = attrs.get('author', instance.author)
            instance.url = attrs.get('url', instance.url)
            instance.content = attrs.get('content', instance.content)
            instance.ip = attrs.get('ip', instance.ip)
            instance.post_title = attrs.get('post_title', instance.post_title)
            instance.post_url = attrs.get('post_url', instance.post_url)
            return instance

        commenter_pw = attrs.get('commenter_pw')
        del attrs['commenter_pw']

        comment = Comment(**attrs)
        comment.commenter_password = commenter_pw

        return comment

【讨论】:

这对我不起作用。我得到:'评论'对象没有属性'commenter_pw' 好的。我必须设置 write_only=True【参考方案3】:

感谢您的回答,对我帮助很大:)

但我觉得这更通用一点,因为我仍然想调用超级序列化器类上的方法

def restore_object(self, attrs, instance=None):
    '''
    we have to ensure that the temporary_password is attached to the model
    even though it is no field
    '''
    commenter_pw = attrs.pop('comment_pw', None)
    obj = super(
        CommentCreateSerializer, self
    ).restore_object(attrs, instance=instance)
    if commenter_pw:
        obj.commenter_pw = commenter_pw
    return obj

【讨论】:

【参考方案4】:

以前的答案不适用于 DRF3.0,restore_object() 方法现已弃用。

我使用的解决方案很糟糕,但我还没有找到更好的解决方案。 我在模型上为此字段放置了一个虚拟 getter/setter,这允许将此字段与模型上的任何其他字段一样使用。

记得在序列化器定义中将该字段设置为 write_only。

class Comment(models.Model):
    @property
    def commenter_pw():
        return None

    @commenter_pw.setter
    def commenter_pw(self, value):
        pass

class CommentCreateSerializer(serializers.ModelSerializer):
    commenter_pw = serializers.CharField(max_length=32, write_only=True, required=False)

    class Meta:
        model = Comment
        fields = ('email', 'author', 'url', 'content', 'ip', 'post_title', 'post_url', 'commenter_pw')

【讨论】:

以上是关于将不在模型中的字段添加到 Django REST 框架中的序列化程序的主要内容,如果未能解决你的问题,请参考以下文章

Django Rest Framework 中的模型翻译

Django REST框架--认证和权限

Django Rest Framework - 将序列化器字段映射到数据库列名

如何在 django rest 框架中仅使用特定变体对象将项目添加到愿望清单?

如何在 django rest 中自动继承外键模型的字段?

将 django-rest-framework 中的超链接添加到 ModelViewSet