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
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 | |
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
136 137 | |
get_column_type
¶
get_column_type()
Source code in saffier/core/db/fields/base.py
139 140 | |
get_constraints
¶
get_constraints()
Source code in saffier/core/db/fields/base.py
142 143 | |
get_columns_nullable
¶
get_columns_nullable()
Resolve SQLAlchemy nullability for this field during metadata build.
The model declaration remains the canonical field definition, but migration generation can temporarily ask Saffier to render selected columns as nullable. That temporary override makes it possible to add a required field to a table that already has rows, then backfill defaults in the generated online migration before the final model constraint is enforced by later migrations.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
TYPE:
|
bool
|
override should make generated columns nullable. |
Source code in saffier/core/db/fields/base.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | |
get_columns
¶
get_columns(name)
Source code in saffier/core/db/fields/base.py
166 167 | |
get_global_constraints
¶
get_global_constraints(name, columns, *, schema=None)
Source code in saffier/core/db/fields/base.py
169 170 171 172 173 174 175 176 177 | |
has_column
¶
has_column()
Source code in saffier/core/db/fields/base.py
179 180 | |
get_embedded_fields
¶
get_embedded_fields(field_name, existing_fields)
Source code in saffier/core/db/fields/base.py
182 183 184 185 186 187 188 | |
expand_relationship
¶
expand_relationship(value)
Source code in saffier/core/db/fields/base.py
190 191 | |
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
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
modify_input
¶
modify_input(name, kwargs)
Source code in saffier/core/db/fields/base.py
212 213 | |
raise_for_non_default
¶
raise_for_non_default(default, server_default)
Source code in saffier/core/db/fields/base.py
215 216 | |
get_default_value
¶
get_default_value()
Source code in saffier/core/db/fields/base.py
218 219 | |
has_default
¶
has_default()
Source code in saffier/core/db/fields/base.py
221 222 | |
get_default_values
¶
get_default_values(field_name, cleaned_data)
Source code in saffier/core/db/fields/base.py
224 225 226 227 228 229 230 231 | |
is_required
¶
is_required()
Source code in saffier/core/db/fields/base.py
233 234 235 236 | |
get_is_null_clause
¶
get_is_null_clause(column)
Source code in saffier/core/db/fields/base.py
238 239 | |
get_is_empty_clause
¶
get_is_empty_clause(column)
Source code in saffier/core/db/fields/base.py
241 242 | |
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
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | |
get_column
¶
get_column(name)
Source code in saffier/core/db/fields/base.py
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 | |
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
1277 1278 1279 1280 1281 1282 1283 | |
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
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 | |
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
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 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 | |