mirror of
https://github.com/davidhalter/django-stubs.git
synced 2025-12-08 04:54:48 +08:00
create(id=None) is valid, if id is AutoField
This commit is contained in:
@@ -478,3 +478,46 @@
|
||||
class MyApp2Config(AppConfig):
|
||||
name = 'myapp2'
|
||||
label = 'myapp2__user'
|
||||
|
||||
|
||||
- case: related_field_to_extracted_from_function
|
||||
main: |
|
||||
from myapp.models import Profile
|
||||
reveal_type(Profile().user) # N: Revealed type is 'myapp.models.User*'
|
||||
installed_apps:
|
||||
- myapp
|
||||
files:
|
||||
- path: myapp/__init__.py
|
||||
- path: myapp/models.py
|
||||
content: |
|
||||
from django.db import models
|
||||
class User(models.Model):
|
||||
pass
|
||||
def get_user_model_name():
|
||||
return 'myapp.User'
|
||||
class Profile(models.Model):
|
||||
user = models.ForeignKey(to=get_user_model_name(), on_delete=models.CASCADE)
|
||||
|
||||
|
||||
- case: related_manager_name_defined_by_pattern
|
||||
main: |
|
||||
from myapp.models import Publisher
|
||||
reveal_type(Publisher().books) # N: Revealed type is 'django.db.models.manager.RelatedManager[myapp.models.Book]'
|
||||
reveal_type(Publisher().articles) # N: Revealed type is 'django.db.models.manager.RelatedManager[myapp.models.Article]'
|
||||
installed_apps:
|
||||
- myapp
|
||||
files:
|
||||
- path: myapp/__init__.py
|
||||
- path: myapp/models.py
|
||||
content: |
|
||||
from django.db import models
|
||||
class Publisher(models.Model):
|
||||
pass
|
||||
class Entry(models.Model):
|
||||
class Meta:
|
||||
abstract = True
|
||||
publisher = models.ForeignKey(to=Publisher, related_name='%(class)ss', on_delete=models.CASCADE)
|
||||
class Book(Entry):
|
||||
pass
|
||||
class Article(Entry):
|
||||
pass
|
||||
@@ -55,10 +55,11 @@
|
||||
class Child4(Child1):
|
||||
value4 = models.IntegerField()
|
||||
|
||||
- case: optional_id_fields_for_create_is_error
|
||||
- case: optional_id_fields_for_create_is_error_if_not_autofield
|
||||
main: |
|
||||
from myapp.models import Publisher, Book
|
||||
Book.objects.create(id=None) # E: Incompatible type for "id" of "Book" (got "None", expected "Union[Combinable, int, str]")
|
||||
|
||||
Book.objects.create(id=None) # E: Incompatible type for "id" of "Book" (got "None", expected "Union[float, int, str, Combinable]")
|
||||
Book.objects.create(publisher=None) # E: Incompatible type for "publisher" of "Book" (got "None", expected "Union[Publisher, Combinable]")
|
||||
Book.objects.create(publisher_id=None) # E: Incompatible type for "publisher_id" of "Book" (got "None", expected "Union[Combinable, int, str]")
|
||||
installed_apps:
|
||||
@@ -71,8 +72,23 @@
|
||||
class Publisher(models.Model):
|
||||
pass
|
||||
class Book(models.Model):
|
||||
id = models.IntegerField(primary_key=True)
|
||||
publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE)
|
||||
|
||||
- case: none_for_primary_key_is_allowed_if_field_is_autogenerated
|
||||
main: |
|
||||
from myapp.models import Book
|
||||
Book.objects.create(id=None)
|
||||
installed_apps:
|
||||
- myapp
|
||||
files:
|
||||
- path: myapp/__init__.py
|
||||
- path: myapp/models.py
|
||||
content: |
|
||||
from django.db import models
|
||||
class Book(models.Model):
|
||||
pass
|
||||
|
||||
- case: when_default_for_primary_key_is_specified_allow_none_to_be_set
|
||||
main: |
|
||||
from myapp.models import MyModel
|
||||
|
||||
Reference in New Issue
Block a user