You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Any idea why I'm getting this error on a ForeignKey to relation with a string value? The to parameter can either be reference to the model class or a string path to resolve the model class (to avoid circular references). If I change this to value from a string to a reference to the model class itself, I no longer get the linter warning.
$ mypy story
story/models.py:18: error: Invalid value for a to= parameter: 'Author'
This code gives me the warning from mypy:
# story/models.py
class Story(models.Model):
...
author = models.ForeignKey('Author', on_delete=models.CASCADE)
If I change it to this, then there is no warning (Author model is defined in the same file):
# story/models.py
class Story(models.Model):
...
author = models.ForeignKey(Author, on_delete=models.CASCADE)
I checked the stubs for ForeignKey in this codebase and it seems to correctly indicate Union of string and model class, so I'm a little stumped as to why this problem would happen.
The text was updated successfully, but these errors were encountered:
Hey folks!
Any idea why I'm getting this error on a ForeignKey
to
relation with a string value? Theto
parameter can either be reference to the model class or a string path to resolve the model class (to avoid circular references). If I change thisto
value from a string to a reference to the model class itself, I no longer get the linter warning.This code gives me the warning from
mypy
:If I change it to this, then there is no warning (Author model is defined in the same file):
I checked the stubs for
ForeignKey
in this codebase and it seems to correctly indicate Union of string and model class, so I'm a little stumped as to why this problem would happen.The text was updated successfully, but these errors were encountered: