Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it proper to use django.contrib.auth.models.User in views.py?

Tags:

django

I want to create and delete users in my view for my IPN.

django.contrib.auth.models import User

Or should this all be done in models.py? If so, how would I do that?

Not sure if it's technically wrong or just bad style.

Thanks so much!

like image 665
User Avatar asked Jul 19 '14 01:07

User


2 Answers

I think the best practice is to use django.contrib.auth.get_user_model() instead.

from django.contrib.auth import get_user_model
...

User = get_user_model()

This way things won't break if you later decide to include a django app that extends or overrides the builtin authentication model (it also avoids a class of bugs related to dependency loops).

like image 163
Paulo Scardine Avatar answered Oct 12 '22 22:10

Paulo Scardine


yes it is proper to use it in views.py:

you can get user model like paulo scardine said ie:

from django.contrib.auth import get_user_model
User = get_user_model()

Now to Create User:

User.objects.create_user(username='user2', password='pass')

Now to Remove User:

It is recommend that you set is_active flag to False instead of deleting accounts; that way, if your applications have any foreign keys to users, the foreign keys won’t break.doc

user_rem=User.objects.get(username='user2')
user_rem.is_active=False
user_rem.save()
like image 21
suhailvs Avatar answered Oct 12 '22 22:10

suhailvs