Django anonymous user in model

[亡魂溺海] 提交于 2019-12-24 12:06:27

问题


I have a model defined as below:

class Example(models.Model):
    user = models.ForeignKey(User, null=True)
    other = models.CharField(max_length=100)

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.

Is there a default value for AnonymousUser to use in a model field?


回答1:


Only users actually in django.contrib.auth.models.User can be in the field, so no. Stick with None/NULL.



来源:https://stackoverflow.com/questions/2474587/django-anonymous-user-in-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!