How to create a one parent form having a two child forms within it? - django

I am trying to achieve this : I want one parent form which have a "submit" button and it should contain the two child forms in which one of the child (here suppose have two child child-1 and child-2 ) child-1 gets added to form dynamically when user click "add child-1". How to achieve this in django forms ?

I think you can create form with hidden fields(child-1) and show them with jQuery after pressing on "add child-1" button.

If your children are of the same origin (ex. a form asking if you have chlidren; you add one child and then another), then you need formsets:
https://docs.djangoproject.com/en/dev/topics/forms/formsets/
If you want a single form to be broken in parts/steps (ex. step 1: fill in your address; step 2: provide information about your company; step 3: etc...), you need a form wizard:
https://docs.djangoproject.com/en/dev/ref/contrib/formtools/form-wizard/

Related

Can I generate a form depending on the results of a view?

I need to create a form which depends on the results of a view. I can generate it within the view and/or create a view template in order to do this....But how do I process it?
How do I connect my form/template generated form to a form_submission function?
When this custom/dynamic form is submitted I need to call some drupal functions to create some content in the site.
I did not find any way to do it in a view, but what I did was:
- Create a form programatically, making only a hidden field for NIDS
- Place my view next to that form using AJAX
- When the view is updated I copy the results of the view to the NIDS field and submit the form (This reloads the page)
- Form submission has 2 behaviours, 1 to re-generate the form when provided with NIDs, and other one for the actual form created by the NIDS result of my view.
Just in case this helps someone

Where do I set fields that can be edited in Sitecore editor options?

Where in sitecore do I configure what fields are displayed when I click this button? (Currently, it lists 3 of the 7 fields that are on the template, I'd like to add another to be editable).
Screenshot
It is one of Edit Frame buttons. When you open your control markup, you see something like this:
<sc:EditFrame ID="editFrame" runat="server" Buttons="/sitecore/content/Applications/WebEdit/Edit Frame Buttons/Default">
...
</sc:EditFrame>
It differs for MVC, but is similar. You need to get Buttons attribute value.
When you open /sitecore/content/Applications/WebEdit/Edit Frame Buttons/Default path in core database you see list of children. It is your buttons. For your case it should be only one button: Edit Item. This item has Fields field where using pipe as delimiter is list of fields that are available for editing.

Form of a list of instances

I am missing an idea or a way to handle this in django.
I have a list of instances (class Sample) and I want to present the last 10 of them in a form with a checkbox before... I want to have a button "work on these" to submit the checked instances to a view. This view should present a detailView of the selected instances.
I don't know, how to handle this exactly. Can somebody give me a hint? All I did so far was just creating a form of a instance.
Thanks in advance.
You can use a ModelChoiceField or a ModelMultipleChoiceField.
class MyForm(forms.Form):
my_list_of_instances = forms.ModelMultipleChoiceField(queryset = MyModel.objects.all().order_by('-id')[:10], widget=forms.CheckboxSelectMultiple)
This should generate a list of checkbox with your last 10 instances created.
With this you have your form ready to receive your input...just point your action to your view.
Edit:
In case you want to have several forms, one for each instance, you can work with Formsets. You would not get the checkboxes tho...you would get any change applied to your instances. If you want to have both (checkboxes and formsets) you can use both suggestions, using the checkbox values sent to the server to filter the formsets you are going to save.

How to add modelforms dynamically?

I have a simple use case.
I have 3 models foo, foo1 and foo2. I have created model forms for them.
Now in a page I have these forms together on that page.
PROBLEM : The user has the option to click on a "ADD" button. Once he clicks on it he will be able to add 1 more foo2 i.e. 1 more foo2 form will open or be displayed. How should I do this ? i.e. how do I add a modelform dynamically ?
Edit:
This can be done using jquery. But my question is how do I differentiate it in the view ? Should I use prefix ? i.e. should I add dynamic prefix to the forms that are being created ?
As Rohan says in the comments, if you have more than one of the same kind of form on a page, you should be using formsets. Each form within the formset has its own prefix which includes an ID which you can simply increment with your Javascript.

django admin Add custom button + custom form

I want to add a custom button near 'Add model_name'. When i click on the newly created button i should like to show a custom form where I cans elect a model out of a select box. When i click on save I want to save this model and chance some parameters so it's a 'add' but without selecting all options again. I give a clear example:
I have a model names 'Book'. The first time i create a new book entry, i have a form 'add Book' and i have to fill out the form completely. So i have the book with primary key = Book_1_1 But now i want to add a second book, it's the same book as the first 1 BUT the version changed, so i want a new book but i don't want to select all items anymore in the standard 'add Book' form, i want something like i click on create new instance --> i can select 1 book out of a select box with all book objects in it, and when i 'save' this a new instance of the book gets generated. This instance has the following primary key: Book_1_2 for example. I know how to save this, but i don't know how to change the admin site to do this. I need 2 things:
1) add a button 'new instance' near 'Add_model_name'
2) Deliver a form with all model_name objects in a select box and when i click save i want to retrieve an object with which i can modify some things to save it as 'new book'.
Any ideas?
UPDATE I already added the 'new' button, but like i can see at this moment instead of the url = add , i have to create a new url inside the admin like add_instance etc. Does someone have any documentation on this?
Regards,
Hein
Making it way too hard on yourself. Just do this:
class MyModelAdmin(admin.ModelAdmin):
# Other stuff here
save_as = True
Now you can open up your book entry, change whatever is different and hit "Save as new" and it'll create a new book with that info instead of overwriting the other.