[Answer]-Modelling a book in Django

1👍

models.py:

class Article(models.Model):
    title = models.CharField(max_length=X)
    ...

class Part(models.Model):
    article = models.ForeignKey(Article)
    order = models.IntegerField()
    subtitle = models.CharField(max_length=X)
    content = models.TextField()
    ...

    class Meta:
        ordering = ('order',)

You have to set order in each part, every query will return them ordered by this field.

Using InlineModelAdmin you can edit all parts of an article from its admin page.

There shouldn’t be performance impacts: if queries are done properly all parts of an article (even all parts of multiple articles) can be returned with only one hit on the database. Read the docs about select_related and prefetch-related.

Leave a comment