It's good future-proofing, in case you ever change the User model in your 
project. In real life, that's highly unlikely, but where it's *really 
important* to use get_user_model() is when you are writing reusable apps 
for distribution. If your app is intended to be dropped into any existing 
Django project, you have no idea what User model is in use by other 
people's projects. This layer of abstraction makes it possible for 
re-usable apps that depend on some User model existing to work no matter 
what.

./s


On Saturday, February 17, 2018 at 2:41:12 PM UTC-8, tangoward15 wrote:
>
>
> Hi, 
>
> I am playing around with user registration. I came across a code where the 
> get_user_model() was assigned to a model in Meta class inside a form. I was 
> just wondering, what is the benefit of using the get_user_model() as Model 
> in a form instead of importing a class from models.py then use that class 
> as model of the form and when should I use it?
>
> models.py
> class RegUser(User):
>
>     def __str__(self):
>         return self.username
>
> forms.py
>
> class UserCreateForm(UserCreationForm):
>
>     class Meta:
>         fields = ('username', 'password1', 'password2')
>         model = get_user_model()
>
>
> Thanks,
> Jarvis
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/b0b0b57c-e152-4340-98ba-86933156a737%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to