M
M
Mikkkch2020-12-08 11:52:46
Django
Mikkkch, 2020-12-08 11:52:46

Should I use get_user_model() when I have a custom user model?

Hello, such a question appeared after I rewrote the user model for the project.

The question is: It's
good to use get_user_model() when establishing relationships between models. The same situation, as far as I know, is the case with forms (in metaclasses).
But, what if I have a written model for the project, which will be the link everywhere? Is this feature worth using?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Chichak, 2020-12-18
@ilya_chch

it's okay if you use it,
but in general, get_user_model() should be used if you are writing a library for Django - because it is not known if the user model will be overridden in the project where this library will
be installed, but if you use type hinting, function cannot be used, you have to do:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from ... import User


def some_func(user: 'User') -> 'User':
    ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question