[Answered ]-Django – Display list of category belonging to each card

1πŸ‘

βœ…

From django-doc,you have to make ForeginKey field in your main Giftcard table which will do ManyToOne relation with Category table.

Try this:

models.py

from django.db import models


class Category(models.Model):
    category = models.CharField(max_length=250)
    rate = models.IntegerField()
    terms = models.TextField()

    def __str__(self):
        return self.category


class Giftcard(models.Model):
    name = models.CharField(max_length=100, unique=True)
    card_image = models.ImageField(upload_to='Giftcard/', blank=False)
    date = models.DateTimeField(auto_now_add=True)
    publish = models.BooleanField(default=False)
    gift_category = models.ForeignKey(Category, on_delete=models.CASCADE)

views.py

def giftcard(request):
    giftcards = Giftcard.objects.filter(publish=True)

    context = {
        'giftcards': giftcards
    }
    return render(request, 'dashboard/giftcard.html', context)

Now, you can access all the properties of Category table in Giftcard items in your template file, and a giftcard can have more than one category, it is called ManyToOne relation,

Note: you can also use choices, which is much better than making table, you can see that too here in django-doc.

Edit

Do this in datalist

<datalist id="category">
    {% for spec in giftcard.specs.all %}

        <option value="{{ spec.category }}">{{ spec.category }}</option>
    {% endfor %}

</datalist>

0πŸ‘

If, as you say in one of your comments, "it won’t work that way because a giftcard can have more than one category" and "No, Category cannot have more than one Giftcard", then your original models are correct, with ForeignKey in the Category class. Second, I do not know what exactly is going on, perhaps add screenshots of what you are getting that is wrong, because I just tried your original code out, and it works, with the modifications to the datalist suggested by @Sunderam. Test this out in your html template:

{% for giftcard in giftcards %}
    {% for spec in giftcard.specs.all %}
        {{ spec.terms }}<br>
        {{ spec.category }}<br>
        {{ spec.rate }}<br>
    {% endfor %}
{% endfor %}

When I did this, I am seeing different terms, categories and rates for each. Again, is this not what you want?

Edit
You must understand that the datalist will have all the spec values, and one shows up when you start typing in the input box. for example, if you type c, then the list will pop up with all categories that begin with c.

Edit
Make sure the datalist has been changed to

<div class="form-group py-2">
  <label for="card-category">Card category</label>
  <input list="category" class="form-control" name="category" id="card_category"
    placeholder="Select card category">

  {% for spec in giftcard.specs.all %}
  <datalist id="category">
    <option value="{{ spec.category }}">{{ spec.category }}</option>
  </datalist>
  {% endfor %}
</div>

After all, you want only one <datalist> element, and then many <option>s.

πŸ‘€raphael

Leave a comment