就像这样; DoesNotExist at / post / naver /
我现在收到此错误,因为我在此行的末尾添加了id,profile = MyProfile.objects.get(user=request.user.id)
我正在使用userena。我必须使用id,否则它不会提供'SimpleLazyObject'
这是追溯
Traceback:
/env/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args, **callback_kwargs)
/ebagu/main/views.py" in post
87. profile = MyProfile.objects.get(user=request.user.id)
/env/local/lib/python2.7/site-packages/django/db/models/manager.py" in manager_method
127. return getattr(self.get_queryset(), name)(*args, **kwargs)
/env/local/lib/python2.7/site-packages/django/db/models/query.py" in get
334. self.model._meta.object_name
Exception Type: DoesNotExist at /post/naver/
Exception Value: MyProfile matching query does not exist.
答案 0 :(得分:2)
我认为你应该使用
profile = MyProfile.objects.get(user_id=request.user.id)
答案 1 :(得分:2)
如果不保证数据库中存在配置文件,则需要编写代码来处理该情况。常见的方法是捕获DoesNotExist
异常。
try:
profile = MyProfile.objects.get(user=request.user)
# if it's a OneToOne field, you can do:
# profile = request.user.myprofile
except MyProfile.DoesNotExist:
profile = None
# other code that handles missing profile
如果所有登录用户都有个人资料,但您的视图也处理匿名用户,那么如果您使用if语句,则代码可能会更清晰。您可以实例化显示匿名用户默认值的配置文件。
if request.user.is_authenticated():
profile = request.user.myprofile
else:
profile = MyProfile(favourite_food="pasta", favourite_colour="blue")
答案 2 :(得分:1)
确保用户存在于模型(MyProfile)
中profile = None
if MyProfile.objects.filter(user=request.user).exists():
profile = MyProfile.objects.get(user=request.user)
else:
pass
# Do something if data not exist's
答案 3 :(得分:0)
我假设基于android {
packagingOptions {
exclude 'META-INF/LICENSE'
}
}
是您用户的一对一字段的其他问题。所以你可以通过
MyProfile