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.
Related
I'm using Django 2.0.7.
I want to get all the fields of a model. I can get all the normal fields in:
model._meta.fields
And I can get m2m fields in:
model._meta.local_many_to_many
However, if the m2m field has an through table, I can't access them? How can I achieve this?
The m2m fields have a through model that you can access:
YourModel.m2m_field.through._meta.fields
Did you try this ?
model._meta.get_all_field_names()
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 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.
I have a queryset. I'm trying to display a feedback form for each of items in the queryset. What's a good way to approach this? Attach the model to a fieldset and then iterate through the forms in a fieldset, displaying the model information? Or loop through both the queryset and fieldsets separately in the template?
Do you mean formset? A Django formset is helpful if you have multiple, identical forms on the same page.
A model formset sounds like it would work based on your description.
Link to the docs:
http://docs.djangoproject.com/en/dev/topics/forms/formsets/