I have a problem that I want to solve when I add many Input dynamic for example five input
How do I know to list all Input fields generated dynamically on django knowing that I get one field by name only
date=form.cleaned_data.get('date')
The problem is illustrated in the following form:
If you are using django forms, you can use the prefix to distinguish them.
For example, if you have 5 equal forms:
for i in xrange(5):
form = FormExample(data=data, prefix=i)
date=form.cleaned_data.get('date')
As an alternative you can use formsets, you can learn more about them in here
Related
I have a form with only one field (name of family members).
class FamilyMemeberItem(forms.Form):
name= forms.CharField(label=_('name'), max_length=20)
Now I want my form be sorted (arbitrary order) defined by the user. For example in a family, I want to show A first, then B and then C, while the creation sequence may be C, B and A. Is there anyway to do that?
I searched and realized I should add an order field to my form and override the __iter__() method. Is that the only way? If there is no way to do that without change in form?
And could anyone please tell me about the field can_order of formset_factory? When I add it, an extra filed is loaded next to my form, and that's and integer presenting the number of that field. Can I change and save that so that the order changes?
I answered a similar question you posted.
I'll just repeat the last part here:
If you want to store the order in the database, you need to define a new field in you model and store the order in that. The order of formsets is only present inside a single request/response, after that its gone.
I have to store data, a part of them is predefined but the user can chose to custom it.
What is the best way to store these data in the database?
2 fields, 1 will be an integer field for predefined option and the second will be a string for the custom user input
1 string field, which will contains a json like {predefined: 2, custom: ''}
1 string field which will contains custom string or predefined option id (converted during the request process)
1 string field which will contains the fulltext option even if it is a predefined (some of these predefined options can be long text)
I tried the 1) but double the number of fields for each "custom ready" data doesn't seem to be perfect...
Any idea ?
Considering you might need the following (it's not very clear from your question):
a form where there is an input field for the customizable part of the string
an easy way to refer to the complete string
a way to administer/manage/validate the non-customizable string
=> use two fields:
class TheModel(Model):
# if you have a certain constant number of choices, use ChoiceField
# otherwise use a ForeingKey and create a different model for those
non_customizable_prefix = ChoiceField(null=False, blank=False, ...)
# unique? validators? max/min length? null/blank?
customizable_part = CharField(...)
#property
def complete_string(self):
return '{}{}'.format(self. non_customizable_prefix, self. customizable_part)
This model will provide you with two separate input fields in Django forms or the Django admin, offering easy ways to make the non_customizable_prefix read only or only modifiable with certain privileges.
What is the best way to handle a page with multiple forms and an unknown number of elements in one of the forms?
Lets suppose I have 3 models, Book, Recipe and Ingredient and these need to be displayed on the same web page. Each Book can also hold any number of Recipes and each Recipe can hold any number of Ingredients. Each of these models will also have a form that inherits from ModelForm.
Once the form is displayed to the user, she is free to dynamically add as many Recipes or Ingredients a she needs. This is to be done via JS with no Ajax.
What is the best way to handle this when validating forms? Validating the form for Book is easy but how do I handle the unknown number of Recipes and Ingredients?
The solution is to use prefixes and numbers. So a form will have the prefix 'recipe_1_' and each ingredient in the recipe will have the prefix 'ingredient_1_'.
For the second recipe and second set of ingredients just up the integer.
When submitting the form it's necessary to get the values from request.form and use them to create a form one by one, usually by looping through the content of request.form and using the prefix to decide which Form to use.
I need a Django Form Field which will take a list of strings.
I'll iterate over this list and create a new model object for each string.
I can't do a Model Multiple Choice Field, because the model objects aren't created until after form submission, and I can't do a Multiple Choice Field, because I need to accept arbitrary strings, not just a series of pre-defined options.
Anyone know how to do this?
Just use a regular text field delimited by commas. After you handle the form submission in the view do a comma string split based on that field. Then iterate over each one creating and saving a new model. Shouldn't be too hard.
In my case to process list of strings I used forms.JSONField(decoder="array") in my forms.py in form class
I came up with a solution -- a little hacky but it works for now.
After grabbing the form data, I stash the list in a variable:
event_locations = form_data.get('event_locations', None)
Then I remove it from form_data, so the Django Form never gets the list:
if event_locations:
del form_data['event_locations']
I instantiate my form with form_data, and handle the list separately:
f = NewEventForm(form_data)
...
for loc in event_locations:
#create new models here
I realize this doesn't directly solve the question I asked, because we still don't have a Django Form Field taking a list, but it's a way to pass in a list to a view that takes a form and be able to handle it.
I have a couple fields within a form that I'd like to be an array of values. How can I achieve something like
categories['sale']['price']
categories['sale']['currency']
categories['sale']['frequency']
and
image[0]
image[1]
image[2]
These fields containing array would be part of a form containing regular fields. If possible, how does one deal with validation?
Bonus question: If I had dynamic fields by themselves, I'd use a Formset?