19.5.1 User in the Shell

The User model is rather unusual, because you’re not supposed to interact with it as you do with other Django models. Specifically, you should never import it directly, as is done in Example 19.10, for reasons we’ll see in Chapter 22.

Example 19.10: Python Code

# this is not the best way to do this! >>> from django.contrib.auth.models import User

Instead, developers should use the get_user_model() function supplied by auth to get the User model, as shown in Example 19.11.

Example 19.11: Python Code

# optimal >>> from django.contrib.auth import get_user_model >>> SiteUser = get_user_model() >>> SiteUser <class 'django .contrib.auth.models.User'> >>> User

Get Django Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.