简体   繁体   中英

django use authenticated user in models

In django, I want to use the authenticated user in my models and I'm using allauth for authentication. In my model I have:

created_by = models.ForeignKey(settings.AUTH_USER_MODEL)

I know now that I have to put something in my settings.py like :

AUTH_USER_MODEL = 'auth.User'

But this is not working. What is the best way to handle this?

I found the solution: In models.py I have now

created_by = models.ForeignKey('auth.User')

and in my view I have

if form.is_valid():
    instance = form.save(commit=False)
    instance.created_by = request.user
    instance.save()

You'll need to do it in the view, as the model has no idea where it is being created from.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM