Skip to content

Feat/builtin models types #2590

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

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
13 changes: 10 additions & 3 deletions django-stubs/contrib/admin/models.pyi
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from typing import Any, ClassVar
from uuid import UUID

from django.contrib.auth.models import AbstractUser
from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models import QuerySet
from django.db.models.base import Model
from django.db.models.expressions import Combinable
from typing_extensions import deprecated

ADDITION: int
Expand Down Expand Up @@ -33,10 +36,14 @@ class LogEntryManager(models.Manager[LogEntry]):
) -> list[LogEntry] | LogEntry: ...

class LogEntry(models.Model):
id: models.AutoField
pk: models.AutoField
action_time: models.DateTimeField
user: models.ForeignKey
content_type: models.ForeignKey
object_id: models.TextField
user: models.ForeignKey[AbstractUser | Combinable, AbstractUser]
user_id: int
content_type: models.ForeignKey[ContentType | Combinable | None, ContentType | None]
content_type_id: int | None
object_id: models.TextField[str | int | Combinable | None, str | None]
object_repr: models.CharField
action_flag: models.PositiveSmallIntegerField
change_message: models.TextField
Expand Down
3 changes: 2 additions & 1 deletion django-stubs/contrib/auth/base_user.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from collections.abc import Iterable
from datetime import date, datetime
from typing import Any, ClassVar, Literal, TypeVar, overload

from django.db import models
Expand All @@ -18,7 +19,7 @@ class AbstractBaseUser(models.Model):
REQUIRED_FIELDS: ClassVar[list[str]]

password = models.CharField(max_length=128)
last_login = models.DateTimeField(blank=True, null=True)
last_login = models.DateTimeField[str | datetime | date | Combinable, datetime | None](blank=True, null=True)
is_active: bool | BooleanField[bool | Combinable, bool]

def get_username(self) -> str: ...
Expand Down
67 changes: 59 additions & 8 deletions django-stubs/contrib/auth/models.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from collections.abc import Iterable
from typing import Any, ClassVar, Literal, TypeAlias, TypeVar
from typing import Any, ClassVar, Literal, TypeAlias, TypeVar, type_check_only

from django.contrib.auth.base_user import AbstractBaseUser as AbstractBaseUser
from django.contrib.auth.base_user import BaseUserManager as BaseUserManager
Expand All @@ -8,6 +8,8 @@ from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.db.models import QuerySet
from django.db.models.base import Model
from django.db.models.expressions import Combinable
from django.db.models.fields.related_descriptors import ManyToManyDescriptor
from django.db.models.manager import EmptyManager
from django.utils.functional import _StrOrPromise
from typing_extensions import Self
Expand All @@ -33,23 +35,69 @@ class PermissionManager(models.Manager[Permission]):
def get_by_natural_key(self, codename: str, app_label: str, model: str) -> Permission: ...

class Permission(models.Model):
content_type_id: int
objects: ClassVar[PermissionManager]

id: models.AutoField
pk: models.AutoField
name = models.CharField(max_length=255)
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
content_type: models.ForeignKey[ContentType | Combinable, ContentType]
content_type_id: int
codename = models.CharField(max_length=100)
group_set: ManyToManyDescriptor[Group, _Group_permissions]
user_set: ManyToManyDescriptor[User, _User_permissions]
def natural_key(self) -> tuple[str, str, str]: ...

class GroupManager(models.Manager[Group]):
def get_by_natural_key(self, name: str) -> Group: ...
async def aget_by_natural_key(self, name: str) -> Group: ...

# This is a model that only exists in Django's model registry and doesn't have any
# class statement form. It's the through model between 'Group' and 'Permission'.
@type_check_only
class _Group_permissions(models.Model):
objects: ClassVar[models.Manager[Self]]

id: models.AutoField
pk: models.AutoField
group: models.ForeignKey[Group | Combinable, Group]
group_id: int
permission: models.ForeignKey[Permission | Combinable, Permission]
permission_id: int

# This is a model that only exists in Django's model registry and doesn't have any
# class statement form. It's the through model between 'User' and 'Group'.
@type_check_only
class _User_groups(models.Model):
objects: ClassVar[models.Manager[Self]]

id: models.AutoField
pk: models.AutoField
user: models.ForeignKey[User | Combinable, User]
user_id: int
group: models.ForeignKey[Group | Combinable, Group]
group_id: int

# This is a model that only exists in Django's model registry and doesn't have any
# class statement form. It's the through model between 'User' and 'Permission'.
@type_check_only
class _User_permissions(models.Model):
objects: ClassVar[models.Manager[Self]]

id: models.AutoField
pk: models.AutoField
user: models.ForeignKey[User | Combinable, User]
user_id: int
permission: models.ForeignKey[Permission | Combinable, Permission]
permission_id: int

class Group(models.Model):
objects: ClassVar[GroupManager]

id: models.AutoField
pk: models.AutoField
name = models.CharField(max_length=150)
permissions = models.ManyToManyField(Permission)
permissions = models.ManyToManyField[Permission, _Group_permissions](Permission)
user_set: ManyToManyDescriptor[User, _User_groups]
def natural_key(self) -> tuple[str]: ...

class UserManager(BaseUserManager[_UserType]):
Expand All @@ -76,8 +124,8 @@ class UserManager(BaseUserManager[_UserType]):

class PermissionsMixin(models.Model):
is_superuser = models.BooleanField()
groups = models.ManyToManyField(Group)
user_permissions = models.ManyToManyField(Permission)
groups = models.ManyToManyField[Group, _User_groups](Group)
user_permissions = models.ManyToManyField[Permission, _User_permissions](Permission)

def get_user_permissions(self, obj: Model | None = ...) -> set[str]: ...
async def aget_user_permissions(self, obj: Model | None = ...) -> set[str]: ...
Expand Down Expand Up @@ -111,10 +159,12 @@ class AbstractUser(AbstractBaseUser, PermissionsMixin):
def get_full_name(self) -> str: ...
def get_short_name(self) -> str: ...
def email_user(
self, subject: _StrOrPromise, message: _StrOrPromise, from_email: str = ..., **kwargs: Any
self, subject: _StrOrPromise, message: _StrOrPromise, from_email: str | None = ..., **kwargs: Any
) -> None: ...

class User(AbstractUser): ...
class User(AbstractUser):
id: models.AutoField
pk: models.AutoField

class AnonymousUser:
id: None
Expand Down Expand Up @@ -148,3 +198,4 @@ class AnonymousUser:
@property
def is_authenticated(self) -> Literal[False]: ...
def get_username(self) -> str: ...
def __int__(self) -> None: ...
11 changes: 10 additions & 1 deletion django-stubs/contrib/contenttypes/models.pyi
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
from typing import Any, ClassVar

from django.contrib.admin.models import LogEntry
from django.contrib.auth.models import Permission
from django.db import models
from django.db.models.base import Model
from django.db.models.fields.related_descriptors import ReverseManyToOneDescriptor
from django.db.models.query import QuerySet

class ContentTypeManager(models.Manager[ContentType]):
def __init__(self, *args: Any, **kwargs: Any) -> None: ...
def get_by_natural_key(self, app_label: str, model: str) -> ContentType: ...
def get_for_model(self, model: type[Model] | Model, for_concrete_model: bool = ...) -> ContentType: ...
def get_for_models(self, *models: Any, for_concrete_models: bool = ...) -> dict[type[Model], ContentType]: ...
def get_for_id(self, id: int) -> ContentType: ...
def clear_cache(self) -> None: ...

class ContentType(models.Model):
id: int
id: models.AutoField
pk: models.AutoField
app_label = models.CharField(max_length=100)
model = models.CharField(max_length=100)
logentry_set: ReverseManyToOneDescriptor[LogEntry]
permission_set: ReverseManyToOneDescriptor[Permission]
objects: ClassVar[ContentTypeManager]
@property
def name(self) -> str: ...
@property
def app_labeled_name(self) -> str: ...
def model_class(self) -> type[Model] | None: ...
def get_object_for_this_type(self, using: str | None = None, **kwargs: Any) -> Model: ...
def get_all_objects_for_this_type(self, **kwargs: Any) -> QuerySet: ...
Expand Down
21 changes: 20 additions & 1 deletion django-stubs/contrib/flatpages/models.pyi
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
from typing import ClassVar, type_check_only

from django.contrib.sites.models import Site
from django.db import models
from django.db.models.expressions import Combinable
from typing_extensions import Self

# This is a model that only exists in Django's model registry and doesn't have any
# class statement form. It's the through model between 'FlatPage' and 'Site'.
@type_check_only
class _FlatPage_sites(models.Model):
objects: ClassVar[models.Manager[Self]]

id: models.AutoField
pk: models.AutoField
site: models.ForeignKey[Site | Combinable, Site]
site_id: int
flatpage: models.ForeignKey[FlatPage | Combinable, FlatPage]
flatpage_id: int

class FlatPage(models.Model):
id: models.AutoField
pk: models.AutoField
url: models.CharField
title: models.CharField
content: models.TextField
enable_comments: models.BooleanField
template_name: models.CharField
registration_required: models.BooleanField
sites: models.ManyToManyField[Site, Site]
sites: models.ManyToManyField[Site, _FlatPage_sites]
def get_absolute_url(self) -> str: ...
18 changes: 9 additions & 9 deletions django-stubs/contrib/gis/db/backends/oracle/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ from django.db.models.manager import Manager
from typing_extensions import Self

class OracleGeometryColumns(models.Model):
table_name: Any
column_name: Any
srid: Any
table_name: models.CharField
column_name: models.CharField
srid: models.IntegerField
objects: ClassVar[Manager[Self]]

@classmethod
Expand All @@ -17,12 +17,12 @@ class OracleGeometryColumns(models.Model):
def geom_col_name(cls) -> Any: ...

class OracleSpatialRefSys(models.Model, SpatialRefSysMixin):
cs_name: Any
srid: Any
auth_srid: Any
auth_name: Any
wktext: Any
cs_bounds: Any
cs_name: models.CharField
srid: models.IntegerField
auth_srid: models.IntegerField
auth_name: models.CharField
wktext: models.TextField
cs_bounds: models.PolygonField
objects: ClassVar[Manager[Self]]

@property
Expand Down
24 changes: 12 additions & 12 deletions django-stubs/contrib/gis/db/backends/postgis/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ from django.db import models
from typing_extensions import Self

class PostGISGeometryColumns(models.Model):
f_table_catalog: Any
f_table_schema: Any
f_table_name: Any
f_geometry_column: Any
coord_dimension: Any
srid: Any
type: Any
f_table_catalog: models.CharField
f_table_schema: models.CharField
f_table_name: models.CharField
f_geometry_column: models.CharField
coord_dimension: models.IntegerField
srid: models.IntegerField
type: models.CharField
objects: ClassVar[models.Manager[Self]]

@classmethod
Expand All @@ -20,11 +20,11 @@ class PostGISGeometryColumns(models.Model):
def geom_col_name(cls) -> Any: ...

class PostGISSpatialRefSys(models.Model, SpatialRefSysMixin):
srid: Any
auth_name: Any
auth_srid: Any
srtext: Any
proj4text: Any
srid: models.IntegerField
auth_name: models.CharField
auth_srid: models.IntegerField
srtext: models.CharField
proj4text: models.CharField
objects: ClassVar[models.Manager[Self]]

@property
Expand Down
24 changes: 12 additions & 12 deletions django-stubs/contrib/gis/db/backends/spatialite/models.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ from django.db import models
from typing_extensions import Self

class SpatialiteGeometryColumns(models.Model):
f_table_name: Any
f_geometry_column: Any
coord_dimension: Any
srid: Any
spatial_index_enabled: Any
type: Any
f_table_name: models.CharField
f_geometry_column: models.CharField
coord_dimension: models.IntegerField
srid: models.IntegerField
spatial_index_enabled: models.IntegerField
type: models.IntegerField
objects: ClassVar[models.Manager[Self]]

@classmethod
Expand All @@ -19,12 +19,12 @@ class SpatialiteGeometryColumns(models.Model):
def geom_col_name(cls) -> Any: ...

class SpatialiteSpatialRefSys(models.Model, SpatialRefSysMixin):
srid: Any
auth_name: Any
auth_srid: Any
ref_sys_name: Any
proj4text: Any
srtext: Any
srid: models.IntegerField
auth_name: models.CharField
auth_srid: models.IntegerField
ref_sys_name: models.CharField
proj4text: models.CharField
srtext: models.CharField
objects: ClassVar[models.Manager[Self]]

@property
Expand Down
Loading
Loading