sbdchd / django-types

:doughnut: Type stubs for Django
MIT License
188 stars 62 forks source link

ManyToManyField[Unknown, Unknown] #114

Open collinanderson opened 2 years ago

collinanderson commented 2 years ago

VSCode is saying my models.ManyToManyField(MyModel, blank=True) is ManyToManyField[Unknown, Unknown]. I'm wondering if that's because I don't have a through model specified? Is there any way to make this work correctly without hand-generating a through model? My ForeignKeys are working fine.

sbdchd commented 2 years ago

Hmm we might need to tweak the overloads a bit, I think it should be possible to have it typed properly without having to specify the through model

Javrd commented 1 year ago

This is a workaround, a bit nasty because the mock line shadows the actual line, that, regarding my editor at least, is considered as unreachable:


    if TYPE_CHECKING:
        from django.db.models.manager import ManyToManyRelatedManager
        model1 = ManyToManyRelatedManager[Model1, 'Model2']()
    else:
        model1 = models.ManyToManyField(Model1, blank=True)