[Django]-Why User model has type string?


The AUTH_USER_MODEL setting is a string, this is often used to refer to the user model, for example in a ForeignKey, the advantage of this is that at that moment, the user model does not have to be loaded (yet).

In order to get a reference to the model, you use the get_user_model() function [Django-doc]:

from .models import Question
from django.contrib.auth import get_user_model

@renderer_classes((TemplateHTMLRenderer, JSONRenderer))
def answers_view(request, *args, **kwargs):
    userstring = request.data['name']
        user0 = get_user_model().objects.get(username=userstring)
    except ObjectDoesNotExist:
        user0 = 'NotFound'
    print('USER: ', user0, flush=True)

Leave a comment