Git Product home page Git Product logo

Comments (13)

applegrew avatar applegrew commented on August 17, 2024

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.

hobbestigrou avatar hobbestigrou commented on August 17, 2024

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.

applegrew avatar applegrew commented on August 17, 2024

Did you submit the page after you clicked on the cross to delete it?

from django-select2.

hobbestigrou avatar hobbestigrou commented on August 17, 2024

Yes, I submit the form via the button send.

from django-select2.

hobbestigrou avatar hobbestigrou commented on August 17, 2024

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.

applegrew avatar applegrew commented on August 17, 2024

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.

brunobord avatar brunobord commented on August 17, 2024

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.

  1. edit a model linked to "tags" using the ManyToManyField standard model field
  2. save it with two tags. let's call them "foo" and "bar".
  3. 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.

applegrew avatar applegrew commented on August 17, 2024

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.

Natim avatar Natim commented on August 17, 2024

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.

applegrew avatar applegrew commented on August 17, 2024

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.

applegrew avatar applegrew commented on August 17, 2024

Done. Get latest version.

from django-select2.

hobbestigrou avatar hobbestigrou commented on August 17, 2024

Hi,

Thank you very much for the implementation, it works very well. Sorry for not being able to make a pr.

from django-select2.

applegrew avatar applegrew commented on August 17, 2024

Np. Gr8 to know your feedback.

from django-select2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.