问题描述
所以大约一年前,我开始了一个项目,像所有新开发人员,我没有真正关注的结构,但现在我进一步与Django它开始出现,我的项目布局主要是我的模型可怕的结构。
我有模型主要是在一个单一的应用程序,真的大多数这些模型应该是在自己的个人应用程序,我没有尝试和解决这个和移动他们与南然而我发现它棘手,真正困难,由于外键等。
但是由于Django 1.7和内置的支持迁移是有更好的方法
预先感谢您的帮助。
创建应用程序& MODELS
1)创建两个应用程序 app1 app2 。
$ python manage.py startapp app1
$ python manage.py startapp app2
2)创建两个模型 model1 & model2 。
app1 / model1.py
从django.db导入模型
类model1(models.Model):
name = models。 CharField(max_length = 100)
app2 / model2.py >
from django.db import models
class model1(models.Model):
name = models .CharField(max_length = 100)
3)应用迁移 p>
$ python manage.py makemigrations app1&& python migrate app1
$ python manage.py makemigrations app2&&& python migrate app2
Django将创建2个表 app1_model & app2_model2 。您可以向模型中添加数据。
允许在模型中添加小数据,以确保在移动模型时保留数据。
In [1]:from apps.app1.models import model1
在[2]:model1.objects.create b Out [2]:< model1:model1 object>
In [3]:model1.objects.create()
Out [3]:< model1:model1 object>
MIGRATING MODEL
让我们看看如何执行实际迁移。
4)重命名表&移动源代码
转到您的数据库,将 app1_model 重命名为 app2_model1 。将 model1 的源代码从 app1 移动到 app2 添加元选项。
app1 / model1.py
code> from django.db import models
app2 / model2.py
from django.db import models
class model2(models.Model):
name = models.CharField(max_length = 100)
class model1(models.Model):
name = models.CharField(max_length = 100)
类元:
managed = False
db_table ='app2_model1'
5)应用迁移。
为两个应用程序应用迁移。
$ python manage.py makemigrations app2&& python manage.py migrate app2
$ python manage.py makemigrations app1&&& python manage.py migrate --fake app1
迁移完成后,您可以检查模型。
在[1]:from apps.app2.models import model1
在[2 ]:model1.objects.all()
Out [2]:[< model1:model1 object>,< model1:model1 object>]
So about a year ago I started a project and like all new developers I didn't really focus to much on the structure, however now I am further along with Django it has started to appear that my project layout mainly my models are horrible in structure.
I have models mainly held in a single app and really most of these models should be in their own individual apps, I did try and resolve this and move them with south however I found it tricky and really difficult due to foreign keys ect.
However due to Django 1.7 and built in support for migrations is there a better way to do this now?
Thank you in advance for any help.
Note: ozan answer is better than mine. Please use that method
CREATING APPS & MODELS
1) Create two apps, app1 & app2.
$ python manage.py startapp app1 $ python manage.py startapp app2
2) Create two models model1 & model2.
app1/model1.py
from django.db import models class model1(models.Model): name = models.CharField(max_length=100)
app2/model2.py
from django.db import models class model1(models.Model): name = models.CharField(max_length=100)
3) Apply migrations.
$ python manage.py makemigrations app1 && python migrate app1 $ python manage.py makemigrations app2 && python migrate app2
Django will create 2 tables app1_model & app2_model2. You can add data to models.
Lets add little data into our models, to make sure we preserve data while moving model.
In [1]: from apps.app1.models import model1 In [2]: model1.objects.create() Out[2]: <model1: model1 object> In [3]: model1.objects.create() Out[3]: <model1: model1 object>
MIGRATING MODEL
Lets look how to perform the actual migration.
4) Rename Table & Move Source CodeGo to your database and rename app1_model to app2_model1. Move source code for model1 from app1 to app2 & add meta options.
app1/model1.py
from django.db import models
app2/model2.py
from django.db import models class model2(models.Model): name = models.CharField(max_length=100) class model1(models.Model): name = models.CharField(max_length=100) class Meta: managed = False db_table = 'app2_model1'
5) Apply Migrations.Apply migrations for both apps.
$ python manage.py makemigrations app2 && python manage.py migrate app2 $ python manage.py makemigrations app1 && python manage.py migrate --fake app1
Once migrations are completed, you can check the data in the model.
In [1]: from apps.app2.models import model1 In [2]: model1.objects.all() Out[2]: [<model1: model1 object>, <model1: model1 object>]
这篇关于如何在两个Django应用程序之间移动模型(Django 1.7)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!