In a django (python) project, I have a foreign key model that reflects a form to fill out. Is it possible to make it so that if the user does not find his option in the drop-down list, he has the opportunity to fill in this field, but with his own option (respectively, with saving to the database). And in the future this option will be shown in the drop-down list. Like in the picture.
So that the user can create a new item if it is not in the dropdown list.
What should be applied to implement this idea? What can be found in the documentation?
Samuel Cormier-Iijima's snippet provides a choice field with an option for the user to specify a choice not yet on the list. You would populate it using the queryset of the foreign key field, and save the new choice to that model when it posts.
Related
So Django model forms make a multiple choice field out of a many to many field in the model. What I would like to do is to have a plus button similar to that of the admin site, that prompts you with a modal to create a new object for the many-to-many field, instead of just being able to select from the queryset of existing ones. Is mtm the right relation to use? Any pointers on how to achieve this?
An example from the admin page where questions is a manytomany field in the Survey model
Short anwer, use ajax in your template. It will allow you to add a new object without refreshing your page.
If you search stack overflow there will be a lot of examples of how to implement ajax.
I got a foreign key model with about 100,000 date.
It's too difficult to select the wanted data. So I'm wondering if there is a humanized way to add foreign key in django admin.
I have tried the raw_id_fields, it's useless.
this don't work.
If you have a raw_id_fields set on your source admin class, you can set the search_fields on the target admin model to be able to filter the result based on the set search_fields, i.e. you will get a search input field in the select list popup, see image below.
Alternatively, django-2.0 just merged a pull request providing support for using select2 widgets in the django admin, see pull request for the full list of supported fields: https://github.com/django/django/pull/6385
With this you can use the autocomplete_fields on a foreignkey field on your source admin class and as before set the search_fields on the target admin class.
Select2 is very handy to deal with such situation. You may check packages for Select2 integration with Django.
django-select2 and django-easy-select2 both are good option to consider.
I have a contact form that is posting values to a model called Enquiry. The Enquiry model has an attribute named enquiry_type which is a dropdown select box on the form. Current options are 'general enquiry' & 'request a call back'.
My question is what is the best practice to allow an admin to add to this dropdown? I was thinking of creating another model named EnquiryType with an attribute .text, & iterate through this in the form? This would allow an admin to create new objects with a .text value of their choice. E.g they could add 'Request an email quote' to the dropdown.
Is this correct thinking or is there a simpler/more accepted protocol? Sorry Im still new to Django!
there is only one requirement - select field must get a list of values or list of tuple (value, description), of course saving those options is the second thing
how you will do this is up to you - you need to find a solution that will fit your needs and skills
simplest is to have secondary table, referenced from primary table as foreign key - you can create very fast admin for this model and it will work straight forward (problems will start when you get a lot of choices...)
other option option is to use any django module, which provide "dynamic settings" - some of them have options to store lists, some of them provide only simple objects, but even then you can store comma separated lists in text field
another option is to store data in files (even .py file that can be imported)
each of those options will be fine, you have the tools, now - choose wisely!
I got a foreign key model with about 100,000 date.
It's too difficult to select the wanted data. So I'm wondering if there is a humanized way to add foreign key in django admin.
I have tried the raw_id_fields, it's useless.
this don't work.
If you have a raw_id_fields set on your source admin class, you can set the search_fields on the target admin model to be able to filter the result based on the set search_fields, i.e. you will get a search input field in the select list popup, see image below.
Alternatively, django-2.0 just merged a pull request providing support for using select2 widgets in the django admin, see pull request for the full list of supported fields: https://github.com/django/django/pull/6385
With this you can use the autocomplete_fields on a foreignkey field on your source admin class and as before set the search_fields on the target admin class.
Select2 is very handy to deal with such situation. You may check packages for Select2 integration with Django.
django-select2 and django-easy-select2 both are good option to consider.
I am trying to generate a webpage that shows a list of objects pulled from a database with a certain state. The list of objects should include a checkbox next to each object, so that if the user checks the object and presses submit, the server will change the state of that object. Since the list of objects is generated on the fly, I'm confused on how to properly write a Django form that will create the necessary checkboxes.
Any ideas?
Thanks,
Alex
You need a model formset
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#model-formsets
This will generate a series of forms, one for each item in the queryset you give it.
Assuming the state is saved as a field on the model, you make a custom ModelForm which only has the state field (if it's a BooleanField it'll show as a checkbox by default)
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#modelform
You can then use this custom form in your model formset:
https://docs.djangoproject.com/en/dev/topics/forms/modelforms/#changing-the-form