Opened 9 years ago
Closed 2 years ago
#26827 closed Cleanup/optimization (fixed)
"ModelState.fields cannot refer to a model class ... Use a string reference instead." when using custom model field derived from ManyToMany
| Reported by: | Rich Rauenzahn | Owned by: | Prashant Pandey |
|---|---|---|---|
| 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 (11)
comment:1 by , 9 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:2 by , 9 years ago
| Description: | modified (diff) |
|---|
comment:3 by , 9 years ago
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.
comment:4 by , 9 years ago
| Has patch: | set |
|---|---|
| Triage Stage: | Unreviewed → Accepted |
comment:5 by , 9 years ago
| Component: | Migrations → Documentation |
|---|---|
| Needs documentation: | set |
| Patch needs improvement: | set |
| Type: | Bug → Cleanup/optimization |
| Version: | 1.9 → master |
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:
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) )
comment:6 by , 5 years ago
| Has patch: | unset |
|---|---|
| Needs documentation: | unset |
| Owner: | removed |
| Patch needs improvement: | unset |
| Status: | assigned → new |
comment:7 by , 2 years ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:9 by , 2 years ago
| Has patch: | set |
|---|
comment:10 by , 2 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Confirmed the bug in 1.9.7 and 1.11.dev.