I got everything to work so far; however, I’m at another step in this process where I am trying to dynamically select a choice in the ChoiceField model for my User in my Employees and Customer Creation views. I have a slight idea how to do this, but it’s not working and I am not sure of the best method here.
Here is where I am at in totality:
models.py
from django.db import models
from django.contrib.auth.models import AbstractUser
# Create your models here.
class User(AbstractUser):
"""User can be Employee or Customer"""
USER_TYPE_CHOICES = (
(1, 'Admin or Business Owner'),
(2, 'Employee'),
(3, 'Customer'),
)
user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES)
class Business(models.Model):
business = models.CharField(max_length=50)
class BusinessOwner(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True )
business = models.ForeignKey(Business, on_delete=models.CASCADE, null=True)
class Customer(models.Model):
""" Customer-specific information """
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True )
business = models.ForeignKey(Business, on_delete=models.CASCADE, null=True)
class Employee(models.Model):
""" Employee-specific information """
user = models.OneToOneField(User, on_delete=models.CASCADE, null=True)
business = models.ForeignKey(Business, on_delete=models.CASCADE, null=True, blank=True)
forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User
from .models import User, Customer, Employee, Business, BusinessOwner
import django_filters
class CustomerFilter(django_filters.FilterSet):
business = django_filters.CharFilter(name='business', lookup_expr='icontains')
customer = django_filters.CharFilter(name='customer', lookup_expr='icontains')
user = django_filters.CharFilter(name='user', lookup_expr='icontains')
class Meta:
model = Customer
fields = ['business', 'customer', 'user', ]
class UserForm(UserCreationForm):
class Meta:
model = User
fields = ( "username", "email", "password1", "password2", "first_name", "last_name", "user_type" )
class BusinessOwnerForm(forms.ModelForm):
class Meta:
model = BusinessOwner
fields = ( )
class EmployeeForm(forms.ModelForm):
class Meta:
model = Employee
fields = ( )
class CustomerForm(forms.ModelForm):
class Meta:
model = Customer
fields = ( )
class BusinessForm(forms.ModelForm):
class Meta:
model = Business
fields = ( "business", )
views.py
def searchUsers(request):
result = Customer.objects.all()
query = request.GET.get("query")
if query:
result = result.filter(
Q(user__customer__icontains=query) |
Q(user__business__icontains=query)
).distinct()
user_filter = CustomerFilter(request.GET, queryset=result)
return render(request, "search_users.html", {'filter': user_filter,
'query': query})
######################
#BUSINESS ADMIN VIEWS#
######################
def BusinessOwnerCreation(request):
if request.method == "POST":
business_form = BusinessForm(request.POST)
user_creation_form = UserForm(request.POST)
businessowner_form = BusinessOwnerForm(request.POST)
if (user_creation_form.is_valid() and business_form.is_valid()):
#Attaches id of Business to Business Owner Form.
business_id = business_form.save(commit=False)
businessowner_form.instance.business = business_id
#Attaches User_Id to to Business Owner Form
new_user = user_creation_form.save(commit=False)
businessowner_form.instance.user = new_user
#Attaches User_Id to to User Creation Form and Business Form.
new_user = user_creation_form.save(commit=False)
business_form.instance.user = new_user
user_creation_form.save()
business_form.save()
businessowner_form.save()
messages.success(request, "You Have Created An Business Owner" )
return redirect("user-homepage")
else:
messages.error(request, "Try creating an Employee Again something went wrong.")
business_form = BusinessForm()
user_creation_form = UserForm()
return render (request, "registration/biz_owner_creation.html",
context={"user_creation_form": user_creation_form,
"business_form": business_form,
})
def employeeCreation(request):
"""Creates an Employee"""
if request.method == "POST":
employee_form = EmployeeForm(request.POST)
user_creation_form = UserForm(request.POST)
if (user_creation_form.is_valid() and employee_form.is_valid()):
employee_form.instance.business = request.user.businessowner.business
new_user = user_creation_form.save(commit=False)
employee_form.instance.user = new_user
user_creation_form.save()
employee_form.save()
messages.success(request, "You Have Created An Employee" )
return redirect("user-homepage")
else:
messages.error(request, "Try creating an Employee Again something went wrong.")
employee_form = EmployeeForm()
user_creation_form = UserForm()
return render (request, "registration/employee_creation.html",
context={"user_creation_form": user_creation_form,
"employee_form": employee_form,
})
def customerCreation(request):
"""Creates a Customer"""
if request.method == "POST":
customer_form = CustomerForm(request.POST)
user_creation_form = UserForm(request.POST)
if (user_creation_form.is_valid() and customer_form.is_valid()):
new_user = user_creation_form.save(commit=False)
customer_form.instance.user = new_user
if request.user.employee:
customer_form.instance.business = request.user.employee.business
else:
pass
if request.user.businessowner:
customer_form.instance.business = request.user.businessowner.business
else:
pass
UserForm(initial = {'user_type' : '3'})
customer_form.save()
user_creation_form.save()
messages.success(request, "You Have Created A Customer")
return redirect("user-homepage")
else:
messages.error(request, "Try creating a Customer Again something went wrong.")
customer_form = CustomerForm()
user_creation_form = UserForm()
return render (request, "registration/customer_creation.html",
context={"user_creation_form": user_creation_form,
"customer_form": customer_form,
})
#There are some other basics views not included here like a index, base, navbar etc . . Not applicable to this problem.
In my customerCreation function i have a line of code:
UserForm(initial = {‘user_type’ : ‘3’})
Am I on the right track here or is there a better way?
Thank you