ManyToManyField¶
ManyToManyField is a virtual field backed by a through model.
It does not create columns on the owning model. Instead it either uses an explicit through model or generates one automatically and exposes a runtime relation descriptor for collection-style access.
Practical example¶
class Article(saffier.Model):
id = saffier.IntegerField(primary_key=True, autoincrement=True)
tags = saffier.ManyToManyField("Tag")
class Meta:
registry = models
Important behaviors¶
- the through model must expose an integer
idprimary key add(),add_many(),create(),remove(), andremove_many()operate through the junction modelembed_throughcan attach the through instance back onto the related object after relation operations- reverse names are generated from the owning model and field name when you do not declare one explicitly
saffier.ManyToManyField
¶
ManyToManyField(
to,
through=None,
through_tablename=NEW_M2M_NAMING,
embed_through=False,
to_foreign_key="",
from_foreign_key="",
**kwargs,
)
Bases: Field
Virtual field describing a many-to-many relation via a through model.
A many-to-many field never maps directly to columns on the owning model.
Instead it manages or creates an intermediate through model, exposes a
Relation descriptor for runtime access, and carries the metadata needed to
build reverse relation names, embedded through objects, and auto-generated
junction table definitions.
Source code in saffier/core/db/fields/base.py
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 | |
inject_default_on_partial_update
instance-attribute
¶
inject_default_on_partial_update = get(
"inject_default_on_partial_update", False
)
target
property
¶
target
Resolve and cache the many-to-many target model class.
| RETURNS | DESCRIPTION |
|---|---|
Any
|
typing.Any: Target model class for the relation. |
get_validator
¶
get_validator(**kwargs)
Source code in saffier/core/db/fields/base.py
127 128 | |
get_column_type
¶
get_column_type()
Source code in saffier/core/db/fields/base.py
130 131 | |
get_constraints
¶
get_constraints()
Source code in saffier/core/db/fields/base.py
133 134 | |
get_columns
¶
get_columns(name)
Source code in saffier/core/db/fields/base.py
136 137 | |
get_global_constraints
¶
get_global_constraints(name, columns, *, schema=None)
Source code in saffier/core/db/fields/base.py
139 140 141 142 143 144 145 146 147 | |
has_column
¶
has_column()
Source code in saffier/core/db/fields/base.py
149 150 | |
get_embedded_fields
¶
get_embedded_fields(field_name, existing_fields)
Source code in saffier/core/db/fields/base.py
152 153 154 155 156 157 158 | |
expand_relationship
¶
expand_relationship(value)
Source code in saffier/core/db/fields/base.py
160 161 | |
clean
¶
clean(name, value, *, for_query=False)
Normalize one logical field value into column-value pairs.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Logical field name.
TYPE:
|
value
|
User-facing field value.
TYPE:
|
for_query
|
Reserved for field implementations that need distinct query-time normalization.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
dict[str, Any]: Database payload keyed by column name. |
Source code in saffier/core/db/fields/base.py
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 | |
modify_input
¶
modify_input(name, kwargs)
Source code in saffier/core/db/fields/base.py
182 183 | |
raise_for_non_default
¶
raise_for_non_default(default, server_default)
Source code in saffier/core/db/fields/base.py
185 186 | |
get_default_value
¶
get_default_value()
Source code in saffier/core/db/fields/base.py
188 189 | |
has_default
¶
has_default()
Source code in saffier/core/db/fields/base.py
191 192 | |
get_default_values
¶
get_default_values(field_name, cleaned_data)
Source code in saffier/core/db/fields/base.py
194 195 196 197 198 199 200 201 | |
is_required
¶
is_required()
Source code in saffier/core/db/fields/base.py
203 204 205 206 | |
get_is_null_clause
¶
get_is_null_clause(column)
Source code in saffier/core/db/fields/base.py
208 209 | |
get_is_empty_clause
¶
get_is_empty_clause(column)
Source code in saffier/core/db/fields/base.py
211 212 | |
operator_to_clause
¶
operator_to_clause(field_name, operator, table, value)
Translate one lookup operator into a SQLAlchemy clause.
| PARAMETER | DESCRIPTION |
|---|---|
field_name
|
Logical field name being filtered.
TYPE:
|
operator
|
Saffier lookup suffix such as
TYPE:
|
table
|
SQLAlchemy table containing the target column.
TYPE:
|
value
|
Lookup value supplied by the caller.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Any
|
SQLAlchemy boolean clause implementing the lookup.
TYPE:
|
Source code in saffier/core/db/fields/base.py
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
get_column
¶
get_column(name)
Source code in saffier/core/db/fields/base.py
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 | |
add_model_to_register
¶
add_model_to_register(model)
Register an auto-generated through model on the owning registry.
| PARAMETER | DESCRIPTION |
|---|---|
model
|
Through model created for the relation.
TYPE:
|
Source code in saffier/core/db/fields/base.py
1241 1242 1243 1244 1245 1246 1247 | |
get_fk_name
¶
get_fk_name(name)
Build a stable FK constraint name for the through table.
| PARAMETER | DESCRIPTION |
|---|---|
name
|
Local field or column name participating in the constraint.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
str
|
Constraint name truncated to common identifier limits.
TYPE:
|
Source code in saffier/core/db/fields/base.py
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 | |
create_through_model
¶
create_through_model()
Create or normalize the through model used to persist the relation.
| RETURNS | DESCRIPTION |
|---|---|
Any
|
typing.Any: Concrete through model class used by the relation. |
| RAISES | DESCRIPTION |
|---|---|
ImproperlyConfigured
|
If an explicit through model does not expose a
valid integer |
Source code in saffier/core/db/fields/base.py
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 | |