-
-
Notifications
You must be signed in to change notification settings - Fork 610
Expand file tree
/
Copy pathtest_orm_relations.py
More file actions
69 lines (50 loc) 路 2.03 KB
/
Copy pathtest_orm_relations.py
File metadata and controls
69 lines (50 loc) 路 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from django.db import models
from django.test.utils import isolate_apps
from ninja import NinjaAPI
from ninja.orm import create_schema
from ninja.testing import TestClient
def test_manytomany():
class SomeRelated(models.Model):
f = models.CharField()
class Meta:
app_label = "tests"
class ModelWithM2M(models.Model):
m2m = models.ManyToManyField(SomeRelated, blank=True)
class Meta:
app_label = "tests"
WithM2MSchema = create_schema(ModelWithM2M, exclude=["id"])
api = NinjaAPI()
@api.post("/bar")
def post_with_m2m(request, payload: WithM2MSchema):
return payload.dict()
client = TestClient(api)
response = client.post("/bar", json={"m2m": [1, 2]})
assert response.status_code == 200, str(response.json())
assert response.json() == {"m2m": [1, 2]}
response = client.post("/bar", json={"m2m": []})
assert response.status_code == 200, str(response.json())
assert response.json() == {"m2m": []}
@isolate_apps("tests")
def test_reverse_foreign_object_relation_is_skipped():
"""A ForeignObject's reverse accessor is a bare ForeignObjectRel. Building a
schema for the referenced model must skip it like any other reverse relation
instead of crashing on the missing ``help_text`` attribute (see #1530)."""
class Order(models.Model):
class Meta:
app_label = "tests"
class OrderDetail(models.Model):
order_id = models.PositiveIntegerField()
order = models.ForeignObject(
Order,
on_delete=models.CASCADE,
from_fields=["order_id"],
to_fields=["id"],
related_name="details",
)
class Meta:
app_label = "tests"
# Order gets a reverse ForeignObjectRel "details"; schema generation must not raise.
OrderSchema = create_schema(Order)
# The reverse relation is skipped, so it does not appear as a schema field.
assert "details" not in OrderSchema.model_fields
assert "id" in OrderSchema.model_fields