applegrew / django-select2

This is a Django integration for Select2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

trying to get the docs straight

mfrasca opened this issue · comments

Goal
I'm trying to use django_select2 in a botanic software. I have a Taxon table, potentially in the hundred of thousands, and initially in the tens of thousands. I need to link this to Plant and Accession (these also easily in the tens of thousands), via a few intermediate classes. in the end, I would have a Verification class, providing a many-to-many link between Taxon and Accession and Contact. Obviously I need something like select2 and lazy querying.

Problem and Code Snippets

I'm having several problems actually, not just one.


for one, the {{ forms.media.css }} and {{ forms.media.js }} do not expand to anything. at least, not in my own project. I added <!-- form: {{ form }} --> in my code, just to check what's going on, and the whole {{ form }} is empty. so I built a separate project, not sure what's the differences, or the relevant differences at least, and I managed to grab the pieces of code that I was missing.


Reading the docs at pythonhosted: https://pythonhosted.org/Django-Select2/django_select2.html#module-django_select2.forms:

image

I tried to use the second, explicit form, and it does not do a thing, not in my own project, and not in the simplified one. I managed to use the first more compact form. see my pull request #542 .


I searched the internet for simple tutorials that would include Django Forms, I found one on Django Girls, which I forked and to which I added a one-beyond-the-last page, showing how to use django-select2. https://github.com/mfrasca/tutorial/tree/master/en/select2_elements

this became my working project, that is the one where I can successfully use your Select2Widget, for a choices field. you can read all that I did in the README.md there.

next step, as said, I didn't manage to get it through.

without altering the database structure, I simply added the author of a blog post to the editable fields, however not realistically reasonable.

modified   blog/forms.py
@@ -1,3 +1,4 @@
+from django.contrib import auth
 from django import forms
 from django_select2 import forms as s2forms
 
@@ -9,5 +10,7 @@ class PostForm(forms.ModelForm):
         model = Post
         fields = ('category', 'title', 'text', 'author', )
         widgets = {
-            'category': s2forms.Select2Widget
+            'category': s2forms.Select2Widget,
+            'author': s2forms.ModelSelect2Widget(model=auth.get_user_model(),
+                                                 search_fields=['first_name', 'last_name', 'username', 'email']),
         }

this is what I get when I click on the blog author form field:
image

and this happens when I type a letter into it:
image

while both 'ola' and 'mario' would match.

I found it reading the sources. we have to use a trailing criterion for the match. like

 search_fields=['first_name__icontains', 'last_name__icontains', ]),

I'll add this to the pull request.