Pass initial values to ChoiceField in django formset - django

I can't figure out how to populate a django ChoiceField with initial data. Preferrably want to do it in the view, as it will change depending on the parameters I pass to the view.
views.py
def index(request):
init_ingredients = [{'food':'Candy','amt':12,'units':'cup'},{'food':'Bacon','amt':9,'units':'cup'}]
IngredientsFormSet = formset_factory(IngredientLineForm, can_delete=True)
if request.method == 'POST':
formset = IngredientsFormSet(request.POST, request.FILES)
...
else:
formset = IngredientsFormSet(initial=init_ingredients)
the 'food' field and the 'amt' field populate, but the 'units' field - which is an html Select input does not populate with initial value. Do I need to define choices too? and have the initial value be one of them?
forms.py
class IngredientLineForm(forms.Form):
food = forms.CharField(widget=forms.TextInput(attrs={'class':'foods form-control'})) #class = food
units = forms.ChoiceField(widget=forms.Select(attrs={'class':'units form-control'}))
amt = forms.CharField(widget=forms.NumberInput(attrs={'class':'amt form-control'}))

I use:
class netadminGlobalFormView(LoginRequiredMixin, FormView):
model = netInfo
form_class = netInfoForm
def get_initial(self):
initial = super(netadminGlobalFormView, self).get_initial()
initial['eth0_ip'] = self.model_instance.get_eth0_ip_stdout
initial['config_type'] = 'DHCP'
return initial
Note that here:
initial['config_type'] = 'DHCP'
I set a value from selection:
# value displayed value
config_types=(
('DHCP', 'Automatic (DHCP)'),
('MANUAL', 'Static (manual)')
)
and form definition includes the following:
class netInfoForm(ModelForm):
eth0_ip=forms.GenericIPAddressField(protocol='IPv4',
widget=forms.TextInput(
attrs={'placeholder': 'xxx.xxx.xxx.xxx'}),
max_length=IPv4_addr_chars,
label=IPv4_addr_html_label,
help_text='required: i.e. 192.168.111.12 ',
required=True
# ,error_messages={'required': 'Please enter IPv4 address, i.e. 192.168.111.12'}
)
config_type = forms.ChoiceField(choices=config_types, widget=forms.RadioSelect())
#,initial='MANUAL')
and in model:
class netInfo(models.Model):
eth0_ip = models.CharField(max_length = IPv4_addr_chars, blank=True, null=False, default=get_eth0_ip_stdout)
config_type = models.CharField(max_length=6, blank=False, null=False, default="DHCP")
W/o using initial value 'DHCP' or 'MANUAL' in sample above the choice starts unselected. Also note that initial could be set in form class (commented above).
So, exactly to your questions:
1> Do I need to define choices too?
Yes, choices should be defined in model.
2> and have the initial value be one of them?
Yes, initial values for choices must match your choices definition for form and model.
At least that's so in django 2.0 .
As about question 1) - I can't claim there is no ability to init choices other way, but for my sample answer for question 2) is exactly that - non-matching values ignored (didn't raise exception).

Related

Django forms: Is it possible to have multiple drop down menus for different tags within a field?

I have a form in a formset where I would like to display multiple drop down menus under a single field 'tests'. I have achieved this in the form of having a single dropdown menu within 'optgroup' tags (see image below).
I guess this way you can only choose a single value.
However, is it possible to 'nest' these drop downs? I.e have them all under one field 'tests', but be able to have several dropdowns with 'tags' and choose results for each tag? Or do I need a field for each 'tag'?
My forms.py:
class ReportForm(forms.ModelForm):
summary = forms.CharField(
widget=forms.Textarea(attrs={'rows':3, 'cols':70}),
label='',
required=False)
tests = forms.CharField(widget=forms.HiddenInput())
class Meta:
model = ClinicallyReportedSample
fields = ('id', 'summary', 'tests', 'hilis_reported')
def __init__(self, *args, **kwargs):
json_data = kwargs.pop('json_data', None)
super(ReportForm, self).__init__(*args, **kwargs)
crs_obj = self.instance
for j in json_data:
if j['lab_no'] == str(crs_obj):
json = j
summary = json['summary']
self.fields['summary'].initial = summary
self.fields['reported'].label = crs_obj
tests = json.get('tests', None)
if tests:
test_choices = (
('mutated', 'mutated'),
('mutated - see comments', 'mutated - see comments'),
('awaiting confirmation', 'awaiting confirmation'),
)
self.fields['tests'] = forms.ChoiceField(
required=True,
label='Current or repeat samples?',
choices=((k, test_choices) for k in tests),
)
What I get now:
I would instead want a dropdown for each gene, and those choices. Do I need to make a field for each gene? The problem I have with doing this is that each result can have 0-10 genes, and this would be incredibly difficult to render in a HTML table.
Thanks
You probably want to implement something template/client-side to handle that, such as Chosen or Selectize.js (see the option groups examples).
Then on your form class implement a clean and/or clean_[field_name] method if you need to get your selected data in the format you want.

Multiple Form with Single Submit Button

I'm currently working with django project. I had to filter the data store on the database based on the user input on form (at template) as looked below.
On form user either enter value or leave it blank. So what I have to do is first find the (valid) user input and then fire appropriate query to display data as user input in the form. So final result should be displayed on table at template.
As I'm new to django, how should I have to pass the data and fire query to represent data at multiple field. As help or link related to these type problem are expected. ( I just able to filter from the database with only one form and had no concept to solve this.)
Model of my temp project is as below.
class exReporter(models.Model):
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.EmailField()
gender = models.CharField(max_length=1)
age = models.IntegerField()
label = models.IntegerField()
There are a number of approaches you can take, but here is one solution you can use that involves chaining together the appropriate filters based on the form's posted data:
*Note: To conform to Python's naming convention, rename exReporter class to ExReporter.
# views.py
def process_ex_reporter_form(request):
if request.method == "POST":
# ExReporterForm implementation details not included.
ex_reporter_form = ExReporterForm(request.POST)
if ex_reporter_form.is_valid():
# If form field has no data, cleaned data should be None.
gender = ex_reporter_form.cleaned_data['gender']
age_start = ex_reporter_form.cleaned_data['age_start']
age_end = ex_reporter_form.cleaned_data['age_end']
aggregation_group = ex_reporter_form.cleaned_data['aggregation_group']
aggregation_id = ex_reporter_form.cleaned_data['aggregation_id']
ex_reporters = ExReporter.objects.get_ex_reporters(gender, age_start,
age_end, aggregation_group, aggregation_id)
else:
# Pass back form for correction.
pass
else:
# Pass new form to user.
pass
# models.py
class ExReporterManager(models.Manager):
def get_ex_reporters(self, gender, age_start, age_end, aggregation_group,
aggregation_id):
ex_reporters = super(ExReporterManager, self).get_query_set().all()
# Even though the filters are being applied in separate statements,
# database will only be hit once.
if ex_reporters:
if gender:
ex_reporters = ex_reporters.filter(gender=gender)
if age_start:
ex_reporters = ex_reporters.filter(age__gt=age_start)
if age_end:
ex_reporters = ex_reporters.filter(age__lt=age_end)
# Apply further filter logic for aggregation types supported.
return ex_reporters

Django form cache or reload issue

I have a page to insert value to db.
After inserting am loading another page with a drop down listing the db values
But the inserted value is not listing in dropdown
The problem is not with transaction/commit etc. The query to retrieve the data for the drop down in second form is correct.
Form1(first page)
class Organization(forms.Form):
orgList = getOrgUnitList()
orgUnit = forms.CharField(label=u'Organization Name',
max_length=50,
error_messages={'required':'Organization name is required field.'})
parentOrg= forms.ChoiceField(label=u'Parent Organization',
choices=[(u'Select',u'Select')]+orgList,
error_messages={'required':'Organization name is required field.'})
Form2(Second page)
class User(forms.Form):
orgUnitList = getOrgUnitList()
email = forms.EmailField(label=u'Email',
max_length=50,
error_messages={'required':'Email is required field'})
orgUnit = forms.ChoiceField(label=u'Organizational Unit',
choices=orgUnitList,
error_messages={'required':'Organizational unit is required field'})
Query
def getOrgUnitList():
orgUnitList = list(OrganizationUnit.objects.values_list
('OrgUnitID','OrgUnitName').order_by('OrgUnitName'))
return orgUnitList
but when i tried to bind the choices in view it is working
working code
*view*
def user()
template = get_template('AddUser.html')
form = AddUser()
orgUnitList = getOrgUnitList()
del objAdminUIDA
form.fields['orgUnit'].widget.choices=orgUnitList
variables = RequestContext(request,{'form':form})
output = template.render(variables)
del form
return HttpResponse(output)
But i cant give the dropdown choice in view i want to give choices in form.i need a solution for form2
Firstly, the orgList is evaluated in form definition, that's why choices don't change. You should place getOrgUnitList in form's __init__ (or in some other method).
Secondly, you don't pass any data to the form, may be you want
form = AddUser(request.POST or None)

Blank option in required ChoiceField

I want my ChoiceField in ModelForm to have a blank option (------) but it's required.
I need to have blank option to prevent user from accidentally skipping the field thus select the wrong option.
This works for at least 1.4 and later:
CHOICES = (
('', '-----------'),
('foo', 'Foo')
)
class FooForm(forms.Form):
foo = forms.ChoiceField(choices=CHOICES)
Since ChoiceField is required (by default), it will complain about being empty when first choice is selected and wouldn't if second.
It's better to do it like this than the way Yuji Tomita showed, because this way you use Django's localized validation messages.
You could validate the field with clean_FOO
CHOICES = (
('------------','-----------'), # first field is invalid.
('Foo', 'Foo')
)
class FooForm(forms.Form):
foo = forms.ChoiceField(choices=CHOICES)
def clean_foo(self):
data = self.cleaned_data.get('foo')
if data == self.fields['foo'].choices[0][0]:
raise forms.ValidationError('This field is required')
return data
If it's a ModelChoiceField, you can supply the empty_label argument.
foo = forms.ModelChoiceField(queryset=Foo.objects.all(),
empty_label="-------------")
This will keep the form required, and if ----- is selected, will throw a validation error.
You can also override form's __init__() method and modify the choices field attribute, reasigning a new list of tuples. (This may be useful for dynamic changes):
def __init__(self, *args, **kwargs):
super(MyForm, self).__init__(*args, **kwargs)
self.fields['my_field'].choices = [('', '---------')] + self.fields['my_field'].choices
in argument add null = True
like this
gender = models.CharField(max_length=1, null = True)
http://docs.djangoproject.com/en/dev/ref/models/fields/
for your comment
THEME_CHOICES = (
('--', '-----'),
('DR', 'Domain_registery'),
)
theme = models.CharField(max_length=2, choices=THEME_CHOICES)

Adding data to many-to-many field of a modelform within a view

class Person(models.Model):
name = models.CharField(max_length=100)
class Entry(models.Model):
text = models.CharField(max_length=100)
person = models.ManyToManyField(Person, blank=True, null=True)
class MyModelForm(forms.ModelForm):
class Meta:
model = Entry
In my view, I need to add pk id's to a submitted form before saving it.
data = request.POST.copy()
# 'person' is a ManyToManyField to a 'Person' model
# a form would normally send multiple id's as POST in this format -> u'id': [u'1', u'2']
# u'1,2' (an example) is a str variable accessible to the view
data[u'person'] = u'1,2'.split(",")
form = MyModelForm(data)
if form.is_valid():
form.save()
This gives me:
int() argument must be a string or a
number, not 'list'
Which is fair enough. It does work in case of:
data[u'person'] = u'1'
I also tried this with no success:
new_form = form.save(commit=False)
new_form.person = u'1,2'.split(",")
new_form.save()
form.save_m2m()
How can I save multiple id's to this ManyToManyField?
Must be easy but I am missing the point.
EDIT:
The desired result is one new instance of MyModelForm (in the 'entry' table) with all id's stored for form.person (multiple records in the 'entry_person' table).
UPDATE:
I seem to have isolated the problem.
If I do:
data = {}
data[u'person'] = u'1,2'.split(",")
It does work as the result is:
{u'person': [u'1', u'2'],}
If I do:
data = request.POST.copy()
data[u'person'] = u'1,2'.split(",")
It does NOT work (gives the error described above) as the result is:
<QueryDict: {u'person': [[u'1', u'2']],}>
So all I need is to have
<QueryDict: {u'person': [u'1', u'2'],}>
Any suggestions how?
QueryDict.setlist(key, list_) solves this problem.
Answered in A workaround for Django QueryDict wrapping values in lists?
The split you entered returns the following:
[u'1', u'2']
To create multiple instances in your database you'd need to iterate over the list:
for x in u'1,2'.split(","):
data[u'person'] = x
form = MyModelForm(data)
if form.is_valid():
form.save()
Out of curiosity, why string to list conversion in the first place? Or is this just a general example
Try:
new_form.person = [int(x) for x in u'1,2'.split(",")]
This will set new_form.person to a list of ints, not a list of strings.