views:

345

answers:

3

I am about to redesign an Online Magazine which was built in Plone. And I am going to do it in Django. (For the simple reason that I like Django and that I have to maintain this particular site for (basically) free.)

However I am unsure about the way I should design the Models. Basically the idea is to have a Site structure - which in Plone mapped to a few folders with content objects inside.

Here is a very simple layout - omitting all irrelevant (?) details.

class Category(models.Model):
    parent = models.ForeignKey('self')
    # stuff like description, slug etc.

class Article(models.Model):
    category = models.ForeignKey(Catgegory)
    # teaser stuff

The question is:

How can I extend, change or mutilate this layout to enable a user of this app to add custom article-like items which could still be handled by a list_items_in_categoryview.

Related - this kinda solve the problem:

Note

I just realized how similar this problem is to tagging. The excellent `django-tagging' app already solved this. However there are some differences.

+1  A: 

Here's one way to do it:

class Category(models.Model):
    parent = models.ForeignKey('self')
    # stuff like description, slug etc.

class Entry(models.Model):
    category = models.ForeignKey(Catgegory)
    view_method = models.IntegerField(choices = settings.CMS_VIEW_METHODS)
    # teaser stuff

In the app article:

class Article(models.Model):
    category = models.OneToOneField(cms.models.Entry)
phoku
+2  A: 

You could simply use duck typing - create models that have the same attributes as Article (or at least the attributes used by list_items_in_category and its templates). The models would simply need to have foreign keys to Category, and you would have to somehow configure list_items_in_category to look up the relevant attributes of Category. e.g.:

def list_items_in_category(request):
    l = []
    for attr in getattr(settings, 'ITEMS_ON_CATEGORY', ['article_set']):
        l.extend(getattr(Category, attr).all())
    # do something with 'l' in a template.

If you don't want to limit the actual attributes of the custom models in that way, you could just create wrapper classes that will adapt them to look like Articles. You will simply need to specify what is considered to be the 'interface' of Articles for the purpose of that view.

spookylukey
+1  A: 

By the way, if you want to make CMS in Django you might want to look at Django CMS.

che
Ooops. How the heck did I miss that one during my survey? Thanks.
phoku