mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-09 05:24:53 +08:00
add ForeignKey
This commit is contained in:
@@ -7,3 +7,5 @@ from .fields import (AutoField as AutoField,
|
||||
Field as Field,
|
||||
SlugField as SlugField,
|
||||
TextField as TextField)
|
||||
from .fields.related import (ForeignKey as ForeignKey)
|
||||
from .deletion import CASCADE as CASCADE
|
||||
|
||||
2
django-stubs/db/models/deletion.pyi
Normal file
2
django-stubs/db/models/deletion.pyi
Normal file
@@ -0,0 +1,2 @@
|
||||
def CASCADE(collector, field, sub_objs, using):
|
||||
...
|
||||
14
django-stubs/db/models/fields/related.pyi
Normal file
14
django-stubs/db/models/fields/related.pyi
Normal file
@@ -0,0 +1,14 @@
|
||||
from typing import Type, Union, TypeVar, Any, Generic
|
||||
|
||||
from django.db import models
|
||||
from django.db.models import Field
|
||||
|
||||
_T = TypeVar('_T', bound=models.Model)
|
||||
|
||||
|
||||
class ForeignKey(Field, Generic[_T]):
|
||||
def __init__(self,
|
||||
to: Union[Type[_T], str],
|
||||
on_delete: Any,
|
||||
**kwargs): ...
|
||||
def __get__(self, instance, owner) -> _T: ...
|
||||
14
test/test-data/check-model-relations.test
Normal file
14
test/test-data/check-model-relations.test
Normal file
@@ -0,0 +1,14 @@
|
||||
[case testForeignKeyWithClass]
|
||||
from django.db import models
|
||||
|
||||
class User(models.Model):
|
||||
pass
|
||||
|
||||
class Profile(models.Model):
|
||||
user = models.ForeignKey(to=User, on_delete=models.CASCADE)
|
||||
|
||||
profile = Profile()
|
||||
reveal_type(profile.user) # E: Revealed type is 'main.User*'
|
||||
[out]
|
||||
|
||||
|
||||
@@ -15,7 +15,8 @@ MYPY_INI_PATH = ROOT_DIR / 'test' / 'plugins.ini'
|
||||
class DjangoTestSuite(DataSuite):
|
||||
files = [
|
||||
'check-model-fields.test',
|
||||
'check-postgres-fields.test'
|
||||
'check-postgres-fields.test',
|
||||
'check-model-relations.test'
|
||||
]
|
||||
data_prefix = str(TEST_DATA_DIR)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user