Skip to content
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

Not detecting Foreign key reverse relationship #150

Closed
canassa opened this issue Aug 31, 2019 · 7 comments · Fixed by #171
Closed

Not detecting Foreign key reverse relationship #150

canassa opened this issue Aug 31, 2019 · 7 comments · Fixed by #171
Labels
bug Something isn't working

Comments

@canassa
Copy link
Contributor

canassa commented Aug 31, 2019

Hello,

I couldn't get the reverse relationships to work, at first I thought that it's not supported but from browser other issues it seems that it's supported.

Here is an example based on my code:

class Transaction(models.Model):
    ...
    def test(self) -> None:
        print(self.transactionlog_set)

class TransactionLog(models.Model):
    ...
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE)

This fails with error: "Transaction" has no attribute "transactionlog_set"

I also tried to add the related_name but it didn't help.

Thanks!

mypy==0.720
django-stubs==1.1.0

@sobolevn sobolevn added the bug Something isn't working label Aug 31, 2019
@mkurnikov
Copy link
Member

I can't reproduce. Could you provide more info? Are those models in the same file, or in different ones?

Also, as always, make sure plugin is enabled in mypy.ini.

@canassa
Copy link
Contributor Author

canassa commented Sep 17, 2019

Yes, it's in the same file. This is my setup.cfg config:

[mypy]
python_version = 3.7
check_untyped_defs = True
ignore_errors = False
ignore_missing_imports = True
strict_optional = True
warn_unused_ignores = True
warn_redundant_casts = True
warn_unused_configs = True
disallow_untyped_defs = False
plugins =
    mypy_django_plugin.main

[mypy.plugins.django-stubs]
# specify settings module to use for django.conf.settings, this setting
# could also be specified with DJANGO_SETTINGS_MODULE environment variable
# (it also takes priority over config file)
django_settings_module = config.settings.local
ignore_missing_settings = True

[mypy-*.migrations.*]
ignore_errors = True

@canassa
Copy link
Contributor Author

canassa commented Sep 17, 2019

I will try to create a small project to reproduce the error

@canassa
Copy link
Contributor Author

canassa commented Sep 17, 2019

Okay, it works fine on a minimal project, so I guess it's something related to my project. I will investigate a bit more

@canassa
Copy link
Contributor Author

canassa commented Sep 17, 2019

I found the problem (sorry for the spam), my model has a custom manager:

class Transaction(models.Model):
    objects = TransactionQuerySet.as_manager()

and my manager runs like this:

class TransactionQuerySet(models.QuerySet):
    ....

@mkurnikov
Copy link
Member

mkurnikov commented Sep 17, 2019

I still don't quite understand how custom manager could affect a ForeignKey relationship. Could you describe a bit more, or would you be able to provide a project / file to reproduce the issue?

@mkurnikov
Copy link
Member

Nevermind, I can reproduce with

from django.db import models
class TransactionQuerySet(models.QuerySet):
    pass
class Transaction(models.Model):
    objects = TransactionQuerySet.as_manager()
    def test(self) -> None:
        self.transactionlog_set
class TransactionLog(models.Model):
    transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE)

Thanks for the insight!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
3 participants