Can anyone explain the format_output method and render method in django Multiwidget.
I have a Select box, a text field for email input and a checkbox. I have created a multiWidget but I want to have a add more feature, like if somebody clicks on the Add More button, javascript will replicate the set of fields that created by the multiWidget.
Also i want to render html like this :
<input type = "text" name=textbox_name[] />
how can I achieve this.
Thanks in advance.
Take a look at Django Formsets and related posts
dynamically add field to a form
Related
In short:
I would like to fill in some form fields in django admin, before I click the save button, based on the input in another field.
The longer version:
I have 2 models, SteamGame and GameInfo which has a ForeignKey to SteamGame. The SteamGame table gets filled once a day with all the games listed on Steam, by fetching and parsing http://api.steampowered.com/ISteamApps/GetAppList/v0002/, the GameInfo table gets filled manually using the admin site. When adding to GameInfo I select the game from SteamGame. Some of the fields are for info I add myself, some of the fields I want to be populated after selecting the game and fetching https://store.steampowered.com/api/appdetails?appids=GAMEID&l=en for that information.
Are there some magic Django functions I can use to accomplish this? Or any other suggestions on how to do this?
Thanks!
you can pass a javascript file and using query you can make a ajax call and append the values to the form using jquery .
class HistoryConfigurationAdmin(admin.ModelAdmin):
class Media:
js = ("customjs/yourjsfile.js",)
and create a yourjsfile.js in static folder of you project.note for using jquery in django admin instead of using $(document).ready we use "jq" instead of "$" symbol
You can use javascript to achieve that.
On your admin model, add this to call for a custom javascript:
class MyAdmin(admin.ModelAdmin):
class Media:
js = ('/static/js/myMagicJavascriptThatDoesAllThisStuff.js',
)
The JS will allow the user to see what will be saved BEFORE saving it.
If you want to pull the information after the save, you should override the admin save function
I have a model method in Django that I am displaying on an admin page just like I would a model field. With a field, I can just add a help_text argument to it to give a description of what the field is and what the user should put into it. However, with a model method, help_text does not work. Adding the attribute short_description changes the way the method name is displayed, which is sort of okay, but I'm looking for a way to add a few sentences of description beneath the method value that is displayed. Is there any way to do this natively, or would I have to resort to overriding admin templates or something? (Which I do not think is worth it for something this minor).
You can do this using JS.
Replace ID-OF-THE-FIELD with the actual id of the desired field.
(function($) {
var myField = $('#ID-OF-THE-FIELD');
// find the id of the desired field by doing
// Right-Click > Inspect element
var help = $('<p class="help">A very long help text</p>');
help.insertAfter(myField);
})(django.jQuery);
Put this code into a JS file and supply this file using class Media of your ModelAdmin class.
Is it possible to render a Django model CharField as a checkbox? I need this checkbox on admin edit page as well as the list_editable list page. E.g. when checked, set this CharField value to 't', else set it to 'n'
You can do a form changing the widgets no matter the type you are using.
Here is the documentation
Then if you need to change other things depending on the input people gives I guess you should do some jQuery creating a $('#mycheckboxid').click function
How do I get the selected value from a form's ComboBox field? what is the model class that deals with ComboBoxes? ..
Thanks.
There's no such thing as a ComboBox in Django (or in HTML). I assume you are talking about a ChoiceField, which renders a select control in HTML.
You access the value of a ChoiceField in exactly the same way as any other field, once the form has been submitted and validated - by accessing form.cleaned_data['fieldname'].
You should read the excellent documentation on forms.
As mentioned by #MMRUser, the ChoiceField is the form class to achieve an HTML select element.
But for the model itself, you can pass the choices argument to a model field (typically a CharField) which will result in the ModelForm using an HTML select element.
I want to change the shape of some fields showed in the admin site.
I found that the template that manage everything is change_form.html with fieldset.html but I cannot find where the fields are actually transformed in html.
Basically I want to change the field of the foreign key adding a link to another page.
Do you have any idea?
Thanks,
Giovanni
The HTML for a given field is handled by its widget in the render function. If you want to customize the look of a field you could create a custom widget which has the additional HTML you need in the render.
You can check out the render some of the built in widgets in django/forms/widgets.py (links to the Django trunk).
In fieldset.html, the code {{ field.field }} renders the field's HTML representation. to achieve what you want, you'll probably need to define your own widget. you can take a look at admin's widgets.py