如果我运行python -Wall manage.py test此警告(和类似的),则会发生:
/local/lib/python2.7/site-packages/django/db/models/fields/__init__.py:1453:
RuntimeWarning: DateTimeField SignUpUser.signup_time received a naive datetime (2018-03-17 21:27:22.620074) while time zone support is active.RuntimeWarning)
Run Code Online (Sandbox Code Playgroud)
但模型SignUpUser不再有这样的字段了。它被称为signup_timestamp. 其他字段也会出现同样的错误。为了解决这些问题,我datetime.now考虑更改为 django 内置时区timezone.now。但错误消息也不会消失。我认为这是由于旧的迁移而发生的。
该网站已经投入使用,但只有我在开发。我应该如何解决这个问题?重置所有迁移文件并使用 --fake-initial 重做迁移?
我正在制作此 crm,但收到此错误,我将与您共享代码
我正在拍摄该视频,但出现此错误
https://www.youtube.com/watch?v=fOukA4Qh9QA&t=4925s
ERRORS:
auth.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
HINT: Add or change a related_name argument to the definition for 'User.groups' or 'User.groups'.
auth.User.user_permissions: (fields.E304) Reverse accessor for 'User.user_permissions' clashes with reverse accessor for 'User.user_permissions'. HINT: Add or change a related_name argument to the definition for 'User.user_permissions' or 'User.user_permissions'.
leads.User.groups: (fields.E304) Reverse accessor for 'User.groups' clashes with reverse accessor for 'User.groups'.
HINT: Add or change a related_name argument to the definition for …Run Code Online (Sandbox Code Playgroud) 所以我从 django 3.1 升级到 3.2,在我的两个模型上,当我进行迁移时,它不断迫使我将自动 id 更改为,BigAutoField即使我DEFAULT_AUTO_FIELD = 'django.db.models.AutoField'在更新之前已经(并且曾经)在我的设置文件中。
operations = [
migrations.AlterField(
model_name='device',
name='id',
field=models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID'),
)
Run Code Online (Sandbox Code Playgroud)
奇怪的是,它只影响几个模型,但其余的都很好,并且都在使用AutoField。
我不反对使用BigAutoField,但由于外键限制,迁移失败。
我已经删除了有问题的迁移,并从数据库中应用的迁移表中删除了它们。如何阻止 Django 强制进行此迁移?我现在很茫然。
这是我的设备型号。正如您所看到的,我没有专门设置主键,我也没有在任何其他模型上这样做,这些都很好。
from django.db import models
from company.models import Company
from django.db.models.signals import pre_save, post_save
from main.models import uuid_pre_save_generator
from django.conf import settings
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer
import json
from django.urls import reverse
from django.utils.html import escape
from django.core.validators import RegexValidator, FileExtensionValidator
class UploadedImage(models.Model): …Run Code Online (Sandbox Code Playgroud) 我一直在尝试使用1.7中的迁移将Django应用中的模型更改同步(postgres 9.1-如果您需要更多有关我的环境的详细信息,请告诉我),但是manage.py migration似乎没有任何作用,并且sqlmigrate不发出任何SQL。
我以为Django 1.7-在进行makemigrations之后运行迁移时“不适用任何迁移”可能适用于我的情况,并且确实在数据库的django_migrations表中找到了一些历史记录。我删除了我要迁移的应用程序的记录。
最近,我放弃了让alter table语句生成/运行并放弃该表的原始版本。尽管manage.py migration声明它正在应用迁移,但数据库没有任何反应。
这是我一直在尝试的步骤:
删除历史记录。
rm -r myapp/migrations
../manage.py dbshell
myapp_db=> delete from django_migrations where app='myapp'
Run Code Online (Sandbox Code Playgroud)
创建一个初始迁移。
cp myapp/models.py.orig myapp/models.py
../manage.py makemigrations myapp
../manage.py migrate
Run Code Online (Sandbox Code Playgroud)
manage.py migration返回以下内容:
....
Running migrations:
Applying myapp.0001_initial... FAKED
Run Code Online (Sandbox Code Playgroud)
然后,我换入新模型并生成新的迁移。
cp myapp/models.py.new myapp/models.py
../manage.py makemigrations myapp
Run Code Online (Sandbox Code Playgroud)
makemigrations的结果位于myapp / migrations / 0002_notificationlog.py中:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('myapp', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='NotificationLog',
fields=[ …Run Code Online (Sandbox Code Playgroud) 我遇到了一个奇怪的问题.我在Mac OS X Yosemite上使用Django 1.7.1,我已经配置了一个本地MySQL数据库.
通常,我创建一个模型,如果我想添加另一个字段,我只需要做一个./manage.py migrate,Django创建一个新的数据库列.
我做到了那一点.这是我的模特:
from django.db import models
from django.utils.translation import ugettext as _
class Product(models.Model):
CATEGORY_CHOICES = (
('apphosting', _('Application Hosting')),
('webhosting', _('Web Hosting'))
)
category = models.CharField(max_length=25, choices=CATEGORY_CHOICES)
name = models.CharField(max_length=25)
price_monthly = models.FloatField()
def __unicode__(self):
return self.name
Run Code Online (Sandbox Code Playgroud)
请注意,我已添加该字段price_monthly.然后,我做了一个./manage.py migrate:
(mysphere)dmanser@ragamuffin:~/git/mysphere on master [!?]$ ./manage.py migrate
Operations to perform:
Synchronize unmigrated apps: crispy_forms
Apply all migrations: customers, sessions, admin, sites, flatpages, contenttypes, products, auth
Synchronizing apps without migrations: …Run Code Online (Sandbox Code Playgroud) 我需要将模型字段的关系从ForeignKey更改为ManyToManyField.这带有数据迁移,用于更新预先存在的数据.
以下是原始模型(models.py):
class DealBase(models.Model):
[...]
categoria = models.ForeignKey('Categoria')
[...]
)
Run Code Online (Sandbox Code Playgroud)
我需要模型字段'categoria'来与应用'deal'中的模型'Categoria'建立多种关系.
我做了什么:
在DealBase中创建一个新字段'categoria_tmp'
class DealBase(models.Model):
categoria = models.ForeignKey('Categoria')
categoria_tmp = models.ManyToManyField('Categoria',related_name='categoria-temp')
Run Code Online (Sandbox Code Playgroud)进行模式迁移
python manage.py makemigrations
编辑migrationfile.py以将数据从categoria迁移到categoria-tmp
def copy_deal_to_dealtmp(apps, schema_editor):
DealBase = apps.get_model('deal', 'DealBase')
for deal in DealBase.objects.all():
deal.categoria_tmp.add(deal.categoria)
deal.save()
class Migration(migrations.Migration):
dependencies = [
('deal', '0017_dealbase_indirizzo'),
]
operations = [
migrations.AddField(
model_name='dealbase',
name='categoria_tmp',
field=models.ManyToManyField(related_name='categoria-temp', to='deal.Categoria'),
preserve_default=True,
),
migrations.RunPython(
copy_deal_to_dealtmp
)
]
Run Code Online (Sandbox Code Playgroud)进行数据迁移
python manage.py migrate
最后,我需要删除列'dealbase.categoria'并将列'dealbase.categoria-tmp'重命名为'dealbase.categoria'
我陷入了第5步.
有人可以帮帮我吗?我在网上找不到答案,我正在使用Django 1.8.
谢谢!
我错误地删除了路径projectName/appName/migrations下的所有.py文件,它包括:
0001_initial.py
0011_auto_20150918_0723.py
0002_auto_20150819_1301.py
...
现在,即使我更新了Model文件,并运行该命令python manage.py makemigrations,它始终会在未检测到更改的情况下提示.
我怎样才能恢复一切?
所以首先我跑makemigrations,然后我跑了migrate,得到了错误ValueError: The database backend does not accept 0 as a value for AutoField.
所以我继续,修改我models.py的修复错误.
现在,当我makemigrations重新开始运行时,它会起作用.然后migrate实际上没有做任何事情,但再次显示错误.所以那里有一个很糟糕的迁移,那么之后有一个是正确的.
所以我./manage.py migrate --fake mainapp zero在Stack Overflow上阅读之后尝试了运行,现在就说了django.db.utils.OperationalError: (1050, "Table 'mainapp_article' already exists").
我有什么想法可以回到我开始的地方,现在重新尝试makemigrations并从models.py中删除错误?
我正在尝试在PostgreSQL 9.6.5数据库上运行Django 1.11迁移,但遇到了奇怪的错误:
Applying myapp.0011_auto_20171130_1807...Traceback (most recent call last):
File "manage.py", line 9, in <module>
execute_from_command_line(sys.argv)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 356, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/core/management/base.py", line 283, in run_from_argv
self.execute(*args, **cmd_options)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/core/management/base.py", line 330, in execute
output = self.handle(*args, **options)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 204, in handle
fake_initial=fake_initial,
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 115, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "/usr/local/myproject/.env/local/lib/python2.7/site-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) …Run Code Online (Sandbox Code Playgroud) 我的ali项目()上有一个应用()website,我希望它具有自己的数据库。问题是,当我运行时python manage.py migrate --database=ali,该命令将重新创建ali数据库中的所有表。而预期的结果将是仅拥有ali_search数据库。
PS:我运行了一些测试后,该应用程序似乎正在按预期运行。换句话说,我的ali应用程序中的模型已保存在ali数据库中。尽管如此,将所有这些空表放在我的ali数据库中仍然不是正确的方法。
设定:
# website.settings
...
INSTALLED_APPS = [
'base.apps.BaseConfig',
'ali.apps.AliConfig',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sitemaps',
'django_comments',
'mptt',
'tagging',
'zinnia',
]
....
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'website',
'USER': 'website',
'PASSWORD': 'website',
'HOST': 'localhost',
'PORT': '5432',
},
'ali': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'ali',
'USER': 'ali',
'PASSWORD': 'ali',
'HOST': 'localhost',
'PORT': '5432',
}
} …Run Code Online (Sandbox Code Playgroud)