I would like to prefetch a model property to a queryset in Django. Is there a way do that ?
Here are the three models:
class Place(models.Model):
name = models.CharField(max_length=200, blank=True)
@property
def bestpicurl(self):
try:
return self.placebestpic.picture.file.url
except:
return None
class PlaceBestPic(models.Model):
place = models.OneToOneField(Place)
picture = models.ForeignKey(Picture, on_delete=models.CASCADE)
class Picture(models.Model):
file = ImageField(max_length=500, upload_to="/images/")
I would need something like:
qs = Place.objects.all().select_related('bestpicurl')
Any clue how to do that ? Thanks!