On May 23, 12:12 pm, bruno desthuilliers
<bruno.desthuilli...@gmail.com> wrote:
> On May 22, 4:34 pm, akaariai <akaar...@gmail.com> wrote:
>
> > So, you would want to do collection.songs.all() and have that ordered
> > by the sequence defined in the m2m table? I agree - this seems to be
> > somewhat hard currently.
>
> I currently have a similar scheme in one of our projects, works fine
> as far as I can tell:
>
> class Categorie(Model):
>    # ....
>
> class Blook(Model):
>   # ....
>   categories = models.ManyToManyField(
>         Category,
>         through='Classification',
>         related_name="blooks",
>         )
>
>   @property
>   def sorted_categories(self):
>      return self.categories.order_by("classifications__position")
>
> class Classification(Model):
>     category = models.ForeignKey(
>         Category,
>         related_name="classifications"
>         )
>
>     blook = models.ForeignKey(
>         Blook,
>         related_name="classifications"
>         )
>
>     position = models.PositiveIntegerField(
>         _(u"Position"),
>         )
>
>     class Meta:
>         unique_together = (
>             ("blook", "category"),
>             ("blook", "position"),
>             )

I would guess you would get duplicate objects returned. I think the
query works as follows:
  - Fetch all Blook's categories
  - For those categories you fetch all the classifications (not only
those which are related to Blook) and order by the position in there.
So, you could get the same category multiple times ordered by
positions not related to the blook <-> category connection.

Maybe the above code does work correctly. The interaction between
multiple references to same reverse foreign key relation in single
queryset is somewhat hard to remember. Could you post the generated
SQL?

 - Anssi

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to