I have two models: the first Property model and the second Property model Image:
Class Property(models.model):
title = models.CharField()
Class PropertyImage(models.model):
property = modelos.Foreignkey(Property, related_name='images')
imagen = models.ImageField()
PropertyImage is where property images are stored and is linked to the first model with a foreign key.
I know that to get the url of the first image of a property in a query I would do something like this:
p1 = Property.objects.first() # la primera propiedad
p1.images.first() # la primera imagen de la propiedad 1
My question is if the template was sent a query where all the properties are (or filtered according to the case) how would I do it within the template to show only the url of the first image for each property?
I have tried to use the first function within the template but it gives me a syntax error.
You should not add programming logic to templates.
Your best option is to add a property to your model:
To show the url inside the Template:
Notes :
first()
, but[0]
it returns the first record of a QuerySet .