Django-灵活的模型? [英] Django - flexible models?

查看:61
本文介绍了Django-灵活的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Django的新手,想用它做一个简单的CMS。

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.

如何用Django最好地解决这样的问题,而无需对每种类型分别进行建模(子模型)?

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

推荐答案

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)

对于Django:您可以使用上面的模型。我正在使用这种模型来填充我的模型。如果使用大模型,则无法动态扩展模型。因为预测模型的大小并不容易。可以在您的管理站点上使用它。

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.

我也将Django与PostgreSQL一起使用,如果您不修改数据库,则该数据库不支持全文搜索。 ( http://www.postgresql.org/docs/9.3/static/textsearch .html

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)

对于Meteor:您知道或不知道MeteorJS是使用MongoDB的全栈框架。就我而言,Mongo是一个快速的NoSQL DB,并支持全文搜索。如果您不能很好地创建数据库,则会发现由于NoSQL而导致数据完整性不足。如果您是经验丰富的JS开发人员,那么与Django相比,用Meteor开发CMS系统的时间会更少。

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.

在我的项目中,我需要快速的搜索引擎和数据诚信我为Django定义了两个数据库。我使用Mongo来搜索引擎,并使用PostgreSQL来保存我的数据。

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.

请阅读以下资源:
Django中的Mongo: http://django-mongodb-engine.readthedocs.org/en/latest/topics/setup.html

Django中有多个DB: https://docs.djangoproject.com/en/1.9/topics/db/multi-db/

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

Django中的多数据库和多模型: django中的多个数据库和多个模型

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

settings.py

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

models.py

class SearchContent(models.Model):
    content = models.CharField(max_length=100, blank=True, default='', null=True)
    class Meta:
        app_label = 'my_database'

这篇关于Django-灵活的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆