Jean-Luca Röder
Jean-Luca Röder

Reputation: 23

How to load a parameter from an object in an object?

class Product(models.Model):
    subcategory = models.ManyToManyField(Subcategory, related_name="Category")
    name = models.CharField(max_length=32, unique=True)
    title = models.CharField(max_length=3000)
    ean = models.PositiveIntegerField(blank=True, null=True, unique=True)
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE, blank=True, null=True)
    origin_name = models.CharField(max_length=32,  blank=True, null=True)
    quantity = models.PositiveIntegerField(blank=True, null=True)
    highlights = models.TextField(max_length=3000, blank=True, null=True)
    description = models.TextField(max_length=3000, blank=True, null=True)
    delivery = models.TextField(max_length=3000, blank=True, null=True)
    selling_price = models.DecimalField(max_digits=9, decimal_places=2)
    slug = models.SlugField(unique=True)

class Infobox(models.Model):
    name = models.CharField(max_length=32)
    measurment = models.CharField(max_length=32)
    resolution = models.CharField(max_length=32)
    product = models.ForeignKey(Product, on_delete=models.CASCADE, blank=True, null=True)

I would like to display on my detail page all infobox objects that are linked to the device, do any of you know how to do this?

THANKSSSS <3

Upvotes: 0

Views: 30

Answers (1)

Lambo
Lambo

Reputation: 1174

urls.py Add below to your urlpatterns:

path('view_infoboxes/<int:product_id>', views.view_infoboxes, name='view_infoboxes'),

views.py

def view_infoboxes(request, product_id):
    template = loader.get_template('page.html')
    context = {"infoboxes": Infobox.objects.filter(product=product_id)}
    return HttpResponse(template.render(context, request))

page.html

...
<div class="row">
  {% for infobox in infoboxes %}
    <p>{{ infobox.name }}</p>
  {% endfor %}
</div>
...

Upvotes: 1

Related Questions