Когда я отправляю форму, я получаю эти ошибки Поле 'id' ожидало число, но получило Почему я получил эти ошибки? Что не так в моем коде :(
Вид:
def employee_list(request):
if request.POST and request.FILES:
form = EmployeeForm(request.POST, request.FILES)
if form.is_valid():
data = form.save(commit=False)
data.user = RiverTimeUser.objects.create_user(
username=request.POST.get('name'),
pin=int(request.POST.get('pin')),
password=request.POST.get('pin')
)
data.save()
messages.add_message(request, messages.SUCCESS,
'New Employee Added Successfuly')
context = {
'object_list': Employee.objects.filter(department__institute=request.user.institute),
'form': EmployeeForm(request.user.institute.id)
}
return render(request, 'employee.html', context=context)
Форма:
class EmployeeForm(forms.ModelForm):
class Meta:
model = Employee
fields = '__all__'
exclude = ['create', 'update', 'admin', 'user']
widgets = {
'date_of_birth': forms.DateInput(attrs={'type': 'date'}),
}
def clean_name(self):
name = self.cleaned_data.get('name')
if RiverTimeUser.objects.filter(username=name).count() > 0:
raise ValidationError('This Employe Username is already in use.')
return name
def __init__(self, institute, *args, **kwargs):
super().__init__()
self.helper = FormHelper()
self.fields['designation'].queryset = Designation.objects.filter(institute_id=institute)
self.fields['department'].queryset = Department.objects.filter(institute_id=institute)
Отредактированная полная информация о трассировке
Я не смог найти совершенно неправильно
Traceback (most recent call last):
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1772, in get_prep_value
return int(value)
The above exception (int() argument must be a string, a bytes-like object or a number, not 'QueryDict') was the direct cause of the following exception:
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "C:\Users\riaju\Documents\atms\New folder\atms\custom\methods.py", line 24, in wrap
return view_func(request, *args, **kwargs)
File "C:\Users\riaju\Documents\atms\New folder\atms\user_maintenance\views.py", line 22, in employee_list
form = EmployeeForm(request.POST, request.FILES)
File "C:\Users\riaju\Documents\atms\New folder\atms\user_maintenance\forms.py", line 27, in __init__
self.fields['designation'].queryset = Designation.objects.filter(institute=institute)
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\db\models\manager.py", line 82, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\db\models\query.py", line 904, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\db\models\query.py", line 923, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\db\models\sql\query.py", line 1350, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\db\models\sql\query.py", line 1377, in _add_q
child_clause, needed_inner = self.build_filter(
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\db\models\sql\query.py", line 1311, in build_filter
condition = self.build_lookup(lookups, col, value)
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\db\models\sql\query.py", line 1165, in build_lookup
lookup = lookup_class(lhs, rhs)
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\db\models\lookups.py", line 22, in __init__
self.rhs = self.get_prep_lookup()
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\db\models\fields\related_lookups.py", line 115, in get_prep_lookup
self.rhs = target_field.get_prep_value(self.rhs)
File "C:\Users\riaju\Documents\atms\venv\lib\site-packages\django\db\models\fields\__init__.py", line 1774, in get_prep_value
raise e.__class__(
Exception Type: TypeError at /employee/
Exception Value: Field 'id' expected a number but got <QueryDict: {'csrfmiddlewaretoken': ['vfO31XU2Y8x9MBzfUZ7rnq4widBlrt2uu6aF63ZorSW9DO6Rv8gjNE72SIN0YY1p'], 'name': ['Training'], 'designation': ['1'], 'department': ['1'], 'phone': ['12345'], 'date_of_birth': ['2020-12-31'], 'card_number': ['124'], 'pin': ['1234']}>.