开发者

Django anonymous user in model

开发者 https://www.devze.com 2022-12-23 23:42 出处:网络
I have a model defined as below: class Example(models.Model): user = models.ForeignKey(User, null=True)

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号