如何使用多个不同的应用程序在Django中组织数据库?

时间:2023-02-05 11:32:00

I'm new to Django (and databases in general), and I'm not sure how to structure the following. The sources of data I'll have for my site are:

我是Django的新手(以及一般的数据库),我不知道如何构建以下内容。我将为我的网站提供的数据来源是:

  1. a blog
  2. 一个博客
  3. for a few different games:
    • a high score list
    • 高分榜
    • user-created levels
    • 用户创建的级别
  4. 对于一些不同的游戏:高分列表用户创建的级别

If I were storing the data in ordinary files, I'd just have one file for each of the above. In Django, ideally (I think) I'd have a separate database for each of these, but apparently multiple database support isn't there for Django yet. I'm worried (unnecessarily?) about keeping everything in one database for two reasons:

如果我将数据存储在普通文件中,我将只为上述每个文件提供一个文件。在Django中,理想情况下(我认为)我会为这些中的每一个都有一个单独的数据库,但显然还有多个数据库支持不适用于Django。我担心(不必要地?)将所有内容保存在一个数据库中有两个原因:

  1. If I screw something up in one of the sections, I don't want to mess up the rest of the data.

    如果我在其中一个部分搞砸了,我不想搞砸其余的数据。

  2. When I'm working on one of these sections, I'd like the freedom to easily change the model around. Since I've learned that syncdb doesn't, in fact, sync the database, I've decided that the easiest thing to do when messing around with a model is to simply wipe the database and start over. Again, I'm worried about messing up the other sections. I looked at south, and it seems like more trouble than it's worth during the planning stages of an app (but I'll reconsider later when there's actually valuable data).

    当我在其中一个部分工作时,我希望能够*地轻松改变模型。因为我已经知道syncdb实际上并没有同步数据库,所以我决定在弄乱模型时最简单的事情就是擦除数据库并重新开始。再次,我担心弄乱其他部分。我看着南方,在应用程序的计划阶段似乎比它的价值更麻烦(但是我会在以后有实际有价值的数据时重新考虑)。

Part of the problem is that I'm not really comfortable keeping my data in a binary format. I'm used to text, so I can easily diff it, modify it in an editor, etc., without going through some magical database interface (I'm using postgresql, by the way).

部分问题是我不太习惯以二进制格式保存数据。我习惯于文本,所以我可以很容易地区分它,在编辑器中修改它等,而不需要通过一些神奇的数据库接口(顺便说一句,我使用的是postgresql)。

Are my fears unfounded? How do people normally handle this problem?

我的恐惧没有根据吗?人们通常如何处理这个问题?

4 个解决方案

#1


6  

For what it's worth, I totally understand your frustration as I went through a really similar thought process when starting. Unfortunately, there isn't much you can do (easily, anyway) besides get familiar with the tools you'll be using.

对于它的价值,我完全理解你的挫败感,因为我在开始时经历了一个非常相似的思考过程。不幸的是,除了熟悉您将要使用的工具之外,您无法做很多事情(无论如何都很容易)。

First of all, you don't need multiple databases for what you're doing - one will do. Each app will create its own tables in the database which are somewhat isolated from one another. As czarchaic mentioned, you can do python manage.py reset app_name to reset just one of them in case you change your model. You will lose that data, though.

首先,您不需要多个数据库来处理您正在做的事情 - 一个人会做。每个应用程序将在数据库中创建自己的表,这些表在某种程度上彼此隔离。正如czarchaic所提到的,你可以做python manage.py重置app_name来重置其中一个,以防你改变你的模型。但是,您将丢失该数据。

To get data in relatively easy to work with format, you can use the command python manage.py dumpdata > file_name.json, and then to reload it later python manage.py loaddata file_name.json. You can use this for backups, local test data, or as a poor man's migration (hint: South would be easier).

要获得相对容易使用格式的数据,可以使用命令python manage.py dumpdata> file_name.json,然后在以后重新加载python manage.py loaddata file_name.json。您可以将其用于备份,本地测试数据或作为穷人的迁移(提示:South会更容易)。

Yet another option is to use a NoSQL database for any data you think will need extra flexibility. Just keep in mind that Django doesn't have support for any of these at the moment. That means no no admin support or ModelForms. Of course, having a model may become unnecessary.

另一个选择是使用NoSQL数据库来处理您认为需要额外灵活性的任何数据。请记住,Django目前不支持任何这些。这意味着没有没有管理员支持或ModelForms。当然,拥有模型可能变得不必要。

#2


4  

In short, your fears are unfounded. You should "organize" your database by project to use the Django term. Each model in each app will have it's own table, but they will all be in the same database. Putting them in a separate database isn't a good idea for a whole host of reasons, the biggest is that you cannot query across databases.

简而言之,你的恐惧是没有根据的。您应该按项目“组织”您的数据库以使用Django术语。每个应用程序中的每个模型都有自己的表,但它们都将位于同一个数据库中。将它们放在一个单独的数据库中并不是一个好主意,原因很多,最大的原因是你不能跨数据库进行查询。

While I agree that south is probably a bit heavy for your initial design/dev stages it should be considered seriously for anything even resembling a beta version and absolutely necessary in production.

虽然我同意南方可能对您的初始设计/开发阶段有点沉重,但应该认真对待任何类似于测试版并且在生产中绝对必要的东西。

If you're going to be messing with your models a bunch during development the best thing to do is use fixtures to load data in quickly after running sync. Or, if you are going to be changing a bunch of required fields, then write some quick Python to create dummy data for you.

如果你要在开发过程中弄乱你的模型,最好的办法是在运行同步后使用夹具快速加载数据。或者,如果您要更改一堆必需字段,请编写一些快速Python来为您创建虚拟数据。

As for not trusting your data to binary, a simple "pg_dump " will get you a textual version of your data. It sounds to me like you're working on your app against live production data, which is a mistake. Your goal should be to get your application built, working, and tested on fake data or at least a copy of your production data and then when you're sure everything is golden migrate it into production. This is where things like south come in handy as you can automate this deployment and it will help you handle any database table/column changes you need to make.

至于不信任您的数据到二进制文件,一个简单的“pg_dump”将为您提供数据的文本版本。听起来像你正在使用你的应用程序来对抗实时生产数据,这是一个错误。您的目标应该是使您的应用程序在假数据或至少生产数据的副本上构建,工作和测试,然后当您确定所有内容都是黄金时,将其迁移到生产环境中。这就像南方这样的东西派上用场,因为您可以自动执行此部署,它将帮助您处理需要进行的任何数据库表/列更改。

I'm sure all of this sounds like a pain, but all of it is able to be automated and trust me it makes your life down the road much much easier.

我确信所有这些听起来都很痛苦,但所有这一切都能够实现自动化并相信我它让你的生活变得更加轻松。

#3


4  

I generally just reset the module

我通常只是重置模块

>>> python manage.py reset blog

this will reset all tables in INSTALLED_APPS.blog

这将重置INSTALLED_APPS.blog中的所有表

I'm not sure if this answers your question but it's much lest destructive than wiping the DB.

我不确定这是否能解决你的问题,但它比擦除数据库更具破坏性。

#4


1  

Syncdb should really only be used for development. That's why it doesn't really matter if you wipe the tables and start again, perhaps exporting look up data into a json file that you can import each time you sync.

Syncdb应该只用于开发。这就是为什么你擦除表并重新开始并不重要,也许将查找数据导出到每次同步时可以导入的json文件中。

When your site reaches production however, you have a little more work. Any changes you make to your models that need to be reflected in the database, need to be emitted as SQL and run manually on the database. There's a django-admin.py function to emit the suggested SQL, which you can use to build up a script to run on the database to migrate it. Like you mention, a migrations app like South can be beneficial here but it's not strictly needed.

但是,当您的网站投入生产时,您还可以完成更多工作。您对模型所做的任何需要在数据库中反映的更改都需要作为SQL发出并在数据库上手动运行。有一个django-admin.py函数可以发出建议的SQL,您可以使用该函数构建一个脚本以在数据库上运行以进行迁移。就像你提到的,像South这样的迁移应用程序在这里可能是有益的,但并不是严格需要的。

As far as your separation of sites goes, run them as separate sites/projects. You can have a separate settings file per project which allows you to run two different databases. This is in contrast to running the two sites as separate apps within the same project. If they're totally separate they probably shouldn't be in the same project unless you need to leverage common code.

就您的站点分离而言,将它们作为单独的站点/项目运行。每个项目可以有一个单独的设置文件,允许您运行两个不同的数据库。这与将两个站点作为同一项目中的单独应用程序运行形成对比。如果他们完全分开,他们可能不应该在同一个项目中,除非您需要利用通用代码。

#1


6  

For what it's worth, I totally understand your frustration as I went through a really similar thought process when starting. Unfortunately, there isn't much you can do (easily, anyway) besides get familiar with the tools you'll be using.

对于它的价值,我完全理解你的挫败感,因为我在开始时经历了一个非常相似的思考过程。不幸的是,除了熟悉您将要使用的工具之外,您无法做很多事情(无论如何都很容易)。

First of all, you don't need multiple databases for what you're doing - one will do. Each app will create its own tables in the database which are somewhat isolated from one another. As czarchaic mentioned, you can do python manage.py reset app_name to reset just one of them in case you change your model. You will lose that data, though.

首先,您不需要多个数据库来处理您正在做的事情 - 一个人会做。每个应用程序将在数据库中创建自己的表,这些表在某种程度上彼此隔离。正如czarchaic所提到的,你可以做python manage.py重置app_name来重置其中一个,以防你改变你的模型。但是,您将丢失该数据。

To get data in relatively easy to work with format, you can use the command python manage.py dumpdata > file_name.json, and then to reload it later python manage.py loaddata file_name.json. You can use this for backups, local test data, or as a poor man's migration (hint: South would be easier).

要获得相对容易使用格式的数据,可以使用命令python manage.py dumpdata> file_name.json,然后在以后重新加载python manage.py loaddata file_name.json。您可以将其用于备份,本地测试数据或作为穷人的迁移(提示:South会更容易)。

Yet another option is to use a NoSQL database for any data you think will need extra flexibility. Just keep in mind that Django doesn't have support for any of these at the moment. That means no no admin support or ModelForms. Of course, having a model may become unnecessary.

另一个选择是使用NoSQL数据库来处理您认为需要额外灵活性的任何数据。请记住,Django目前不支持任何这些。这意味着没有没有管理员支持或ModelForms。当然,拥有模型可能变得不必要。

#2


4  

In short, your fears are unfounded. You should "organize" your database by project to use the Django term. Each model in each app will have it's own table, but they will all be in the same database. Putting them in a separate database isn't a good idea for a whole host of reasons, the biggest is that you cannot query across databases.

简而言之,你的恐惧是没有根据的。您应该按项目“组织”您的数据库以使用Django术语。每个应用程序中的每个模型都有自己的表,但它们都将位于同一个数据库中。将它们放在一个单独的数据库中并不是一个好主意,原因很多,最大的原因是你不能跨数据库进行查询。

While I agree that south is probably a bit heavy for your initial design/dev stages it should be considered seriously for anything even resembling a beta version and absolutely necessary in production.

虽然我同意南方可能对您的初始设计/开发阶段有点沉重,但应该认真对待任何类似于测试版并且在生产中绝对必要的东西。

If you're going to be messing with your models a bunch during development the best thing to do is use fixtures to load data in quickly after running sync. Or, if you are going to be changing a bunch of required fields, then write some quick Python to create dummy data for you.

如果你要在开发过程中弄乱你的模型,最好的办法是在运行同步后使用夹具快速加载数据。或者,如果您要更改一堆必需字段,请编写一些快速Python来为您创建虚拟数据。

As for not trusting your data to binary, a simple "pg_dump " will get you a textual version of your data. It sounds to me like you're working on your app against live production data, which is a mistake. Your goal should be to get your application built, working, and tested on fake data or at least a copy of your production data and then when you're sure everything is golden migrate it into production. This is where things like south come in handy as you can automate this deployment and it will help you handle any database table/column changes you need to make.

至于不信任您的数据到二进制文件,一个简单的“pg_dump”将为您提供数据的文本版本。听起来像你正在使用你的应用程序来对抗实时生产数据,这是一个错误。您的目标应该是使您的应用程序在假数据或至少生产数据的副本上构建,工作和测试,然后当您确定所有内容都是黄金时,将其迁移到生产环境中。这就像南方这样的东西派上用场,因为您可以自动执行此部署,它将帮助您处理需要进行的任何数据库表/列更改。

I'm sure all of this sounds like a pain, but all of it is able to be automated and trust me it makes your life down the road much much easier.

我确信所有这些听起来都很痛苦,但所有这一切都能够实现自动化并相信我它让你的生活变得更加轻松。

#3


4  

I generally just reset the module

我通常只是重置模块

>>> python manage.py reset blog

this will reset all tables in INSTALLED_APPS.blog

这将重置INSTALLED_APPS.blog中的所有表

I'm not sure if this answers your question but it's much lest destructive than wiping the DB.

我不确定这是否能解决你的问题,但它比擦除数据库更具破坏性。

#4


1  

Syncdb should really only be used for development. That's why it doesn't really matter if you wipe the tables and start again, perhaps exporting look up data into a json file that you can import each time you sync.

Syncdb应该只用于开发。这就是为什么你擦除表并重新开始并不重要,也许将查找数据导出到每次同步时可以导入的json文件中。

When your site reaches production however, you have a little more work. Any changes you make to your models that need to be reflected in the database, need to be emitted as SQL and run manually on the database. There's a django-admin.py function to emit the suggested SQL, which you can use to build up a script to run on the database to migrate it. Like you mention, a migrations app like South can be beneficial here but it's not strictly needed.

但是,当您的网站投入生产时,您还可以完成更多工作。您对模型所做的任何需要在数据库中反映的更改都需要作为SQL发出并在数据库上手动运行。有一个django-admin.py函数可以发出建议的SQL,您可以使用该函数构建一个脚本以在数据库上运行以进行迁移。就像你提到的,像South这样的迁移应用程序在这里可能是有益的,但并不是严格需要的。

As far as your separation of sites goes, run them as separate sites/projects. You can have a separate settings file per project which allows you to run two different databases. This is in contrast to running the two sites as separate apps within the same project. If they're totally separate they probably shouldn't be in the same project unless you need to leverage common code.

就您的站点分离而言,将它们作为单独的站点/项目运行。每个项目可以有一个单独的设置文件,允许您运行两个不同的数据库。这与将两个站点作为同一项目中的单独应用程序运行形成对比。如果他们完全分开,他们可能不应该在同一个项目中,除非您需要利用通用代码。