Skip to content

Django ForeignKey error: Invalid value for a to= parameter with str value #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
dvndrsn opened this issue Feb 20, 2019 · 2 comments
Closed

Comments

@dvndrsn
Copy link

dvndrsn commented Feb 20, 2019

Hey folks!

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.

@mkurnikov
Copy link
Member

Try master, I recently pushed some code addressing this problem.

@mkurnikov
Copy link
Member

Closing issue for now, feel free to reopen if problem is still present on the latest release.

@TonyRippy TonyRippy mentioned this issue Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants