How to assign a user to an employee in Django

I am a beginner in django and trying to create a web application for my task. I am trying to assign a user to every to employee. The idea is that user(in my class Employee) brings a list of user options that have not yet been assigned to the new employee to be registered

My model

class Employee():

  name = models.CharField(max_length=100)
  job = models.CharField(max_length=30)
  user = models.OneToOneField(User,on_delete=models.CASCADE)

def __str__(self):

My form

  class EmployeeForm(forms.ModelForm):
    user= forms.ModelChoiceField(queryset=User.objects.filter(id=Employee.user))
  class Meta:
     model = Employee
     fields = [

My view

def register_employee(request):
  if request.method == 'POST':
    form = EmployeeForm(request.POST)
     if form.is_valid():
        return redirect('/')
     return redirect('list_employee')
     form = EmployeeForm()
  return render(request, 'employee/employee_form.html',{'form': form})

I guess your code to be modified as,

  class EmployeeForm(forms.ModelForm):
    user= forms.ModelChoiceField(queryset=User.objects.filter(employee=None))

Please try the above.

Back to Top