Reputation: 300
Is there a way to display content based on th value in the table field? Lets say that I have a field in my table called label, which isnt obligatory to fill in, can I filter the content based on if that field is populated for specific item? Item that has that field populated would be displayed and item with that field left blank wouldnt.
model.py:
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.FloatField()
discount_price = models.FloatField(blank=True, null=True)
category = models.ManyToManyField(Category, blank=True)
label = models.ManyToManyField(Label, blank=True)
slug = models.SlugField(unique=True)
description = models.TextField()
image = models.ImageField()
views.py:
def HomeView(request):
item_list = Item.objects.all()
context = {
'item_list': item_list,
}
return render(request, "home.html", context)
Upvotes: 0
Views: 29
Reputation: 36
If I understand correctly, you want to have the Item
s where label
is not None? Then you could try this:
item_list = Item.objects.exclude(label=None)
Upvotes: 0
Reputation: 2752
Use filter
item_list = Item.objects.filter(label = your_label_item)
if you want to get all Items where label is not null you can use this
item_list = Item.objects.filter(label__isnull = False)
Upvotes: 1