I have a many-to-many field on one of my models and a ModelForm to represent it. I have it in a template but it shows up as a multiple select field. I need it to show up as a CharField so the user can put in comma-delimited values. Is there any way to do this?
Take a look at the code in django.contrib.admin.widgets.ForeignKeyRawIdWidget to see how the admin's raw_id_fields are implemented and try specifying it as the widgets= kwarg when you define that field on your form.
Related
I have a field by the name related in my model
related=ArrayField(models.IntegerField(),null=True)
I want this field to appear as a choicefield with multiple selection. How do i set the field in my form to achieve this?
You can try with third party multi select field. link
I have client model which has a foreign key field to a country model.
So in Django admin, when I create a client and I select the country where this client belongs to. but the problem is the select list is too long(too many countries on this planet). Sometimes it takes just too long to get the one I need.
So I wonder if there is other widget in djano admin that provides a select-input-combo widget.
When I type in the input and it will filter out the right one for me to select.
Hope you can understand what I need here.
did you see raw_id_fields ?
you would do in admin.py something like:
class ClientAdmin(admin.ModelAdmin):
raw_id_fields = ("country",)
admin.site.register(Client, ClientAdmin)
then select widget will become something like:
Since Django 2.0 there is autocomplete_fields. From the documentation:
autocomplete_fields is a list of ForeignKey and/or ManyToManyField fields you would like to change to Select2 autocomplete inputs.
and
The Select2 input looks similar to the default input but comes with a search feature that loads the options asynchronously. This is faster and more user-friendly if the related model has many instances.
Note that you need to define search_fields in the related object's ModelAdmin since it is used by the widget.
Say I have a single form that has a field type and depending on what type the user inputs, the data is placed into a model/table. How do you do this with django forms?
Thanks!
In Django, a form for more than one model is called a FormSet.
I've created a onetomany relationship between two models. The model
that has the foreignkey I can use their formset without problems, but
now I want to fill this relation through the form of the model that
has the "many", I know I can retrieve it using the *_set, but how
can I create a MultipleChoiceField form element of this data?
Thanks for any help!
Regards,
Thiago
Have a look at the docs for fields which handle relationships. You could use a ModelMultipleChoiceField -- it's essentially a MultipleChoiceField that takes its choices from a queryset.
Is the exclude list in a ModelForm any different from an exclude list in a ModelAdmin? If my ModelForm is tied to the ModelAdmin, where do I need to specify the exclude list ideally; in the ModelForm Meta class or in the ModelAdmin subclass?
Depends if you want to do something else with the form. If its displayed somewhere else than the admin and you want to exclude the same field there as well, define it in the ModelForm. If the ModelForm is just used in the admin and nowhere else you basically can choose what makes more sense for you. I personally would still keep it in the ModelForm so this functionality is tied to it instead to the admin.
Edit (see comments below):
Apparently there seems to be a bug in Django. If I exclude something in a ModelForm and then use this form in the ModelAdmin it is still showing this field for some reason. You better exclude in the admin only to be 100% sure or specify fields in the ModelForm without the field you want to exclude.