-3

I am new to Django and would like to do a simple CMS with it.

There should be a model for pages. The users should be able to set a page type and then different fields should be available.

For example (simplified - in reality there would be a lot more fields):

Page - Type 1: title, text
Page - Type 2: title, text, images
Page - Type 3: title, text_column_1, text_column_2, images, links, downloads

I will predefine the fields. The users won't be able to change them.

How can something like that best be solved with Django without doing a separate (child)model for each type?

horace
  • 543
  • 7
  • 23

1 Answers1

2
class PageFieldDefinitions(models.Model):
    field_name = models.CharField(max_length=100, blank=True, default='', null=True)

class Pages(models.Model):
    name = models.CharField(max_length=100, blank=True, default='', null=True)

class PageFieldContent(models.Model):
    content = models.CharField(max_length=100, blank=True, default='', null=True)
    page_field = models.ForeignKey(PageFieldDefinitions)
    page = models.ForeignKey(Pages)

For Django: You can use the model above. I am using this kind of model to populate my model. You cannot dynamically expand your model if you use "big" model. Because it is not easy to predict your model's size. It is possible to use this on your admin site.

Also I am using Django with PostgreSQL and this DB does not support full text search if you don't modify the DB. (http://www.postgresql.org/docs/9.3/static/textsearch.html)

For Meteor: As you know or do not know MeteorJS is a full stack framework uses MongoDB. Mongo is a fast NoSQL DB as far as I use and supports full text search. If you don't create your DB well, you will see lack of data integrity because of NoSQL. If you are an experienced JS developer it will take less time to develop CMS system with Meteor when I compare to Django.

In my project I need a fast search engine and data integrity. I defined two DBs to Django. I use Mongo for searching engine and PostgreSQL for keeping my data.

Read these sources below: Mongo in Django: http://django-mongodb-engine.readthedocs.org/en/latest/topics/setup.html

Multiple DBs in Django: https://docs.djangoproject.com/en/1.9/topics/db/multi-db/

Multi DBs and MultiModels in Django: multiple databases and multiple models in django

settings.py

DATABASES = {
    'default': {
        'NAME': 'app_data',
        'ENGINE': 'django.db.backends.postgresql',
        'USER': 'postgres_user',
        'PASSWORD': 's3krit'
    },
    'search': {
        'ENGINE' : 'django_mongodb_engine',
        'NAME' : 'my_database',
        ...
        'OPTIONS' : {
           'socketTimeoutMS' : 500,
            ...
        }
    }
} 

models.py

class SearchContent(models.Model):
    content = models.CharField(max_length=100, blank=True, default='', null=True)
    class Meta:
        app_label = 'my_database'
Community
  • 1
  • 1
Umut Gunebakan
  • 391
  • 3
  • 14
  • Thanks! I will investigate your examples and links. Meteor and MongoDB looks more and more interesting to me. – horace Dec 15 '15 at 20:39