User
User

Reputation: 24741

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

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!

Upvotes: 6

Views: 261

Answers (2)

suhailvs
suhailvs

Reputation: 21710

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()

Upvotes: 2

Paulo Scardine
Paulo Scardine

Reputation: 77321

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).

Upvotes: 5

Related Questions