@@ -12,21 +12,13 @@ class TypedBigAutoField(models.BigAutoField):
1212
1313 ```
1414 class MyModel(models.Model):
15- MyModelID = NewType("MyModelID", int)
16- type ID = MyModelID
17-
18- class IDField(TypedBigAutoField[ID]):
19- pass
20-
15+ # Boilerplate for fully-typed ID field:
16+ ID = NewType("ID", int)
17+ class IDField(TypedBigAutoField[ID]): ...
2118 id = IDField(primary_key=True)
2219
23- ...
20+ ... # rest of your model's fields...
2421 ```
25-
26- Note: using a full explicit name like "MyModelID" for the `NewType(...)` and
27- then aliasing it to `ID` is recommended, so you can use `MyModel.ID`
28- everywhere for convenience, but will still see "MyModelID" in the mypy
29- output, as opposed to confusing messages like "expected 'ID' but got 'ID'".
3022 """
3123
3224 # The actual typing + django-stubs "magic" is handled entirely by the .pyi file.
@@ -37,3 +29,14 @@ def __class_getitem__(cls, _):
3729 # and `class XxxField(TypedBigAutoField[XxxID])` work without `Generic` in the MRO.
3830 # (Including `Generic` as a superclass here breaks Django's field __deepcopy__.)
3931 return cls
32+
33+
34+ class TypedAutoField (models .AutoField ):
35+ """
36+ Use sparingly. This is a 32-bit version of `TypedBigAutoField`, and you
37+ should usually be using that instead of this for primary key fields.
38+ """
39+
40+ @classmethod
41+ def __class_getitem__ (cls , _ ): # See explanation on the class above.
42+ return cls
0 commit comments