All code involved is below.
Right now it all works well except when the ‘noc_tech_name’ field is saved it stores the x.id value and not the ‘username’ value.
and when a new user record is added thru admin it does not show up in select unless I shut down the app and restart it.
I also added a **def init(self, *args, kwargs): to the forms class ToLogForm to refresh the users on every form load, but is not working at all.
Goal: Form with select control that has current Django users as ChoiceField option to select.
index_2.html (partial)
<div class="input-group input-group-sm mb-2 w-100">
<label class="input-group-text" for="{{ form.noc_tech_name.id_for_label }}">NOC Tech :**</label>
{{ form.noc_tech_name }}
{% for error in form.noc_tech_name.errors %}
<span class="text-danger">{{ error }}</span>
{% endfor %}
</div>
forms.py (partial)
class ToLogForm(forms.ModelForm):
EntryType(models.IntegerChoices):' in models.py
entrytype = forms.ChoiceField(choices=[[1, 'Control_M'],[2, 'SolarWinds'],[3, 'FYI'],[4, 'Other'],[5, 'Adhoc']],
widget = forms.Select(attrs={'class': 'form-control'}))
noc_tech_name = forms.ChoiceField(choices = [(x.id, x.get_username()) for x in User.objects.all()], widget = forms.Select(attrs={'class': 'form-control'}))
#validation
def clean(self):
super(ToLogForm, self).clean()
social_title = self.cleaned_data.get('social_title')
if len(social_title)<4:
self.add_error('social_title','Can not save social_title less than 4 characters long')
self.fields['social_title'].widget.attrs.update({'class': 'form-control is-invalid'})
return self.cleaned_data
def __init__(self, *args, **kwargs):
super(ToLogForm, self).__init__(*args, **kwargs)
noc_tech_name = forms.ChoiceField(choices = [(x.id, x.get_username()) for x in User.objects.all()], widget = forms.Select(attrs={'class': 'form-control'}))
models.py (partial)
class ToLog(models.Model):
class EntryType(models.IntegerChoices):
control_m = 1, "Control_M"
solarwinds = 2, "SolarWinds"
fyi = 3, "FYI"
other = 4, "Other"
adhoc = 5, "Adhoc"
social_title = models.CharField(max_length=200, null=True)
ivanti_ticket = models.IntegerField(db_column='ivanti_ticket', blank=True, null=True)
date_time = models.DateTimeField(auto_now_add=True)
completed = models.BooleanField(default=False)
comments = models.TextField(blank=True, null=True)
noc_tech_name = models.CharField(max_length=50, blank=True, null=True)
entrytype = models.PositiveSmallIntegerField(choices=EntryType.choices, blank=True, null=True)
def __str__(self):
return self.social_title
views.py (partial)
def edit_turn(request, pk):
if request.method == 'GET':
tologs = ToLog.objects.get(pk=pk)
context = {}
context['tologs'] = tologs
context['form'] = ToLogForm(initial={
'social_title': tologs.social_title,
'ivanti_ticket': tologs.ivanti_ticket,
'date_time': tologs.date_time,
'completed': tologs.completed,
'comments' : tologs.comments,
'noc_tech_name' : tologs.noc_tech_name,
'entrytype' : tologs.entrytype
})
template = 'turnover_app/index_2.html'
return render(request, template, context)
else:
tologs = ToLog.objects.get(pk=pk)
form = ToLogForm(request.POST, instance=tologs)
if form.is_valid():
form.save()
#print('save')
template = 'turnover_app/index_2.html'
return render(request, template, {'form': form,'tologs': tologs})
else:
template = 'turnover_app/index_2.html'
return render(request, template, {'form': form,'tologs': tologs})