[Answered ]-How to prepare a list for queryset (select2) from multiple m2m filed

1👍

You can do that with Concat and Value() expressions

The following code should do what you want:

from django.db.models import CharField, F, Value as V
from django.db.models.functions import Concat

attrs = list(ProductAttributes.objects.values("id").annotate(
    id_dummy=Concat(
        'product', V('-'), 'size__name', V('-'), 'cupsize__name', V('-'), 'colour__name',
        output_field=CharField()
    ),
    text=Concat(
        'size__name', V('-'), 'cupsize__name', V('-'), 'colour__name',
        output_field=CharField()
    )
).values("id_dummy", "text").annotate(id=F('id_dummy')).values_list("id", "text")

In your place i would use django-select2

my_choice = forms.ChoiceField(widget=Select2Widget(attrs={'data-placeholder': 'Your placeholder'}), choices=YOURE_CHOICES, required=False)
👤NKSM

Leave a comment