{{{
ValueError: ModelState.fields cannot refer to a model class -
"runs.to" does. Use a string reference instead.
}}}
I still got the error after restarting migrations from scratch, so I
determined it is about the migration looking at the currently declared
models, not the historical model migration scripts.
I have this (trimmed) code:
{{{
class _RunsField(models.ManyToManyField):
def __init__(self, *args, **kwargs):
super(_RunsField, self).__init__(Run,
through='BaselineAssignment')
class Baseline(models.Model):
runs = _RunsField()
class BaselineAssignment(models.Model):
run = models.ForeignKey('Run', on_delete=models.CASCADE)
baseline = models.ForeignKey('Baseline', on_delete=models.CASCADE)
}}}
If I change {{{Run}}} in the {{{__init__}}} in {{{_RunsField}}} to
{{{'Run'}}} (quoted) the problem goes away.
This seems like this is something that should have been handled by the
underlying migration code, and even if it isn't, the error message is
quite unhelpful.
--
Ticket URL: <https://code.djangoproject.com/ticket/26827>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* owner: nobody => Zaheers
* needs_docs: => 0
* status: new => assigned
* needs_tests: => 0
* needs_better_patch: => 0
Comment:
I've tried to reproduce the same error in a fresh project with Django
1.9.7. When I run
--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:1>
Old description:
> I ended up in a weird situation (Django 1.9.7) where I couldn't make any
> additional migrations. So I reset my migrations thinking the problem was
> somehow in the migration scripts themselves:
>
> {{{
> ValueError: ModelState.fields cannot refer to a model class -
> "runs.to" does. Use a string reference instead.
> }}}
>
> I still got the error after restarting migrations from scratch, so I
> determined it is about the migration looking at the currently declared
> models, not the historical model migration scripts.
>
> I have this (trimmed) code:
>
> {{{
> class _RunsField(models.ManyToManyField):
>
> def __init__(self, *args, **kwargs):
> super(_RunsField, self).__init__(Run,
> through='BaselineAssignment')
>
> class Baseline(models.Model):
>
> runs = _RunsField()
>
> class BaselineAssignment(models.Model):
> run = models.ForeignKey('Run', on_delete=models.CASCADE)
> baseline = models.ForeignKey('Baseline', on_delete=models.CASCADE)
>
> }}}
>
> If I change {{{Run}}} in the {{{__init__}}} in {{{_RunsField}}} to
> {{{'Run'}}} (quoted) the problem goes away.
>
> This seems like this is something that should have been handled by the
> underlying migration code, and even if it isn't, the error message is
> quite unhelpful.
New description:
I ended up in a weird situation (Django 1.9.7) where I couldn't make any
additional migrations. So I reset my migrations thinking the problem was
somehow in the migration scripts themselves:
{{{
ValueError: ModelState.fields cannot refer to a model class -
"runs.to" does. Use a string reference instead.
}}}
I still got the error after restarting migrations from scratch, so I
determined it is about the migration looking at the currently declared
models, not the historical model migration scripts.
I have this (trimmed) code:
{{{
class Run(models.Model):
pass
class _RunsField(models.ManyToManyField):
def __init__(self, *args, **kwargs):
super(_RunsField, self).__init__(Run,
through='BaselineAssignment')
class Baseline(models.Model):
runs = _RunsField()
class BaselineAssignment(models.Model):
run = models.ForeignKey('Run', on_delete=models.CASCADE)
baseline = models.ForeignKey('Baseline', on_delete=models.CASCADE)
}}}
If I change {{{Run}}} in the {{{__init__}}} in {{{_RunsField}}} to
{{{'Run'}}} (quoted) the problem goes away.
This seems like this is something that should have been handled by the
underlying migration code, and even if it isn't, the error message is
quite unhelpful.
--
Comment (by Zaheers):
Confirmed the bug in 1.9.7 and 1.11.dev.
--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:2>
Comment (by Zaheers):
Removed raisings of exceptions to see if other tests would fail and figure
out a way to use classes instead of strings.
As it turns out, the existing tests don't fail. (Default tests/runtests.py
with Python3).
Both strings and classnames can be used now.
--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:3>
* has_patch: 0 => 1
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:4>
* component: Migrations => Documentation
* needs_docs: 0 => 1
* version: 1.9 => master
* type: Bug => Cleanup/optimization
* needs_better_patch: 0 => 1
Comment:
The checks are in there for reasons (which is why there are tests as well
;) ). This has performance, consistency resons while moving ModelStates
from migration to migration. Instead we should add a comment to the docs
in the "Custom Model Fields" part that one cannot do what you did above
but should use `'myapp.Run'` as a string reference instead.
I'm also fine if you update the message to something along the lines:
{{{#!python
raise ValueError(
'Model fields in 'ModelState.fields' cannot refer to a model class.
"%s.%s.%s.to" does. '
'Use a string reference instead.' % (self.app_label, self.name, name)
)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:5>
* owner: Zaheer Soebhan => (none)
* needs_better_patch: 1 => 0
* has_patch: 1 => 0
* status: assigned => new
* needs_docs: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:6>
* owner: (none) => Prashant Pandey
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:7>
Comment (by Prashant Pandey):
https://github.com/django/django/pull/17510
--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:8>
* has_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:9>
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:10>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"a8adb6aa6cc6b9efd043acc980b5744bc211c760" a8adb6aa]:
{{{
#!CommitTicketReference repository=""
revision="a8adb6aa6cc6b9efd043acc980b5744bc211c760"
Fixed #26827 -- Improved ModelState error message when relations refer
model classes.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:11>