开发者

Django: How can I add weight/ordering to a many to many relationship?

I'm having Pages with TextBlocks on them. Text Blocks may appear on different pages, pages may have several text blocks on them. Every page may have these blocks in an ordering of it's own. This can be solved by using a separate through parameter. Like so:

class TextBlock(models.Model):
    title = models.CharField(max_length=255)
    text = models.TextField()

class Page(models.Model):
    textblocks = models.ManyToManyField(TextBlock, through=OrderedTextBlock)

class OrderedTextBlock(models.Model):
    text_block = models.ForeignKey(TextBlock)
    product_page = models.ForeignKey(Page)
    weight = models.IntegerField()

  开发者_如何转开发  class Meta:
        ordering = ('weight',)

But I'm not very enthousiastic about the violations of DRY for my app. (There's a lot of ordered ManyToMany relations). Is there a recommended way to go about this?

Edit: typo


You could, perhaps, create your own Custom Field - OrderedManyToManyField, perhaps, as a subclass of ManyToManyField.

Or, if that doesn't look ideal you could use your existing approach, creating multiple 'through' models, but subclass them from each other to adhere to DRY as best you can

class OrderedM2M(models.Model):
    weight = models.IntegerField()

    class Meta:
        ordering = ('weight',)
        abstract = True

class OrderedTextBlock(OrderedM2M):
    text_block = models.ForeignKey(TextBlock)
    product_page = models.ForeignKey(Page)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜