Django 1.7a2迁移中可能出现的错误? [英] Possible bug in Django 1.7a2 Migrations?
问题描述
我一直在努力解决这个问题,没有任何运气...
这是我尝试迁移时遇到的错误应用程序使用makemigrations命令后。
self._related_fields = self.resolve_related_fields()
文件/ home / cg / webdev / rivos / local / lib / python2.7 / site-packages / django-django-834d78f / django / db / models / fields / related.py,第1386行,在resolve_rel ated_fields
raise ValueError('相关模型%r无法解析'%self.rel.to)
ValueError:相关模型'checkout_mgr.ReturnReceipt'无法解析
这是正在创建错误的迁移:
#encoding:utf8
from django。 db导入模型,迁移
import django.core.validators
类迁移(migrations.Migration):
依赖关系= [
('checkout_mgr','0001_initial'),
]
operations = [
migrations.CreateModel(
name ='ReturnLineItem',
fields = [
(u'id',型号) .AutoField(verbose_name = u'ID',serialize = False,auto_created = True,primary_key = True)),
('return_receipt',models.ForeignKey(to ='checkout_mgr.ReturnReceipt',to_field = u'id ')),
('purchase_line_item',models.ForeignKey(to ='checkout_mgr.ReceiptLineItem',to_field = u'id')),
('return_reason',models.CharField(default = ',max_length = 2,选择= [('',' - 请选择 - '),('PD','产品缺陷'),('PU','产品不满意'),('CU' 客户不满意),('LP','客户找到更低的价格'),('CV','竞争对手提供更多价值')])),
('return_reason_details',models.TextField(blank =真的))
('quantity',models.IntegerField(default = 1,validators = [django.core.validators.MinValueValidator(1),django.core.validators.MaxValueValidator(100)])),
],
options = {
},
bas es =(models.Model,),
),
]
相关模型:
class ReturnReceipt(Receipt):
pre>
class Meta:
proxy = True
def __init __(self,* args,** kwargs):
重载__init__将收据类型设置为RF:返回收据
super(ReturnReceipt,self).__ init __(* args,** kwargs)
self.type ='RF'
self._subtotal = self._tax_total = self._total = 0
self.totals_calculated = False
class ReturnLineItem(models.Model):
return_receipt = models.ForeignKey(ReturnReceipt)
ReturnReceipt
是一个代理模型,在中被用作外键ReturnLineItem
模型。
Django似乎在上面正确生成了迁移,但我不明白错误。
我会发布在这里,而不是提交错误报告,以防我完全愚蠢的事情。
解决方案对我有用的解决方案是完全删除我的迁移文件夹和数据库,然后运行命令 -
python manage.py makemigrations
python manage.py migrate
因为这个错误发生在我身上,因为外键有些错位,甚至在撤消之后,这个错误也没有进行。
I've been trying to figure this out all day without any luck...
This is the error that I'm getting when I try to migrate an app after using the "makemigrations" command.
self._related_fields = self.resolve_related_fields() File "/home/cg/webdev/rivos/local/lib/python2.7/site-packages/django-django-834d78f/django/db/models/fields/related.py", line 1386, in resolve_rel ated_fields raise ValueError('Related model %r cannot be resolved' % self.rel.to) ValueError: Related model 'checkout_mgr.ReturnReceipt' cannot be resolved
This is the migration that seems to be creating the error:
# encoding: utf8 from django.db import models, migrations import django.core.validators class Migration(migrations.Migration): dependencies = [ ('checkout_mgr', '0001_initial'), ] operations = [ migrations.CreateModel( name='ReturnLineItem', fields=[ (u'id', models.AutoField(verbose_name=u'ID', serialize=False, auto_created=True, primary_key=True)), ('return_receipt', models.ForeignKey(to='checkout_mgr.ReturnReceipt', to_field=u'id')), ('purchase_line_item', models.ForeignKey(to='checkout_mgr.ReceiptLineItem', to_field=u'id')), ('return_reason', models.CharField(default='', max_length=2, choices=[('', '-- Please Select --'), ('PD', 'Product defective'), ('PU', 'Product unsatisfactory'), ('CU', 'Customer unsatisfied'), ('LP', 'Customer found lower price'), ('CV', 'Competitor offers more value')])), ('return_reason_details', models.TextField(blank=True)), ('quantity', models.IntegerField(default=1, validators=[django.core.validators.MinValueValidator(1), django.core.validators.MaxValueValidator(100)])), ], options={ }, bases=(models.Model,), ), ]
The related models:
class ReturnReceipt(Receipt): class Meta: proxy = True def __init__(self, *args, **kwargs): """ Overload __init__ to set the receipt type to RF: Return receipt """ super(ReturnReceipt, self).__init__(*args, **kwargs) self.type = 'RF' self._subtotal = self._tax_total = self._total = 0 self.totals_calculated = False class ReturnLineItem(models.Model): return_receipt = models.ForeignKey(ReturnReceipt)
ReturnReceipt
is a proxy model and is being used as a Foreign Key in theReturnLineItem
model.Django seems to generate the migration correctly above, but I don't understand the error.
I though I'd post here first rather than file a bug report in case I'm doing something completely silly.
解决方案The solution which worked for me is to delete my migrations folder and database completely thereafter running command-
python manage.py makemigrations python manage.py migrate
because this error occured to me due to some misplacement of foreign key, and even after undo , this error was not going.
这篇关于Django 1.7a2迁移中可能出现的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!