'Django - How to render a form field as a table with only some of the items that match a specific criteria?

I have this form:

forms.py

from django.forms.models import ModelForm
from .models import Order

class OrderForm(ModelForm):
    class Meta:
        model = Order
        fields = '__all__'

From this model:

models.py

from django.db import models
from django_countries.fields import CountryField
from django.core.validators import MaxValueValidator

class Category(models.Model):
    name = models.CharField(max_length=255)
    description = models.TextField(blank=True)
    image = models.ImageField(blank=True)

    def __str__(self) -> str:
        return f"{self.name}"

class Product(models.Model):
    name = models.CharField(max_length=255)
    category = models.ForeignKey(to=Category, on_delete=models.CASCADE)
    description = models.TextField(blank=True)
    price = models.DecimalField(max_digits=8, decimal_places=2)
    image = models.ImageField(blank=True)
    vat_percentage = models.DecimalField(max_digits=4, decimal_places=2, blank=True)

    @property
    def price_with_vat(self):
        if self.vat_percentage:
            return (self.price / 100 * self.vat_percentage) + self.price 
        else:
            return self.price

    def __str__(self) -> str:
        return f"{self.name} / {self.price} EUR"

class Address(models.Model):
    street = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    country = CountryField()
    zip_code = models.PositiveIntegerField(validators=[MaxValueValidator(99999999)])

    def __str__(self):
        return f"{self.street} / {self.city}"

class DeliveryAddress(models.Model):
    street = models.CharField(max_length=255)
    city = models.CharField(max_length=255)
    country = CountryField()
    zip_code = models.PositiveIntegerField(validators=[MaxValueValidator(99999999)])

    def __str__(self):
        return f"{self.street} / {self.city}"

class Order(models.Model):
    name = models.CharField(max_length=255)
    surname = models.CharField(max_length=255)
    address = models.ForeignKey(Address, on_delete=models.PROTECT)
    delivery_address = models.ForeignKey(DeliveryAddress, on_delete=models.PROTECT)
    company_name = models.CharField(max_length=255)
    company_ico = models.PositiveIntegerField(validators=[MaxValueValidator(9999999999)])
    company_dic = models.PositiveIntegerField(validators=[MaxValueValidator(999999999999)], blank=True)
    company_vat = models.PositiveIntegerField(validators=[MaxValueValidator(999999999999)], blank=True)
    products = models.ManyToManyField(Product)

And this is my view:

views.py

def create_order(request):
    if request.method == 'GET':
        context = {
            'form': OrderForm
        }
        return render(request, 'eshop/create_order.html', context=context)

It renders the form like this: rendered form

What I want to achieve:

The products field in the form should be rendered as a table, looking something like this: desired output table The rest of the rendered fields are fine for me, but I just need the products to be rendered as a table. Additionally, I'd need to loop through the items in the products field to display only those with specific primary key pk (something like, display only the products in the table in the form where pk == 1 AND where pk == 5.

How do I achieve this?

Thank you



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source