我使用基于 Django 的应用程序创建一个容器,使用 PostgreSQL 构建另一个容器,使用docker-compose.
makemigrations如果我在应用程序 docker 容器(链接到 PostgreSQL 容器)内运行bash 会话的外部,如下所示:
python manage.py makemigrations myapp
Run Code Online (Sandbox Code Playgroud)
我得到以下异常:
django.db.utils.OperationalError: could not
translate host name "db" to address: nodename
nor servname provided, or not known
Run Code Online (Sandbox Code Playgroud)
这是有道理的:我在容器执行上下文之外没有任何 PostgreSQL 实例(和配置)。
根据我过去使用其他语言的经验以及类似这篇文章的经验,我希望运行makemigrations并提交它。
这是创建和提交迁移文件的最佳方法:
settings.py DATABASE为能够makemigrations从本地 bash 会话运行命令?makemigrations容器内部执行的结果复制到我的存储库中,然后提交吗?docker-entrypoint.sh方法(示例)进行迁移并运行它们?我正在为模型使用Django 1.7和django-polymorphic
class ReferenceItem(PolymorphicModel):
created_at = models.DateTimeField(_('date created'), auto_now_add=True, db_index=True)
updated_at = models.DateTimeField(_('date modified'), auto_now=True, db_index=True)
uuid = UUIDField(auto=True, unique=True)
description = models.CharField(max_length=255)
class OrderItem(ReferenceItem):
order = models.ForeignKey('Order', related_name='items')
sku = models.CharField(max_length=255)
quantity = models.IntegerField()
unit_price = models.DecimalField(max_digits=10, decimal_places=2)
amount = models.DecimalField(max_digits=10, decimal_places=2)
tax_rate = models.DecimalField(max_digits=3, decimal_places=2)
commission_rate = models.DecimalField(max_digits=3, decimal_places=2)
Run Code Online (Sandbox Code Playgroud)
当我运行时makemigrations,出现此错误:
raise InvalidBasesError("Cannot resolve bases for %r\nThis can happen if you are inheriting models from an app with migrations (e.g. contrib.auth)\n in an app with no migrations; …Run Code Online (Sandbox Code Playgroud) class Categories(models.Model):
id = models.ForeignKey('auth.User',primary_key=True)
name = models.CharField(max_length=100)
description = models.CharField(max_length=300)
def __str__(self):
return Categories.name
class Meta:
order_with_respect_to = 'id'
class Specializations(models.Model):
id = models.ForeignKey('auth.User',primary_key=True)
name = models.CharField(max_length=100)
description = models.CharField(max_length=300)
categories = models.ForeignKey(Categories, on_delete=models.CASCADE)
def __str__(self):
return Specializations.name
Run Code Online (Sandbox Code Playgroud)
course.Categories.id: (fields.W342) 在外键上设置 unique=True 与使用 OneTo OneField 具有相同的效果。
HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.
Run Code Online (Sandbox Code Playgroud)
course.Courses.id:(fields.W342) 在外键上设置 unique=True 与使用 OneToOne 字段具有相同的效果。
HINT: ForeignKey(unique=True) is usually better served by a OneToOneField.
Run Code Online (Sandbox Code Playgroud)
course.Specializations.id: (fields.W342) 在外键上设置 unique=True 与使用 OneToOneField 具有相同的效果。
尽管关系是一对多,但仍会出现此警告或错误!不是一对一
我正在使用 virtualenv 在运行 python3.5 和 Django 1.11 的开发服务器上工作
当我使用“ python manage.py makemigrations ”时出现此错误
Traceback (most recent call last):
File "manage.py", line 8, in <module>
from django.core.management import execute_from_command_line
File "/opt/hafez_bot/env/lib/python3.5/site-packages/django/core/management/__init__.py", line 13, in <module>
from django.core.management.base import (
File "/opt/hafez_bot/env/lib/python3.5/site-packages/django/core/management/base.py", line 17, in <module>
from django.db.migrations.exceptions import MigrationSchemaMissing
File "/opt/hafez_bot/env/lib/python3.5/site-packages/django/db/migrations/__init__.py", line 1, in <module>
from .migration import Migration, swappable_dependency # NOQA
ImportError: No module named 'django.db.migrations.migration'
请帮我
我知道这是关于我的“ manage.py ”文件,它与迁移无关。因为“python manage.py help”引发了同样的错误
这是我的“ pip freeze ”输出:
certifi==2017.7.27.1
Django==1.11.6
djangorestframework==3.6.4
future==0.16.0 …Run Code Online (Sandbox Code Playgroud) 我有一个 python2.7 django 项目(我知道,我在 20 世纪!),其中有一些模型。我需要重写 makemigrations ,以便迁移文件名的形式为 0001.py、0002.py 等,而不是像 0001_initial.py、0002_model1.py 等那样,默认情况下会发生这种情况。
我已经研究了如何创建自定义管理.py 命令,并且能够覆盖 makemigrations 命令。目前我的自定义命令(python2.7)的代码如下所示:
路径/to/project/app/management/commands/makemigrations.py
from django.core.management.commands.makemigrations import Command as CoreMakeMigrationsCommand
class Command(CoreMakeMigrationsCommand):
def handle(self, *args, **options):
super(Command, self).handle(*args, **options)
Run Code Online (Sandbox Code Playgroud)
目前它只是调用原来的 makemigrations 而已。autodetector.py我需要能够修改决定文件命名的方式(这是 makemigrations 流程的一部分)。在这个文件中,有suggest_name如下所示的方法:
@classmethod
def suggest_name(cls, ops):
"""
Given a set of operations, suggest a name for the migration they might
represent. Names are not guaranteed to be unique, but put some effort
into the fallback name to avoid VCS conflicts if …Run Code Online (Sandbox Code Playgroud) 我在使用django模型迁移方面遇到了麻烦.我的应用程序中有一些模型,我已经有了一些数据.当我在我的应用程序中添加一些模型并运行时makemigrations,应用程序报告没有任何变化.我知道迁移时有时会出现一些错误,所以我删除了数据库中的django_migrations表并makemigrations再次运行,现在程序找到了我的新字段.
现在的问题是,如果我运行migrate系统告诉我一些表已经存在.(这是正确的,因为他们这样做).我不想删除那些表,因为我已经有了数据.
我无法运行migrate --fake,因为程序会认为我已经拥有了所有表,这不是真的.
所以,我正在寻找一种告诉程序的方法:运行迁移,如果表存在则跳过它.(--fake它)
另一个问题是为什么会发生这种情况,makemigrations无法识别我的更改(一些缓存问题,......)?