[Django] #26827: "ModelState.fields cannot refer to a model class ... Use a string reference instead." when using custom model field derived from ManyToMany

126 views
Skip to first unread message

Django

unread,
Jul 1, 2016, 1:29:12 PM7/1/16
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
----------------------------+--------------------
Reporter: rrauenza | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/26827>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Jul 2, 2016, 5:17:50 AM7/2/16
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
----------------------------+--------------------------------------
Reporter: rrauenza | Owner: Zaheers
Type: Bug | Status: assigned
Component: Migrations | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------
Changes (by Zaheers):

* 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>

Django

unread,
Jul 2, 2016, 5:57:57 AM7/2/16
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
----------------------------+--------------------------------------
Reporter: rrauenza | Owner: Zaheers
Type: Bug | Status: assigned
Component: Migrations | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------

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>

Django

unread,
Jul 2, 2016, 9:10:22 AM7/2/16
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
----------------------------+--------------------------------------
Reporter: rrauenza | Owner: Zaheers
Type: Bug | Status: assigned
Component: Migrations | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------------------------

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>

Django

unread,
Jul 2, 2016, 9:12:41 AM7/2/16
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
----------------------------+------------------------------------

Reporter: rrauenza | Owner: Zaheers
Type: Bug | Status: assigned
Component: Migrations | Version: 1.9
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
----------------------------+------------------------------------
Changes (by Zaheers):

* has_patch: 0 => 1
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:4>

Django

unread,
Jul 2, 2016, 9:39:10 AM7/2/16
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
--------------------------------------+------------------------------------
Reporter: rrauenza | Owner: Zaheers
Type: Cleanup/optimization | Status: assigned
Component: Documentation | Version: master

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by MarkusH):

* 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>

Django

unread,
Apr 21, 2021, 4:55:32 AM4/21/21
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
--------------------------------------+------------------------------------
Reporter: Rich Rauenzahn | Owner: (none)
Type: Cleanup/optimization | Status: new
Component: Documentation | Version: dev

Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
--------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* 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>

Django

unread,
Nov 22, 2023, 1:29:31 PM11/22/23
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
-------------------------------------+-------------------------------------
Reporter: Rich Rauenzahn | Owner: Prashant
Type: | Pandey
Cleanup/optimization | Status: assigned

Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Prashant Pandey):

* owner: (none) => Prashant Pandey


* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:7>

Django

unread,
Nov 23, 2023, 12:02:36 AM11/23/23
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
-------------------------------------+-------------------------------------
Reporter: Rich Rauenzahn | Owner: Prashant
Type: | Pandey
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Prashant Pandey):

https://github.com/django/django/pull/17510

--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:8>

Django

unread,
Nov 23, 2023, 12:03:40 AM11/23/23
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
-------------------------------------+-------------------------------------
Reporter: Rich Rauenzahn | Owner: Prashant
Type: | Pandey
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Prashant Pandey):

* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:9>

Django

unread,
Nov 23, 2023, 3:19:05 PM11/23/23
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
-------------------------------------+-------------------------------------
Reporter: Rich Rauenzahn | Owner: Prashant
Type: | Pandey
Cleanup/optimization | Status: assigned
Component: Documentation | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/26827#comment:10>

Django

unread,
Nov 23, 2023, 4:34:05 PM11/23/23
to django-...@googlegroups.com
#26827: "ModelState.fields cannot refer to a model class ... Use a string reference
instead." when using custom model field derived from ManyToMany
-------------------------------------+-------------------------------------
Reporter: Rich Rauenzahn | Owner: Prashant
Type: | Pandey
Cleanup/optimization | Status: closed
Component: Documentation | Version: dev
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages