Hi there once more Ken and thx for the hint, took me a while but i managed it. In my case, ModelChoiceFields are the solution as i am populating a models table with this form. But i didn#t tell you that I’m afraid
So, right now i have this model in models.py:
class Examinations(models.Model):
purpose = models.CharField(max_length=100)
result = models.CharField(max_length=100)
date = models.DateField()
examiner = models.CharField(max_length=50)
valid_thru = models.DateField(null=True, blank=True)
comment = models.CharField(max_length=300, null=True, blank=True)
employee = models.ForeignKey(Employees, on_delete=models.CASCADE)
def __str__(self):
return self.purpose
class Meta:
verbose_name_plural = 'Begutachtungen'
ordering = ['date']
This in my forms.py:
class ExaminationForm(ModelForm):
class Meta:
model = Examinations
fields = '__all__'
purpose = forms.ModelChoiceField(queryset=Tauglichkeit.objects.all(), empty_label="Untersuchung auf",
widget=forms.Select(attrs={
'class': "form-select text-center fw-bold",
'style': 'max-width: auto;',
}))
result = forms.ModelChoiceField(queryset=Ergebnis.objects.all(), empty_label="Begutachtungsergebnis",
widget=forms.Select(attrs={
'class': "form-select text-center fw-bold",
'style': 'max-width: auto;',
}))
date = forms.DateField(label='Untersuchungsdatum:', widget=DateInput(attrs={
'class': "form-control text-center fw-bold",
'style': 'max-width: auto;',
'placeholder': 'Untersuchungsdatum',
'type': 'text',
'onfocus': "(this.type = 'date')"
}))
valid_thru = forms.DateField(label='gültig bis:', required=False, widget=DateInput(attrs={
'class': "form-control text-center fw-bold",
'style': 'max-width: auto;',
'placeholder': 'gültig bis',
'type': 'text',
'onfocus': "(this.type = 'date')"
}))
examiner = forms.CharField(widget=TextInput(attrs={
'class': "form-control text-center fw-bold",
'style': 'max-width: auto;',
'placeholder': 'Untersucher'
}))
comment = forms.CharField(required=False, widget=Textarea(attrs={
'class': "form-control fw-bold",
'style': 'max-width: auto;',
'placeholder': 'Kommentar',
'rows': '3'
}))
employee = forms.ModelChoiceField(queryset=Employees.objects.all(), empty_label="Mitarbeiter",
widget=forms.Select(attrs={
'class': "form-select text-center fw-bold",
'style': 'max-width: auto;',
}))
and this in my views.py:
@access_group("examiner")
def examinations(request):
context = {
'special_header': "Begutachtungen - BA90/5",
'names': Employees.objects.all(),
}
if request.method == 'GET':
form = ExaminationForm()
else:
form = ExaminationForm(request.POST)
if 'id' in request.POST and request.POST['id'] == 'Datensatz auswählen...':
context['error'] = 'Bitte wählen Sie zuerst einen Mitarbeiter aus'
elif 'id' in request.POST:
emp_id = request.POST['id']
last_name = Employees.objects.get(id=emp_id).last_name
first_name = Employees.objects.get(id=emp_id).first_name
rank = Employees.objects.get(id=emp_id).title_rank
context['entries'] = Employees.objects.filter(id=emp_id)
context['id'] = Employees.objects.get(id=emp_id).id
context['prefix_title'] = Employees.objects.get(id=emp_id).prefix_title
context['last_name'] = last_name
context['first_name'] = first_name
context['examinations'] = Examinations.objects.filter(employee_id=emp_id).order_by('-date', 'purpose')
context['special_header'] = 'Begutachtungen - ' + last_name + ', ' + first_name
elif form.is_valid():
print(request.POST['employee_number'])
form.save()
context['success'] = 'Neuer Begutachtungseintrag gespeichert!'
else:
form = ExaminationForm()
context['error'] = 'Es ist ein Fehler aufgetreten, die eingegebenen Daten wurden NICHT gespeichert!'
context['examination_form'] = form
return render(request, 'examination.html', context)
rendered result looks like this:
Filling out and saving works as intended with two little problems:
i want to put a default value other than ‘blank’ in the saved cleaned form-data, if there is no entry in “comment” and i want to get rid of the form field for “employee”, as the employee has to be selected by user already to REACH this form. So actually, wenn submitting the form, additionally the user-id from already chosen employee is handed over together with the form-data. (that’s the “print” command for to check, if this works…)
So alltogehter i want to do something like, but of course, it is not working THIS way, because this would be too simple i think
elif form.is_valid():
print(request.POST['employee_number'])
if form.comment = '':
form.comment = '---'
form.employee = request.POST['employee_number']
form.save()
context['success'] = 'Neuer Begutachtungseintrag gespeichert!'
Hope i could sufficently tell what i intend…