P
P
Peterson_s2020-10-29 22:03:18
Django
Peterson_s, 2020-10-29 22:03:18

How to display several models of related ForeignKeys in one template (forms.py)?

How to display several models of related ForeignKeys in one template (forms.py)? So that you can add data from the site to the
models.py database

class Tables(models.Model):
    title = models.CharField(max_length=200)
    start_price = models.IntegerField()
    is_active = models.BooleanField(default=True)
    summary = models.TextField(max_length=1000, help_text="Enter a brief description of your product")
    
    def __str__(self):
        """
        String for representing the Model object.
        """
        return str(self.title)

class TableImage(models.Model):
    table = models.ForeignKey(Tables, blank=True, null=True, default=None, on_delete=models.SET_NULL)
    photo = models.ImageField()
    is_main = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    def __str__(self):
        return str(self.table)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IDzone-x, 2020-10-30
@IDzone-x

Just

views.py
def _(request):
images = TableImage.objects.all()
return render(request, <шаблон>, {'images': images}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question