|
| 1 | +- case: bulk_update_valid_fields |
| 2 | + installed_apps: |
| 3 | + - myapp |
| 4 | + main: | |
| 5 | + from myapp.models import Article, Author, Category |
| 6 | +
|
| 7 | + # Valid single field updates |
| 8 | + articles = Article.objects.all() |
| 9 | + Article.objects.bulk_update(articles, ["title"]) |
| 10 | + Article.objects.bulk_update(articles, ["content"]) |
| 11 | + Article.objects.bulk_update(articles, ["published"]) |
| 12 | +
|
| 13 | + # Valid multiple field updates |
| 14 | + Article.objects.bulk_update(articles, ["title", "content"]) |
| 15 | + Article.objects.bulk_update(articles, ["title", "content", "published"]) |
| 16 | +
|
| 17 | + # Valid foreign key field updates (by field name and attname) |
| 18 | + Article.objects.bulk_update(articles, ["author"]) |
| 19 | + Article.objects.bulk_update(articles, ["author_id"]) |
| 20 | + Article.objects.bulk_update(articles, ["category"]) |
| 21 | + Article.objects.bulk_update(articles, ["category_id"]) |
| 22 | +
|
| 23 | + # Valid updates on different models |
| 24 | + authors = Author.objects.all() |
| 25 | + Author.objects.bulk_update(authors, ["name"]) |
| 26 | + Author.objects.bulk_update(authors, ["email"]) |
| 27 | +
|
| 28 | + categories = Category.objects.all() |
| 29 | + Category.objects.bulk_update(categories, ["name"]) |
| 30 | + Category.objects.bulk_update(categories, ["parent"]) |
| 31 | + Category.objects.bulk_update(categories, ["parent_id"]) |
| 32 | +
|
| 33 | + # Variables containing field names |
| 34 | + field_name = "title" |
| 35 | + Article.objects.bulk_update(articles, [field_name]) |
| 36 | +
|
| 37 | + # Dynamic field lists |
| 38 | + def get_fields() -> list[str]: |
| 39 | + return ["title"] |
| 40 | +
|
| 41 | + Article.objects.bulk_update(articles, get_fields()) |
| 42 | +
|
| 43 | + files: |
| 44 | + - path: myapp/__init__.py |
| 45 | + - path: myapp/models.py |
| 46 | + content: | |
| 47 | + from django.db import models |
| 48 | +
|
| 49 | + class Category(models.Model): |
| 50 | + name = models.CharField(max_length=100) |
| 51 | + parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True) |
| 52 | +
|
| 53 | + class Author(models.Model): |
| 54 | + name = models.CharField(max_length=100) |
| 55 | + email = models.EmailField() |
| 56 | +
|
| 57 | + class Tag(models.Model): |
| 58 | + name = models.CharField(max_length=50) |
| 59 | +
|
| 60 | + class Article(models.Model): |
| 61 | + title = models.CharField(max_length=200) |
| 62 | + content = models.TextField() |
| 63 | + published = models.BooleanField(default=False) |
| 64 | + author = models.ForeignKey(Author, on_delete=models.CASCADE) |
| 65 | + category = models.ForeignKey(Category, on_delete=models.CASCADE) |
| 66 | + tags = models.ManyToManyField(Tag) |
| 67 | +
|
| 68 | +
|
| 69 | +
|
| 70 | +- case: bulk_update_invalid_fields |
| 71 | + installed_apps: |
| 72 | + - myapp |
| 73 | + main: | |
| 74 | + from myapp.models import Article, Author, Category |
| 75 | + from typing import Literal |
| 76 | +
|
| 77 | + articles = Article.objects.all() |
| 78 | +
|
| 79 | + # Empty fields list |
| 80 | + Article.objects.bulk_update() # E: Missing positional arguments "objs", "fields" in call to "bulk_update" of "Manager" [call-arg] |
| 81 | + Article.objects.bulk_update(articles, []) # E: Field names must be given to bulk_update() [misc] |
| 82 | +
|
| 83 | + # Invalid field names (Django's FieldError) |
| 84 | + Article.objects.bulk_update(articles, ["nonexistent"]) # E: Article has no field named 'nonexistent' [misc] |
| 85 | + Article.objects.bulk_update(articles, ["invalid_field"]) # E: Article has no field named 'invalid_field' [misc] |
| 86 | +
|
| 87 | + # Cannot update primary key fields |
| 88 | + Article.objects.bulk_update(articles, ["id"]) # E: bulk_update() cannot be used with primary key fields. Got "id" [misc] |
| 89 | +
|
| 90 | + # Mixed valid and invalid fields |
| 91 | + Article.objects.bulk_update(articles, ["title", "nonexistent"]) # E: Article has no field named 'nonexistent' [misc] |
| 92 | + Article.objects.bulk_update(articles, ["id", "title"]) # E: bulk_update() cannot be used with primary key fields. Got "id" [misc] |
| 93 | +
|
| 94 | + # Whitespace-only field names |
| 95 | + Article.objects.bulk_update(articles, [""]) # E: Article has no field named '' [misc] |
| 96 | + Article.objects.bulk_update(articles, [" "]) # E: Article has no field named ' ' [misc] |
| 97 | +
|
| 98 | + # ManyToMany is not a concrete updatable field |
| 99 | + Article.objects.bulk_update(articles, ["tags"]) # E: bulk_update() can only be used with concrete fields. Got "tags" [misc] |
| 100 | +
|
| 101 | + # Multiple invalid fields |
| 102 | + Article.objects.bulk_update(articles, ["nonexistent1", "nonexistent2"]) # E: Article has no field named 'nonexistent1' [misc] # E: Article has no field named 'nonexistent2' [misc] |
| 103 | +
|
| 104 | + # Primary key with valid fields |
| 105 | + Article.objects.bulk_update(articles, ["title", "id", "content"]) # E: bulk_update() cannot be used with primary key fields. Got "id" [misc] |
| 106 | +
|
| 107 | + # Literal type variables are validated |
| 108 | + invalid_field: Literal["nonexistent"] = "nonexistent" |
| 109 | + Article.objects.bulk_update(articles, [invalid_field]) # E: Article has no field named 'nonexistent' [misc] |
| 110 | +
|
| 111 | + pk_field: Literal["id"] = "id" |
| 112 | + Article.objects.bulk_update(articles, [pk_field]) # E: bulk_update() cannot be used with primary key fields. Got "id" [misc] |
| 113 | +
|
| 114 | + # Test with different models |
| 115 | + authors = Author.objects.all() |
| 116 | + Author.objects.bulk_update(authors, ["id"]) # E: bulk_update() cannot be used with primary key fields. Got "id" [misc] |
| 117 | + Author.objects.bulk_update(authors, ["invalid"]) # E: Author has no field named 'invalid' [misc] |
| 118 | +
|
| 119 | + categories = Category.objects.all() |
| 120 | + Category.objects.bulk_update(categories, ["id"]) # E: bulk_update() cannot be used with primary key fields. Got "id" [misc] |
| 121 | + Category.objects.bulk_update(categories, ["invalid"]) # E: Category has no field named 'invalid' [misc] |
| 122 | +
|
| 123 | + files: |
| 124 | + - path: myapp/__init__.py |
| 125 | + - path: myapp/models.py |
| 126 | + content: | |
| 127 | + from django.db import models |
| 128 | +
|
| 129 | + class Category(models.Model): |
| 130 | + name = models.CharField(max_length=100) |
| 131 | + parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True) |
| 132 | +
|
| 133 | + class Author(models.Model): |
| 134 | + name = models.CharField(max_length=100) |
| 135 | + email = models.EmailField() |
| 136 | +
|
| 137 | + class Article(models.Model): |
| 138 | + title = models.CharField(max_length=200) |
| 139 | + content = models.TextField() |
| 140 | + published = models.BooleanField(default=False) |
| 141 | + author = models.ForeignKey(Author, on_delete=models.CASCADE) |
| 142 | + category = models.ForeignKey(Category, on_delete=models.CASCADE) |
| 143 | + tags = models.ManyToManyField('myapp.Tag') |
| 144 | +
|
| 145 | + class Tag(models.Model): |
| 146 | + name = models.CharField(max_length=50) |
0 commit comments