A
A
Alex228Alex2021-02-28 02:50:34
Django
Alex228Alex, 2021-02-28 02:50:34

In Django, the form does not change the parameter of the widget. Why?

Here is my forms.py, here my form inherits everything from the model, but I have one widget that is not standard (Select, and according to the standard, CharField has a TextInput widget). There is a function that generates values ​​for it. Everything is fine, only my widget has a choices parameter that takes a list of values ​​to choose from. And this list, as planned, should change during the execution of the code. But for some reason it doesn't happen. The choices parameter seems to take values ​​once, and then at server startup, and does not want to change them during code execution.
Here are all the files:

# forms.py

from django.forms import ModelForm, TextInput, Select, DateInput
from department.models.employee import Employee
from department.funcional import func


class EmployeeForm(ModelForm):
    class Meta:
        model = Employee
        fields = ['name', 'dep', 'salary', 'position', 'date']
        widgets = {
            "name": TextInput(attrs={
                               'class': 'form-control',
                               'placeholder': 'Enter a name for employee... '
                                }),
            "dep": Select(choices=func.dep_select(), attrs={
                'class': 'form-control',
            }),
            "salary": TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Enter salary... '
            }),
            "position": TextInput(attrs={
                'class': 'form-control',
                'placeholder': 'Enter position... '
            }),
            "date": DateInput(attrs={
                'class': 'form-control',
                'placeholder': 'Enter a date...'
            }),
        }


# views.py

def employee_add(request):
error = ''
form = EmployeeForm()

if request.method == "POST":
    form = EmployeeForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect('employee_home')
    else:
        error = "Ошибка"


time = datetime.now().date()

data = {
    'form': form,
    'error': error,
    'time': time,
}
return render(request, 'employee/employee_add.html', data)


#models.py

from django.db import models

class Employee(models.Model):

    name = models.CharField('Employee:', max_length=30)
    dep = models.CharField('Department:', max_length=30)
    salary = models.CharField('Salary:', max_length=30)
    position = models.CharField('Position:', max_length=30)
    date = models.DateField('Date:', max_length=30)


And here is the function that generates the list. This function receives values ​​from another model and contributes them to dep_set. But for some reason, the values ​​do not want to change during the execution of the code. But I just can't figure out where exactly they don't change, in this function, or whether the choices parameter itself accepts them once when the server starts...
from department.models.department import Department

def dep_select():

    dep_set = []
    department = Department.objects.all()

    for i in department:
        a = (i, i)
        dep_set.append(a)

    return dep_set

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-02-28
@Alex228Alex

Yes, indeed, dep_select is only executed when the EmployeeForm class is defined. To change dep when creating a form, do it in the form's __init__ .
But it's not clear why you have dep CharField, and not ForeignKey on Department, then select would be created automatically.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question