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
Related
The Django docs say you can add a form to the admin UI:
class ArticleAdmin(admin.ModelAdmin):
form = MyArticleAdminForm
I want a custom editing UI for a special field in my model, where I display multiple widgets. (It's not exactly the same, but an analogy might be an old-school hex editor widget, where you want fine editing control on a big blob of information.) Perhaps I could break the multiple values into multiple database objects and use an InlineAdmin, but I have app-specific reasons to not do that.
I thought I'd use a Form object with some custom fields, but Django says it must be a ModelForm:
<class 'myapp.admin.MyAdmin'>: (admin.E016) The value of 'form' must inherit from 'BaseModelForm'.
Is it possible to display multiple widgets (basically a very custom form) for a single model value in Django admin?
EDIT: It looks like MultiWidget might work? I'm gonna look into that. Also, this question is related. That suggests I should just change the widget on the field.
The answer was to make a MultiWidget, overriding:
__init__ to set up the widgets
decompress and value_from_datadict to unpack and pack the field value
template_name to render my own template
get_context to make the context for the template
I am rewriting some administration interface to django 2.2, currently using django autocomplete_fields admin feature. Simply said I have ModelAdmin object OrderAdmin, which has nested TabularInline ProductAdmin: variable-length table of products which might be added to order. Each of these ProductAdmin holders just contains ForeignKey to actual product class, with some other attributes.
Now I wonder: where does django store id - ForeignKey - of item selected with autocomplete field? It doesn't mark OPTION in selectbox as selected, and although there is suspicious hidden input field with #cashregisterproduct_set-0-id on page, it doesn't have any value. Or is there some special way how to access it? I was thinking about adding id to __str__ method of model and parsing, but thats just ugly.
Thanks for tip.
EDIT: to make it 100% clear, where from does django get ForeignKey of object selected through autoselect_field, when creating new object from ModelAdmin?
I got misguided thinking that this is managed by django. Selected data might be accessed by using select2 framework:
selected_value = $('.myselectbox').select2().val();
related: https://stackoverflow.com/a/47451658/16268461
In the admin form, how do you filter a django choicefield on the basis of value of another choicefield.
For example, if a choicefield is having "fruits" and "vegetables", then on selection of fruits, the second choicefield should have "apple,pear,orange" etc.Similarly one selection of vegetables the field should show "brinjal,lady finger,cabbage"
Thanks in advance
You could probably accomplish that with grouped selects in django-smart-selects. The default is outputting a drop down menu. And you should be able to modify it to output radio buttons instead.
I built a form in django and I'm having trouble debugging my Tours Choices. It is a ChoiceField and I use the CheckboxMultipleSelect Widget. I don't know what I'm doing wrong to get the error in the screenshot below. Any thoughts? Do I need to facilitate more information? I'm a django newbie.
Picture of the Form Error
A form.ChoiceField lets you input exactly one choice for a form (out of a selection of several choices). If you saved it to a database in a model there would be one value stored in the database. Your CheckboxMultipleSelect widget is for a form.MultipleChoiceField where you can input multiple values. So change your ChoiceField to a MultipleChoiceField in your form and you should be fine. If you save this data to a model, that model would have to be an appropriate field, like a ManyToManyField if they are Foreign Keys.
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.