Opened 4 years ago

Last modified 4 years ago

#26827 assigned Cleanup/optimization

"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: Zaheer Soebhan
Component: Documentation Version: master
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: yes
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description (last modified by Zaheer Soebhan)

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 - "" 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):

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 (5)

comment:1 Changed 4 years ago by Zaheer Soebhan

Owner: changed from nobody to Zaheer Soebhan
Status: newassigned
Last edited 4 years ago by Zaheer Soebhan (previous) (diff)

comment:2 Changed 4 years ago by Zaheer Soebhan

Description: modified (diff)

Confirmed the bug in 1.9.7 and

comment:3 Changed 4 years ago by Zaheer Soebhan

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/ with Python3).
Both strings and classnames can be used now.

comment:4 Changed 4 years ago by Zaheer Soebhan

Has patch: set
Triage Stage: UnreviewedAccepted

comment:5 Changed 4 years ago by Markus Holtermann

Component: MigrationsDocumentation
Needs documentation: set
Patch needs improvement: set
Type: BugCleanup/optimization
Version: 1.9master

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 - '' does. "
    "Use a string reference instead." % (self.app_label,, name)
Last edited 4 years ago by Markus Holtermann (previous) (diff)
Note: See TracTickets for help on using tickets.
Back to Top