模型中的Django匿名用户 [英] Django anonymous user in model
本文介绍了模型中的Django匿名用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的模型定义如下:
class Example(models.Model):
user = models.ForeignKey(User, null=True)
other = models.CharField(max_length=100)
问题是Django拒绝将django.contrib.auth.models.AnonymousUser直接分配给Example.user作为null字段,因此每次我都必须检查request.user.is_authenticated()是否手动分配Example.user = None.
The problem is Django refuses to assign django.contrib.auth.models.AnonymousUser directly to Example.user as null field so everytime I have to check if request.user.is_authenticated() ans assign Example.user = None manually.
AnonymousUser是否在模型字段中使用默认值?
Is there a default value for AnonymousUser to use in a model field?
推荐答案
只有实际在django.contrib.auth.models.User
中的用户才能在该字段中,所以不可以.坚持使用None
/NULL
.
Only users actually in django.contrib.auth.models.User
can be in the field, so no. Stick with None
/NULL
.
这篇关于模型中的Django匿名用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文