django admin Add custom button + custom form - django

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.

Related

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 create a one parent form having a two child forms within it?

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/

Using jquery autocomplete Django form

I am using jquery autocomplete with my generic views on Django. I am getting the list via AJAX, but there is one problem. When the user get the category he wants, he will select that value. Thus, if I append the value on that field $("#id_category").val(ui.item.value); it will show the pk on that field instead of value, so if I append $("#id_category").val(ui.item.label); django will complain it should be the instance. Django is completely right. How to make this work?
Edit:
I have made id_category hidden field and added category_display to append the text value. It is working. But while editing the category_display would be empty. Again I am using generic view. How to solve this one on the edit?
I had similar issue and I suggest you to think about question: 'What will happen if user enters not valid category name(category with this name doesn`t exist)?'
if you need to accept entered value and add new category, you have to make clean_category method in form:
def clean_category(self):
name = self.data['category']
return Category.objects.get_or_create(name=name)[0]
and display ui.item.label for user
if new category isn`t acceptable you can raise error in clean_category method or use something like select2(http://ivaynberg.github.io/select2/) for your field. I choose second variant.

How do I change the template of the Mailchimp Wordpress widget?

I want to change how the form looks like and the labels on the fields of the form.
Login in as Admin and then, under the Plugins area in the sidebar, click Editor. There's a dropdown menu labeled "Select plugin to edit". Click that and select "MailChimp" and then click the "Select" button. The sidebar widget form is called mailchimp/mailchimp_widget.php
The form's code begins right after the first PHP block.
You can also edit the code directly by looking in the wordpress/wp-content/plugins/mailchimp/ directory. The translations are in the po sub-directory.
The trick with this template is that the fields are loaded from elsewhere. In order to change the label, you have to set the option of the fields in the PHP code. Each field is looped through and printed out automatically.
For example to change the "Email Address" label to read "Email" add the following code at the end of the first PHP block:
$mv[0]['name'] = 'Email';
This assumes that the first field that will be printed out is the Email Address field. You can do a var_dump to see what other options are available.
If you want to make more drastic changes to the form, remember that when the widget is updated, you'll have to make the changes again and merge them with the updated version.

Magento:How to display drop down attribute on the product listing page

I have created a drop down attribute with a configurable product.
I would like to add in the grid/list product’s page the combo with the choice of my select attribute in order to be able to put in directly from this page the product into my cart wich is impossible for the moment : i have to go on the second page to choose the attribute.
i’ve read a lot of threads but i don’t find a way to achieve it.
thanks for the help.
So you want product "add to cart button" with "custom attributes" to be added to the product listing pages..there's an extension for this, see below:
http://www.mageworx.com/ajax-cart-magento-extension.html
I have only managed to add an "add all item to cart" button and "add to cart" button on my category listing pages.
For the attributes which have a frontend type “Select” we need to define options that on the product page converted into a dropdown. For example attribute color may have options Red, Green, Blue etc. which are defined in the admin area.
We can easily collect all the defined options for any attribute.
// use your own attribute code here
$attribute_code = "color";
$attribute_details = Mage::getSingleton("eav/config")->getAttribute("catalog_product", $attribute_code);
$options = $attribute_details->getSource()->getAllOptions(false);
Foreach($options as $option){
// print_r($option) and find all the elements
echo $option["value"];
echo $option["label"];
}
For more information about magento cart products, visit our Web\Agency-shop