Closed
Description
Hi.
I'm getting is nullable but its generic get type parameter is not optional
for fields with derived ForeignKey
field types.
Given my main model uses a "name" as primary key, I'd like to use _name
instead of _id
as attname and dbcolum suffix, and I think I can only overwrite dbcolumn in a fields constructor (not the attname).
Demo code:
class ForeignNameKey(models.ForeignKey):
def get_attname(self) -> str:
return '%s_name' % self.name
class Domain(models.Model):
name = models.CharField(unique=True, max_length=255, blank=False, primary_key=True)
class Foo(models.Model):
domain = ForeignNameKey(
Domain,
on_delete=models.RESTRICT,
null=True,
)
-> error: ForeignNameKey is nullable but its generic get type parameter is not optional
I tried understanding what the code from #2048 does, but couldn't figure out why it works with models.ForeignKey
and not with my derived class. Also the error message is completely useless - I have no idea how to "fix" it (also unclear what is wrong; I don't think anything is actually wrong).
System information
- OS: debian bookworm
python
version: 3.11.2django
version: 5.1.2mypy
version: 1.11.2django-stubs
version: 5.1.0django-stubs-ext
version: 5.1.0