DJNAGO Many to Many field MultiSelect Into DropdownList

1,922 views
Skip to first unread message

Maryam Yousaf

unread,
Oct 18, 2021, 9:50:59 AM10/18/21
to Django users
Hi,  

I have one manytomany field in one of my model which is currently coming as Multiselect but I need a dropdown where user can select multiple values as I have huge data to show.

I am trying this in forms.py but it is not showing dropdown field.

Kindly help me out.

Model.py:

class Chain(models.Model):
chain_id = models.AutoField(primary_key=True)
chain_name = models.CharField(max_length=255, unique=True)
chain_type = models.ForeignKey(ChainType, on_delete=models.CASCADE)
history = HistoricalRecords()

def __str__(self):
return self.chain_name

class Meta:
ordering = ('chain_name',)


class Brg(models.Model):
brg_campaign_id = models.AutoField(primary_key=True)
campaign_tracking = models.ForeignKey(CampaignTracking, on_delete=models.CASCADE)
brg_name = models.ForeignKey(Chain, on_delete=models.CASCADE, related_name="primary_brg",
help_text='Add Brgs/chain names for these above campaign has run')
brg_benchmark = models.ManyToManyField(Chain, related_name="competitor_brg", null=True,
blank=True, help_text='Add max.5 other benchmarks brgs to check overall impact')
history = HistoricalRecords()

def __str__(self):
return "Brg names list"

class Meta:
verbose_name = 'Brg Campaign Tracking'

forms.py:
class ChainForm(forms.ModelForm):
class Meta:
model: Chain
# fields = ('campaign_tracking', 'brg_name', 'brg_benchmark',)
widgets = {
'chain_name': Select(),
}

Regards,
maryam

Sebastian Jung

unread,
Oct 18, 2021, 10:08:12 AM10/18/21
to django...@googlegroups.com
Hello,

then change in widgets Select() to SelectMultiple().


Regards


This email and any files transmitted with it contain confidential information and/or privileged or personal advice. This email is intended for the addressee(s) stated above only. If you are not the addressee of the email please do not copy or forward it or otherwise use it or any part of it in any form whatsoever. If you have received this email in error please notify the sender and remove the e-mail from your system. Thank you.

This is an email from the company Just Eat Takeaway.com N.V., a public limited liability company with corporate seat in Amsterdam, the Netherlands, and address at Oosterdoksstraat 80, 1011 DK Amsterdam, registered with the Dutch Chamber of Commerce with number 08142836 and where the context requires, includes its subsidiaries and associated undertakings.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/ac59ba2a-5baa-4302-88c9-0dd17606fdaen%40googlegroups.com.

Maryam Yousaf

unread,
Oct 18, 2021, 10:23:37 AM10/18/21
to django...@googlegroups.com
It is already selectmultiple . I need dropdown where I can select multiple values .
currently, it is coming like this:

Screenshot 2021-10-18 at 16.22.02.png

Sebastian Jung

unread,
Oct 18, 2021, 10:42:16 AM10/18/21
to django...@googlegroups.com
When u use SelectMultiple then you get a select widget where you can select many options.... Your code are wrong in forms.py in widgets=....

Shaheed Haque

unread,
Oct 18, 2021, 11:45:50 AM10/18/21
to django...@googlegroups.com
On Mon, 18 Oct 2021 at 15:41, Sebastian Jung <sebasti...@gmail.com> wrote:
When u use SelectMultiple then you get a select widget where you can select many options.... Your code are wrong in forms.py in widgets=....

Am Mo., 18. Okt. 2021 um 16:22 Uhr schrieb 'Maryam Yousaf' via Django users <django...@googlegroups.com>:
It is already selectmultiple . I need dropdown where I can select multiple values .
currently, it is coming like this:

Apologies if this is stating the obvious, but to select more than one item, are you using "shift+click" or "ctrl+click" or just "click"?

You need to use the modifier key with "click", not just "click".
 

Maryam Yousaf

unread,
Oct 19, 2021, 3:56:38 AM10/19/21
to django...@googlegroups.com
It is not working with SelectMultiple. I can select multiple values but it is not showing any drop down.

On Mon, 18 Oct 2021 at 16:07, Sebastian Jung <sebasti...@gmail.com> wrote:
Screenshot 2021-10-19 at 09.54.17.png

Sebastian Jung

unread,
Oct 20, 2021, 1:58:27 AM10/20/21
to django...@googlegroups.com
This is Admin Frontend this has nothing to do with normal Frontend. In Admin Frontend i have no experience...

danielp...@gmail.com

unread,
Oct 23, 2021, 11:18:54 AM10/23/21
to Django users

you can use django-ajax-selects library

https://github.com/crucialfelix/django-ajax-selects/

Define a lookup channel:

yourapp/lookups.py

from ajax_select import register, LookupChannel 
from .models import Tag 

@register('tags') 
class TagsLookup(LookupChannel): 
    model = Tag 

    def get_query(self, q, request): 
          return self.model.objects.filter(name__icontains=q).order_by('name')[:50] 
  def format_item_display(self, item): 
          return u"<span class='tag'>%s</span>" % item.name

Add field to a form:

yourapp/forms.py
from ajax_select.fields import AutoCompleteSelectMultipleField 

class DocumentForm(ModelForm): 
       class Meta: 
           model = Document 
           tags = AutoCompleteSelectMultipleField('tags')
Reply all
Reply to author
Forward
0 new messages