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!
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).
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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With