[Answer]-First element by ForeignKey

1👍

You could do:

class Car(models.Model):
    @property
    def default_picture(self):
        try:
            return self.pictures.all()[:1][0]
        except IndexError:
            return None

Then when you run your query for cars, you can do:

cars = Car.objects.all().values('id', 'default_picture__image')

Leave a comment