I have a django autocomplete light, with more than 200 options. Therefore, i want to include a button "select all".
I almost managed to do so, with the following code.
$('#facility-types').val(['2', '5', '6', '10', '11', '12', '13', '14']);
$('#facility-types').trigger('change'); // Notify any JS components that the value changed
The problem is, that with django autocomplete light, the option-tags for the select fields are only then created, when the user has manually selected an option.
Furthermore, the option-tag has a data-select-2-id that appears me to be kind of random.
<option value="12" data-select2-id="127">xxxx</option>
Any ideas, how to programatically select all options of a django-autocomplete-light select field?
Related
I searched in the last day for information about how to add a select/deselect all checkbox into my Django admin panel but I didn't find the solution.
I like to have these checkbox for all the columns not just for the first (in this case the user model). I like to manage all the objects with one checkbox click but it is also important to save the choice and if I come back to this site it should show the condition I saved before.
I attached an image about what I like to do.
Consider a Django Admin changelist view looking somewhat like this:
I would like to pre-check some of the checkboxes in the list. Documentation didn't help me further.
Any ideas on how to achieve this in a nice way? Where by nice I mean, as standard as possible.
You could tick the checkboxes using JavaScript. For example, to tick the checkbox for the item with primary key 1, using jQuery, you would do:
$(".action-select[value='1']").prop('checked', true);
Or you could use Django's bundled jQuery if you want:
django.jQuery(".action-select[value='1']").prop('checked', true);
Bear with my in this moment of confusion.
I have a project for a small clinic, which will be made using Python+Django.
It's a "very simple" application: they want their patients list and their consults list.
I will do this in Django, using only the admin interface(because it's the only way I know). It will be separated in two modules, one for patients, other for consults.
Here is my question: When the user adds a new consult on the django admin interface, is it possible for him to double click the name of that patient in the form input to open a window with the patient page? (the django admin patient module page)
Look at the screen-shot above, it's a selector with the lists of all patients(this is one of the inputs when inserting a new consult), the NORMAL BEHAVIOR of this is when you click someone, it highlights, when its highlighted and you click the arrow, it passes to the other site, or when you DOUBLE CLICK someone, it passes to the other side too.
What I wanted to do, is when you double click someone, it opened the patient window. Is this possible?
Thanks in advance.
Is there a widget for rendering a foreign key field in Django admin that can scale to handle an arbitrarily large table and provide a user-friendly interface for looking up a FK?
The default widget in Django 1.3 is a select box that lists every record in the referenced table. For tables containing hundreds of thousands of records...this basically crashes the server. I see there's a ref_id_fields ModelAdmin option, for rendering the field as a simple text box, but that strikes me as a pretty un-user-friendly workaround since it expects the user to know the exact ID for the record they want to reference.
I'm surprised no solution for this is already builtin, but Googling only found me one project implementing a jQuery-powered autocomplete widget. Are there any other solutions for this problem?
are you getting all the static files? raw_id_fields come with an ajax widget, which puts a little magnifying glass next to the text box with the input widget. clicking brings up a popup with a changelist (including e.g. search) that you can use to find the id. (and automatically insert by clicking the entry)
see screenshot (first google image search)
There are several more auto-complete apps for Django: http://djangopackages.com/grids/g/auto-complete/
By default, Django's admin renders ForeignKey fields in admin as a select field, listing every record in the foreign table as an option. In one admin-accessible model, I'm referencing the User model as a ForeignKey, and since I have thousands of users Django is populating the select with thousands of options. This is causing the admin page to load incredibly slowly, and the select is not very useful since it can take a while to scroll through thousands of options to find the one you want.
What's the best way to change the rendering of this field in order to improve page load and usability? I'd like the select field to be replaced with some sort of button to launch a search form popup, or a text field that searches keywords via Ajax to find the Id for the specific User they want to associate. Does admin have anything like this builtin, or would I have to write this from scratch?
Add raw_id_fields to your model to only show the ID instead of a dropdown.
You're right, Cerin, the cause of the slowdown is because Django is populating the <select> element with too many options. You might want to use an autocomplete element instead.
Interestingly, Django 2.0 has introduced a new feature on the admin site, called autocomplete_fields, which I think you will find useful in this case. It uses AJAX.
class ExampleAdmin(models.ModelAdmin):
autocomplete_fields = ['example_field_user']
You can use one of the few autocomplete apps for Django. Check them at Django Packages.
There's also django-extensions that have ForeignKeyAutocompleteAdmin that fit your needs pretty well.
Another option is to add readonly_fields instead of raw_id_fields