applegrew / django-select2

This is a Django integration for Select2
MIT License
710 stars 316 forks source link

Django ModelSelect2MultipleWidget for ManyToMany field not working #615

Closed ap9313 closed 4 years ago

ap9313 commented 4 years ago

Hi! I'm trying to use a ModelSelect2MultipleWidget to represent a manyto many field from my model. And i can't get it to work

class Artikel(models.Model):
    id = models.AutoField(unique=True, primary_key=True)
    artikel_typ = models.IntegerField(unique=False, default=0)
    bezeichnung = models.TextField(max_length=50)
    menge = models.FloatField(blank=True, null=True)
    preis = models.FloatField(blank=True, null=True)
    einheit = models.TextField(blank=True, null=True)

    def __str__(self):
        return self.bezeichnung

class Leistung(models.Model):
    id = models.TextField(unique=True,primary_key=True)
    bezeichnung = models.TextField()
    dauer = models.FloatField(blank=True, null=True)  # This field type is a guess.
    preis = models.FloatField()  # This field type is a guess.
    artikel = models.ManyToManyField(Artikel,null=True, unique=False)

    def __str__(self):
        return self.bezeichnung
    class Meta:
        db_table = 'leistung'

this is where i define my form in my form.py:

class AddNewServiceForm(forms.ModelForm):

class Meta:
    artikel_dd = Artikel.objects.all()

    model = Leistung
    fields = '__all__'
    widgets = {'id':forms.TextInput(attrs={'id': 'id', 'name': 'id', 'type': 'text', 'class': 'form-control',
               'data-val-required': 'Bitte tragen Sie eine 4MyHealth ID ein!', 'aria-required': 'true',
               'aria-invalid': 'false', }),
        'bezeichnung': forms.TextInput(
        attrs={'id': 'bezeichnung', 'name': 'bezeichnung', 'type': 'text', 'class': 'form-control',
               'data-val-required': 'Bitte tragen Sie eine Bezeichnung ein!', 'aria-required': 'true',
               'aria-invalid': 'false', }),
        'dauer': forms.TextInput(
            attrs={'id': 'dauer', 'name': 'dauer', 'type': 'number', 'step': '0.01', 'class': 'form-control',
                   'data-val-required': 'Bitte tragen Sie die Dauer ein!', 'aria-required': 'true',
                   'aria-invalid': 'false', }),
        'preis': forms.TextInput(
            attrs={'id': 'preis', 'name': 'preis', 'type': 'number', 'step': '0.01', 'class': 'form-control',
                   'data-val-required': 'Bitte tragen Sie die Dauer ein!', 'aria-required': 'true',
                   'aria-invalid': 'false', }),
        'artikel': ModelSelect2MultipleWidget(queryset=Artikel.objects.all(),attrs={'id': 'artikel', 'name': 'artikel_dropdown',
                                                            'aria-required': 'false',
                                                            'aria-invalid': 'false', }),

    }

this is a snippet of my template where i use the form.

        {% load static %}
    <!DOCTYPE html>
    <html lang="en">

    {% block javascript %}
        <script type="text/javascript">

        </script>
    {% endblock %}
    {% block jquery %}
        <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
        <script type="text/javascript">

        </script>
        <script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    {% endblock %}

    <script src="{% static 'django_select2/django_select2.js' %}"></script>

    <link href="/static/website/js/select2.min.js" rel="stylesheet" />
    <script src="/static/website/js/select2.min.js"></script>
    <head>
        {% include 'head.html' %}

        <!--Leistungen -->
        {% load static %}

    <body class="animsition">

    <div class="page-wrapper">

        {% include 'sidebar.html' %}

        <!-- PAGE CONTAINER-->
        <div class="page-container">
...
            <div class="row form-group">

              <label class="label mb-1">Verfรผgbare Artikel:</label>
                {{ forms.artikel }}
              </div>
....

this is the setting.py

SELECT2_JS = 'assets/js/select2.min.js'

# Set the cache backend to select2
CACHES = {
    'default': {
        'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache',
        'LOCATION': '127.0.0.1:8000',
    },
    'select2': {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:8000",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",
        }
    }
}

SELECT2_CACHE_BACKEND = 'select2'

and i also added the path to my url.py like path("select2/", include("django_select2.urls"))

this is what i got before setting up the cache with django_redis.cache.RedisCache:

Screenshot 2020-07-08 at 11 11 54

after setting the cache like in the docs, i get a connection error:

Screenshot 2020-07-08 at 12 19 20

Can you help me out? What am i missing/ what am i doing wrong? Thanks a lot! ๐Ÿ˜Š

codingjoe commented 4 years ago

Hi @ap9313,

looking at your stack trace, it looks like you are working on macOS.

It like redis isn't running. Would you mind restarting brew services restart redis and trying again?

Best, Joe

ap9313 commented 4 years ago

Hey @codingjoe,

yes I'm using macOs, pyCharm as editor and an sqllite db for the moment. I ran the command to restart the service and it didn't work. i installed i prior with pip install.... now i also installed it with brew and started the service, added the ip and port on which it runs and it still doesn't work. I get the form and see the field but it has no data in it! when i type something in the field, i get the following console error: Failed to load resource: the server responded with a status of 500 (Internal Server Error).. any hints?

Regards, Attila

codingjoe commented 4 years ago

Hi Attila, you need to make sure, that you have Redis server installed and setup. The latter is simple, since the brew version is preconfigured correctly. A simple brew install redis should solve your issue.

If it doesn't I recommend reading into the Redis and Django-Redis documentation.

Best, Joe

ap9313 commented 4 years ago

Hi Johannes,

as i said: i installed it with brew afterwards, started the service and it still doesn't work. i switched from ModelSelect2MultipleWidget to Select2Widget and the widget works! but when i try to save/add/update (aka POST) a service: it doesn't work ๐Ÿ™ˆ๐Ÿ™ˆ!

Thanks for the tipps! I'm one step ahead now, i'll have to figure out the next solution now ๐Ÿ˜…๐Ÿ‘Œ๐Ÿป

Regards, Attila

codingjoe commented 4 years ago

Since this seems to be a different issue, could you open a new ticket for that. Preferably on the proper repo: https://github.com/codingjoe/django-select2/issues

Best, Joe