Note that there are some explanatory texts on larger screens.

plurals
  1. POReverse relation (ForeignKey) in Djano ORM with unique combination
    primarykey
    data
    text
    <p>Hi I was just wondering if this is feasible through Django ORM:</p> <p>Please ignore model names etc, they are not real</p> <pre><code>class Image(models.Model): ... item = models.ForeignKey(Bag, related_name="images") flag_frontpage_image = models.BooleanField() imagefile = models.ImageField(upload_to="items/%m/%Y") ... class Item(models.Model): ... </code></pre> <p>In views.py:</p> <pre><code>class LandingPageView(SomeMixin,SomeOtherMixin,ListView): .... def get_context_data(self, **kwargs): context = super(LandingPageView, self).get_context_data(**kwargs) context['items'] = Item.objects.prefetch_related('images').order_by('?').all()[:10] return context </code></pre> <p>This is ok and working ok, what I want to achieve is that I want in the view to fetch in 1 query applying a join the image a well. LandingPageView displays a list of items with the image that is flagged as frontpage image, as Django states (and SQL wise correctly) a reverse select_related is not possible, since Item can have many images, but...a Item can have only one frontpage image. I know there are other ways of achieving this in terms of refactoring, currently since I prefer to avoid partitioning models, I follow a mixin way:</p> <pre><code>class GetFeaturedMixin(object): @property def featured_image(self): if self.images.count &gt; 0: for image in self.images.all(): if image.flag_frontpage_image == 1: return image.imagefile class Item(models.Model, GetFeaturedMixin): ... </code></pre> <p>And then in the view:</p> <pre><code>&lt;img src="{{ item.featured_image }}"&gt; </code></pre> <p>This works fine, but I would like to know if there is a way through Django relational querysets to achieve fetching the whole set in single queryset. Please don't answer or ask about raw queries, I know they do exist and have used them even on this type, I was just wondering if the above can be achieved with the ORM layer. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload