Comments (13)
The fields have a pre-decided set of accepted values. Tags require that user be able to create new values. This calls for a new enhancement.
from django-select2.
Hi,
We found a solution with my colleagues, so we make a widget and a personalized field. In the widget was added javascript function that goes:
from django_select2 import AutoModelSelect2MultipleField, AutoHeavySelect2MultipleWidget
class AutoAnswerWidget(AutoHeavySelect2MultipleWidget):
def __init__(self, *args, **kwargs):
kwargs['select2_options'] = {
'createSearchChoice': JSFunction(
'''function (term, data) {
if ($(data).filter(function() {
return this.text.localeCompare(term) === 0;
}).length === 0) {
return {id: "@" + term, text: term}; };}'''),
'tags': 'true',
'tokenSeparators': [",", " "],
}
super(AutoAnswerWidget, self).__init__(*args, **kwargs)
class AutoAnwserChoices(AutoModelSelect2MultipleField):
widget = AutoAnswerWidget
queryset = Tag.objects
search_fields = ['name__icontains']
def clean(self, tags):
tags_id = []
for tag in tags:
if tag.startswith('@'):
tag, created = Tag.objects.get_or_create(name=tag[1:].strip(),
site=self.site)
tags_id.append(getattr(tag, 'pk', tag))
tags_obj = super(AutoAnwserChoices, self).clean(tags_id)
return tags_obj
And it works almost in now deleting does not work, when you press the cross level displays the tag disappears correctly but it is not deleted from the db, do you have any idea why. I am willing to make the code more generic and implement a widget and fields in django-select2, but I would love to watch with you, you better know your code.
from django-select2.
Did you submit the page after you clicked on the cross to delete it?
from django-select2.
Yes, I submit the form via the button send.
from django-select2.
I do not know for what reason but select2-offscreen a hidden input the last value is doubled, I do not think this is normal as you have an idea.
from django-select2.
I am not sure if I understand what you are saying, but that does not look right.
Anyway I will take the opportunity to mention one hack Django-Select2 (DS2) does when handling multi-value fields. Select2 Js creates uses a single hidden input field to store the values. The values are stored delimited by a configurable delimiter. DS2 sets the delimiter to null character, which the user will never normally input. When value in that hidden input changes then DS2's js code renders multiple input hidden fields for each value in the primary input field. So, each of these new fields will have only one value. Also they all share the same name, the name which the primary input had (the name is removed from primary). This hack makes sure when the page is submitted then the field python code gets a list instead of a string.
from django-select2.
DS2 sets the delimiter to null character, which the user will never normally input.
that statement has triggered a "warning sign" in my head. "null" character may has several side effects both in the client-side code and the server-side. AFAICT, "," and ";" are way much more standard and it's easier to split a string using well-known separators rather than an invisible char.
apart from that, I've been able to reproduce the bug.
- edit a model linked to "tags" using the ManyToManyField standard model field
- save it with two tags. let's call them "foo" and "bar".
- go back to the form. The visual list of tags is well displayed, with "foo" and "tags". One can remove them using the GUI.
but...
Here is the HTML:
<input id="id_tags" multiple="multiple" name="" type="hidden" value="3 4 4" txt="foo bar bar" class="select2-offscreen" tabindex="-1">
(please note that I had to replace the "null char" with spaces, because I couldn't copy-paste the HTML code from my browser to this comment form... it's that kind of bad side effects I was expecting)
from django-select2.
Well till now I have had no issues with null character. They are being properly encoded. The reason for going against norms is that I have no prior knowledge of the use-cases where this library could be used. A comma could very well be a valid value. Nonetheless that is configurable. Anyway I have posted a SO question regarding this - http://stackoverflow.com/questions/18786822/what-are-characters-which-can-be-safely-x-www-form-urlencoded
The bug you mentioned is definitely a bug. 👍 There seem to be a problem in heavy_data.js
. However I did not encounter this issue before.
from django-select2.
What about sending multiple values ? tags=1&tags=2&tags=3
and use request.POST['tags'].getlist()
then it's only HTTP standard.
from django-select2.
A working code would help. It seems like a lot work. I took that approach since that required less code and less Django tweaking.
from django-select2.
Done. Get latest version.
from django-select2.
Hi,
Thank you very much for the implementation, it works very well. Sorry for not being able to make a pr.
from django-select2.
Np. Gr8 to know your feedback.
from django-select2.
Related Issues (20)
- Option to choose search_fields for Select2MultipleWidget ? HOT 2
- support django3.0 HOT 2
- DjangoCMS 3.7 HOT 8
- Clean option no works properly HOT 2
- Issues when certain special characters appear in the field_id HOT 5
- How to add limitations to a RelatedFieldMixin HOT 1
- js config file HOT 3
- multi model select field HOT 1
- Release 7.2.1 on PIP HOT 6
- Interdependent selects for fields of one model HOT 3
- ModelSelect2Widget initial value is not selected HOT 6
- How do I load selected options for ModelSelect2MultipleWidget HOT 5
- Disable multiple select on tag widget HOT 5
- ModelSelect2TagWidget tagging new values HOT 9
- Implementation of Select2Widget with formsets. HOT 6
- Use https://<cdn>/ rather than //<cdn>/ for security? HOT 4
- Codecov migration to marketplace app HOT 2
- Django Select2 not working for formset when click on add more. HOT 1
- Django ModelSelect2MultipleWidget for ManyToMany field not working HOT 5
- Empty Dropdown Using ModelSelect2Widget HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from django-select2.