django-filters many to many

393 views
Skip to first unread message

sebasti...@gmail.com

unread,
Feb 14, 2021, 10:30:05 AM2/14/21
to Django users
Hello,

i have installed per pip django-filters.

Models.py:

class Productinterests(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, default="", blank=False, null=False)

class Address(models.Model):
   productinterests = models.ManyToManyField(Productinterests,blank=True, default=0)
   lastname = models.CharField(max_length=255, default="", )
   firstname = models.CharField(max_length=255, default="", blank=True, null=True)

filters.py:
   class AddressFilter(django_filters.FilterSet):
lastname = django_filters.CharFilter(lookup_expr='icontains',label="Nachname")
firstname = django_filters.CharFilter(lookup_expr='icontains', label="Vorname")

    productinterests = django_filters.ModelMultipleChoiceFilter(to_field_name='name',queryset=Productinterests.objects.all(),
widget=forms.CheckboxSelectMultiple, label="Produktinteresse")

    class Meta:
       model = Address
       fields = ['lastname','firstname','productinterests' ]

views.py:
   def search(request):
         liste = Address.objects.all()

         address = AddressFilter(request.GET, queryset=liste)
return render(request, 'search/address_list.html', {'filter': user_filter})


Problem: Website shows in Multiselect Productinterest only as Output:
 "Productinterests object (1)" instead of name from object. How can i output name?

Regards             

Agni Venus

unread,
Feb 14, 2021, 11:31:34 PM2/14/21
to django...@googlegroups.com
Add 
Def __str__(self):
return self.what-ever-name-you-want

--
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/636ab5c4-d24e-42ab-bec2-be2eab833bdbn%40googlegroups.com.

Agni Venus

unread,
Feb 14, 2021, 11:32:17 PM2/14/21
to django...@googlegroups.com
Put this below the model...

Add 
Def __str__(self):
return self.what-ever-name-you-want

Agni Venus

unread,
Feb 14, 2021, 11:32:59 PM2/14/21
to django...@googlegroups.com
Put this inside and below if your model



Def __str__(self):
return self.what-ever-name-you-want

Sebastian Jung

unread,
Feb 15, 2021, 3:33:41 AM2/15/21
to django...@googlegroups.com
Good morning,

Yeah it works. Thank you

Sebastian Jung

unread,
Feb 15, 2021, 6:25:25 AM2/15/21
to django...@googlegroups.com
Hello,

now it shows on frontend the name. Problem is, that in value also string is instead id.

For example:

<label class="checkbox-inline">
                <input type="checkbox" name="productinterests" value="Test" id="id_productinterests_0"> Test
              </label>

but this must be:

<label class="checkbox-inline">
                <input type="checkbox" name="productinterests" value="0" id="id_productinterests_0"> Test
              </label>

How can i change this?

Regards

MOHIT DILIP MAKWANA

unread,
Feb 15, 2021, 9:23:12 AM2/15/21
to Django users
Hey,
      You can add a dunder method for your  "Productinterests" model to return the name of the object instead of the name of model.
It involves the use of '__str__()' method. You can implement it in the following way:

class Productinterests(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, default="", blank=False, null=False) 
    def __str__(self):
            return str(self.name)

Just add the extra function in your models, and then whenever a "Productinterests" object is called, it will be outputted as the value of its 'name' property.

Regards.       

Sebastian Jung

unread,
Feb 15, 2021, 10:47:24 AM2/15/21
to django...@googlegroups.com
Yeah but this doesn't work. I describe IT in my Last Post why

Regards

--
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.
Reply all
Reply to author
Forward
0 new messages