Assuming the following model:
class Category(models.Model):
related = models.ManyToManyField('self', symmetrical = False, through = 'CategoryRelation', null = True, blank = True)
Assuming the following intermediate 'through' relation:
class CategoryRelation(models.Model):
source = models.ForeignKey('Category', null = False, blank = False, verbose_name = _('source'), related_name = 'relation_source')
target = models.ForeignKey('Category', null = False, blank = False, verbose_name = _('target'), related_name = 'relation_target')
order = models.PositiveIntegerField(_('order'), null = False, blank = True, default = 0)
class Meta:
ordering = ( 'order', )
How can I obtain the Category objects related to a given Category while preserving the ordering? The following code will produce the correct Category objects, not in the correct order, and include duplicate entries in the Query Set even when using .distinct():
relations = CategoryRelation.objects.filter(source = self)
Category.objects.filter(relation_target__in = relations).order_by('related')
Thanks!