问题
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 the ReturnLineItem
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.
回答1:
I was through the same error a few days ago. I had some apps and some classes sharing the same name, for instance: app called person and a model called Person, when I run makemigrations and migrate the error mentioned above take place. The easiest way I found was made a short refactoring and rename my models, delete the old migrations and make new ones. Not so clever but I was running out of time. Still looks like a bug for me.
回答2:
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.
来源:https://stackoverflow.com/questions/22445046/possible-bug-in-django-1-7a2-migrations