gpt4 book ai didi

python - 属性错误: 'NoneType' object has no attribute 'save'

转载 作者:行者123 更新时间:2023-11-30 21:55:40 25 4
gpt4 key购买 nike

我正在尝试使用react-facebook-login登录facebook而不是存储数据,但它给了我一个关于“保存”的错误,我不明白为什么,我与其他应用程序使用了完全相同的结构,它工作正常,你能告诉我我做错了什么吗?

这是 models.py 代码:


class Social(EmbeddedDocument):
name = StringField()
accountId = ReferenceField(Account)
userId = StringField()
project = ReferenceField(Project)
email = EmailField()
meta = {'allow_inheritance': True}
add_date = DateTimeField(default=datetime.datetime.now)

def save(self, *args, **kwargs):
if not self.add_date:
self.add_date = datetime.datetime.now()
self.add_date = datetime.datetime.now()
return super(Social, self).save(*args, **kwargs)

class Facebook(Social):
pageId = StringField(unique=True)

View .py

class FacebookCreateView(APIView):
permission_classes = (AllowAny,)
def post(self, request):
serializer = FacebookSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)

return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

序列化器.js

class FacebookSerializer(DocumentSerializer):
class Meta:
model = Facebook
fields = ('name', 'accountId','userId','pageId','project','email')

def create(self, validated_data):
facebook_account = Facebook(
accountId=validated_data['accountId'],
name=validated_data['name'],
userId=validated_data['userId'],
pageId=validated_data['pageId'],
project=validated_data['project'],
email=validated_data['email'])
facebook_account.save()
return facebook_account

我正在从前端调用

  responseFacebook = (response) => {  
console.log(response);
let data = new FormData(); // creates a new FormData object
data.append('name', response.name);
data.append('email', response.email);
data.append('userId', response.userID);
data.append('accountId', '5ced550e927bd2cfc01264bc');
data.append('project', '5ce5acf4ec67314c53cae53e');
data.append('pageId', 'tobeadded');
axios.post('http://127.0.0.1:8000/social/fb/', data)
.then(res => { if(res.status === 200){
console.log(res)}})
.catch(err=>console.log(err))
//window.FB.logout();
}

它给了我这个错误:

Internal Server Error: /social/fb/
Traceback (most recent call last):
File "C:\Users\Kouter\Desktop\pfeCode\backend\env\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\Kouter\Desktop\pfeCode\backend\env\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\Kouter\Desktop\pfeCode\backend\env\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Kouter\Desktop\pfeCode\backend\env\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Kouter\Desktop\pfeCode\backend\env\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Kouter\Desktop\pfeCode\backend\env\lib\site-packages\rest_framework\views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Kouter\Desktop\pfeCode\backend\env\lib\site-packages\rest_framework\views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "C:\Users\Kouter\Desktop\pfeCode\backend\env\lib\site-packages\rest_framework\views.py", line 466, in raise_uncaught_exception
raise exc
File "C:\Users\Kouter\Desktop\pfeCode\backend\env\lib\site-packages\rest_framework\views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\Kouter\Desktop\pfeCode\backend\social_accounts\views.py", line 17, in post
serializer.save()
File "C:\Users\Kouter\Desktop\pfeCode\backend\env\lib\site-packages\rest_framework\serializers.py", line 214, in save
self.instance = self.create(validated_data)
File "C:\Users\Kouter\Desktop\pfeCode\backend\social_accounts\serializers.py", line 18, in create
facebook_account.save()
File "C:\Users\Kouter\Desktop\pfeCode\backend\social_accounts\models.py", line 20, in save
return super(Social, self).save(*args, **kwargs)
File "C:\Users\Kouter\Desktop\pfeCode\backend\env\lib\site-packages\mongoengine\document.py", line 97, in save
self._instance.save(*args, **kwargs)
AttributeError: 'NoneType' object has no attribute 'save'

最佳答案

Document 代表 MongoDB 文档(即集合中的记录),Document 类绑定(bind)到特定集合。 EmbeddedDocument 表示嵌套在文档中的结构。

因此,根据设计,EmbeddedDocument 不会附加到任何集合,除非您将其嵌入文档中。

这意味着您无法查询或保存EmbeddedDocument类,您需要查询/保存父文档。

就您而言,您的 EmbeddedDocument 实例未附加到父 Document 类。

使您的 Social 类继承自 Document 而不是 EmbeddedDocument,并且它应该是可保存的。

关于python - 属性错误: 'NoneType' object has no attribute 'save' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56411797/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com