我正在尝试将models.py
我的应用程序拆分为几个文件:
我的第一个猜测是这样做:
myproject/
settings.py
manage.py
urls.py
__init__.py
app1/
views.py
__init__.py
models/
__init__.py
model1.py
model2.py
app2/
views.py
__init__.py
models/
__init__.py
model3.py
model4.py
Run Code Online (Sandbox Code Playgroud)
这不起作用,然后我找到了这个,但在这个解决方案中我仍然有一个问题,当我运行时,python manage.py sqlall app1
我有类似的东西:
BEGIN;
CREATE TABLE "product_product" (
"id" serial NOT NULL PRIMARY KEY,
"store_id" integer NOT NULL
)
;
-- The following references should be added but depend on non-existent tables:
-- ALTER TABLE "product_product" ADD CONSTRAINT "store_id_refs_id_3e117eef" FOREIGN KEY ("store_id") REFERENCES "store_store" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE INDEX "product_product_store_id" ON …
Run Code Online (Sandbox Code Playgroud) 我刚刚了解到将模型类拆分为不同的文件会打破许多django的内置功能.
我来自java背景.在那里,不能接受写一个很长的类文件的好习惯.但是django对所有模型类执行单个文件可能会导致程序员编写非常长的models.py文件.这将使程序员难以看到整个域模型的组织.
那么为什么django强制执行单个文件来包含所有域类?
当我尝试迁移我的代码时,我收到此错误.这是我的代码和类:
from django.db import models
from core.models import Event
class TicketType(models.Model):
name = models.CharField(max_length=45)
price = models.DecimalField(max_length=2, decimal_places=2, max_digits=2)
type = models.CharField(max_length=45)
amount = models.IntegerField()
event = models.ForeignKey(Event)
class Meta:
app_label = "core"
import datetime
from django.core.serializers import json
from django.db import models
from core.models import User
class Event(models.Model):
page_attribute = models.TextField()
name = models.TextField(max_length=128 , default="New Event")
description = models.TextField(default="")
type = models.TextField(max_length=16)
age_limit = models.IntegerField(default=0)
end_date = models.DateTimeField(default=datetime.datetime.now())
start_date = models.DateTimeField(default=datetime.datetime.now())
is_active = models.BooleanField(default=False)
user = models.ForeignKey(User)
ticket_type=models.ForeignKey('core.models.ticket_type.TicketType') …
Run Code Online (Sandbox Code Playgroud)