Monday, February 15, 2021

Re: django-filters many to many

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

Am Mo., 15. Feb. 2021 um 05:32 Uhr schrieb Agni Venus <praispranav@gmail.com>:
Put this inside and below if your model



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

On Mon, 15 Feb, 2021, 10:01 am Agni Venus, <praispranav@gmail.com> wrote:
Put this below the model...
Add 
Def __str__(self):
return self.what-ever-name-you-want

On Mon, 15 Feb, 2021, 10:00 am Agni Venus, <praispranav@gmail.com> wrote:
Add 
Def __str__(self):
return self.what-ever-name-you-want

On Sun, 14 Feb, 2021, 9:00 pm sebasti...@gmail.com, <sebastian.jung2@gmail.com> wrote:
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             

--
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+unsubscribe@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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CADaZBzFj_67RTdDmtcbA3LcsxJ_nH6OXmEg%3DCMCcjB29N-bSxA%40mail.gmail.com.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAKGT9myWfChiyyV7_uSnDhOT8GYng4rvvyEbiZSFjCpQPiAx0g%40mail.gmail.com.

No comments:

Post a Comment