In Django, where to define user

For a site where users host discussions communicate with each other like on X(twitter), using Django, where should i define a user; in the forms.py, models.py or views.py file. This site will work with users data eg. when grading users.

Use the built-in User model. If you need to customize it, do so in model.py: https://docs.djangoproject.com/en/6.0/topics/auth/customizing/#specifying-a-custom-user-model.

You do not per se need to define a User model: Django already has one defined in the django.contrib.auth.models module, and this one is used by default.

However you can define one yourself (or use a third party Django application). Then it is in the models.py of an app. Often this app is named account or user, but you can use whatever you think is useful.

While not required, it is often a good idea to inherit from AbstractBaseUser if you wish to define a custom user model, since this will offer a lot of functionality without having to define it yourself: think about creating users through a management command, setting a password, checking permissions, etc.

So then it looks like:

# user/models.py

class CustomUser(AbstractBaseUser):
    # ...

beware that if you define a custom user model, you set it in the settings.py file:

# settings.py

AUTH_USER_MODEL = 'user.CustomUser'
Вернуться на верх