Database Design: Many-to-Many Orderly Relationships

In Django, I have the following models:

class Pink(models.Model):
    ...

class White(models.Model):
    ...
    pinks = models.ManyToManyField(Pink)
    ...



At some point, I needed to determine the order Pinksinside a White (so that White1.pinks: P1, P2, P3instead of random White1.pinks: P2, P3, P1), so I created

class PinkInWhite(models.Model):
    pink = models.ForeignKey(Pink)
    white = models.ForeignKey(White)
    position = models.PositiveIntegerField("pink position inside this white")

    class Meta:
        ordering = ['white','position']



Question 1: is the best solution?

Question 2: This is clearly redundant: I still have a field Pinksin my model White, because I need it in other situations for which ordering is not required. Can I save it or is it better to delete it and always use the relationship model PinkInWhite?




(Model names are fictitious. To simplify the issue, I did not use model real names, and talking about Mr. X and Mr. Y did not help readability ...)

+3
1

, .

, PinkInWhite.

, , , , , . , , , approriate whites ( ). , , , .

, , , .

+1

Source: https://habr.com/ru/post/1765151/


All Articles