Commit 18a9aaa
committed
[sqlalchemy] allow create/update with object for one/many 2 many
For one-2-many and many-2-many relationships, allow the create and
update routes to accept a partial object in the foreign key attribute.
For example:
client.post("/heros", json={
"name": Bob,
"team": {"name": "Avengers"}
}
Assuming there is already a team called Avengers, Bob will be
created, the Team with name "Avengers" will be looked up
and used to populate Bob's team_id foreign key attribute.
The only setup required is for the input model for the
foreign object to specify the Table class that can be used
to lookup the object.
For example:
class Team(Base):
"""Team DTO."""
__tablename__ = "teams"
id = Column(Integer, primary_key=True, index=True)
name = Column(String, unique=True)
class TeamUpdate(Model):
name: str
class Meta:
orm_model = Team1 parent 66fd32f commit 18a9aaa
File tree
2 files changed
+362
-6
lines changed- fastapi_crudrouter/core
- tests
2 files changed
+362
-6
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
| |||
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| 13 | + | |
12 | 14 | | |
13 | 15 | | |
14 | 16 | | |
15 | 17 | | |
| 18 | + | |
16 | 19 | | |
17 | 20 | | |
18 | 21 | | |
| |||
39 | 42 | | |
40 | 43 | | |
41 | 44 | | |
42 | | - | |
| 45 | + | |
43 | 46 | | |
44 | 47 | | |
45 | 48 | | |
| |||
63 | 66 | | |
64 | 67 | | |
65 | 68 | | |
66 | | - | |
| 69 | + | |
67 | 70 | | |
68 | 71 | | |
69 | 72 | | |
| |||
97 | 100 | | |
98 | 101 | | |
99 | 102 | | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
100 | 134 | | |
101 | 135 | | |
102 | 136 | | |
103 | 137 | | |
104 | 138 | | |
105 | 139 | | |
106 | | - | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
107 | 146 | | |
108 | 147 | | |
109 | 148 | | |
| |||
123 | 162 | | |
124 | 163 | | |
125 | 164 | | |
126 | | - | |
127 | | - | |
128 | | - | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
129 | 171 | | |
130 | 172 | | |
131 | 173 | | |
| |||
0 commit comments