Reputation: 769
I tried creating a new user but it didn't work, I have tried debugging it but don't get a way about this. I have a User Model but want to try and create different user types like students, teachers something like that which would all be in the user user model as well as their various user models.
View.py
def AddCustomerManager(request):
if request.method == "POST":
email = request.POST.get('email')
username = request.POST.get('username')
password = request.POST.get('password')
try:
user = User.objects.create_user(email=email, username=username, password=password, user_type=2)
user.save()
messages.success(request, "Customer Manager Added Successfully")
except:
messages.error(request, "Failed to Add Customer Manager")
return render(request, "pusheat_admin/addcm.html")
models.py
class User(AbstractBaseUser, PermissionsMixin):
email = models.EmailField(_('email address'), unique=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
user_type_choice = ((1, "SuperUser"), (2, "CustomerManager"))
user_type = models.CharField(default=1, choices=user_type_choice, max_length=10)
objects = UserManager()
class CustomerManager(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
fullname = models.CharField(max_length=50, blank=False)
email = models.EmailField()
password = models.CharField(max_length=32)
addcm.html
<form role="form" method="POST">
{% csrf_token %}
<div class="card-header"><h4>Add Customer Manager</h4></div>
<div class="card-body">
<div class="form-group">
<label>Email address</label>
<input type="email" class="form-control" name="email" placeholder="Enter email">
</div>
<div class="form-group">
<label>Username</label>
<input type="text" class="form-control" name="username" placeholder="Username">
</div>
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" placeholder="Password">
</div>
</div>
<div class="card-footer">
<button type="submit" class="btn btn-primary">Add Customer Manager</button>
</div>
</form>
Upvotes: 0
Views: 89
Reputation: 1222
Change this line in models.py
:
user_type = models.CharField(default=1, choices=user_type_choice, max_length=10)
to:
user_type = models.PositiveSmallIntegerField(default=1, choices=user_type_choice, max_length=10)
Upvotes: 2