Code


Version 5 (modified by oubiwann, 9 years ago) (diff)

--

CookBook - Data Model

Multiple Levels and Multiple Components

There are times when you have data/content that has many-to-one relationships. This is very easy to accomodate, just as this example demonstrates. Here is code similar to that of the example:

from django.core import meta

class Document(meta.Model):
    fields = (
        meta.CharField('short_name', maxlength=30),
        meta.CharField('title', maxlength=100),
    )

    def __repr__(self):
        return self.title

    admin = meta.Admin()

class Paragraph(meta.Model):
    fields = (
        meta.ForeignKey(
            Document, 
            edit_inline=True, 
            num_in_admin=3, 
            num_extra_on_change=1
        ),
        meta.TextField('content'),
    )

    def __repr__(self):
        return self.headline

This is fantastic. It's convenient. But... what if we have more than just two "levels"? What if our document data is divided into two types of data, say "summary" and "details"? And what if each of these views then has potentially multiple paragraphs?

Stay tuned; more to come...