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: POSTRequest URL: http://127.0.0.1:8000/API/comment/create/Django Version: 1.5.2Python Version: 2.7.2Installed 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,**kwargs)file "/Users/tlovett1/.virtualenvs/commentflow/lib/python2.7/site-packages/rest_framework/generics.py" in post 372. return self.create(request,**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.errorsfile "/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解决方法 如果有任何人好奇,解决方案是覆盖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总结
以上是内存溢出为你收集整理的python – 在Django REST框架中添加不在模型中的序列化程序的字段全部内容,希望文章能够帮你解决python – 在Django REST框架中添加不在模型中的序列化程序的字段所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)