diff options
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/composite_pk/models/tenant.py | 10 | ||||
| -rw-r--r-- | tests/composite_pk/tests.py | 8 |
2 files changed, 14 insertions, 4 deletions
diff --git a/tests/composite_pk/models/tenant.py b/tests/composite_pk/models/tenant.py index c85869afa7..65eb0feae8 100644 --- a/tests/composite_pk/models/tenant.py +++ b/tests/composite_pk/models/tenant.py @@ -14,17 +14,18 @@ class Token(models.Model): secret = models.CharField(max_length=10, default="", blank=True) -class BaseModel(models.Model): +class AbstractUser(models.Model): pk = models.CompositePrimaryKey("tenant_id", "id") tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE) + email = models.EmailField(unique=True) id = models.SmallIntegerField(unique=True) class Meta: abstract = True -class User(BaseModel): - email = models.EmailField(unique=True) +class User(AbstractUser): + pass class Comment(models.Model): @@ -35,13 +36,14 @@ class Comment(models.Model): related_name="comments", ) id = models.SmallIntegerField(unique=True, db_column="comment_id") - user_id = models.SmallIntegerField() + user_id = models.SmallIntegerField(null=True) user = models.ForeignObject( User, on_delete=models.CASCADE, from_fields=("tenant_id", "user_id"), to_fields=("tenant_id", "id"), related_name="comments", + null=True, ) text = models.TextField(default="", blank=True) integer = models.IntegerField(default=0) diff --git a/tests/composite_pk/tests.py b/tests/composite_pk/tests.py index 5dea23c9f2..91cbee0635 100644 --- a/tests/composite_pk/tests.py +++ b/tests/composite_pk/tests.py @@ -184,6 +184,14 @@ class CompositePKTests(TestCase): with self.assertNumQueries(1): self.assertEqual(user.email, self.user.email) + def test_select_related(self): + Comment.objects.create(tenant=self.tenant, id=2) + with self.assertNumQueries(1): + comments = list(Comment.objects.select_related("user").order_by("pk")) + self.assertEqual(len(comments), 2) + self.assertEqual(comments[0].user, self.user) + self.assertIsNone(comments[1].user) + def test_model_forms(self): fields = ["tenant", "id", "user_id", "text", "integer"] self.assertEqual(list(CommentForm.base_fields), fields) |
