Opened 8 years ago
Last modified 13 months ago
#26827 closed Cleanup/optimization
"ModelState.fields cannot refer to a model class ... Use a string reference instead." when using custom model field derived from ManyToMany — at Version 2
Reported by: | Rich Rauenzahn | Owned by: | Zaheer Soebhan |
---|---|---|---|
Component: | Documentation | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description (last modified by )
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.
Change History (2)
comment:1 by , 8 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 8 years ago
Description: | modified (diff) |
---|
Confirmed the bug in 1.9.7 and 1.11.dev.