#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. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/051.8888b19270631292abf2617c06fcb5ec%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.