Use .set() when assigning multiple relations.
Review Request #11989 — Created Jan. 24, 2022 and submitted
Using the assignment operator for setting the far side of a relation
such asManyToManyField
orForeignKey
now generates an error
instructing us to use.set()
instead.In many cases, we were using
.set()
to set a list of relations on
newly-created objects within tests. In these cases, we can save one
query per use by using.add()
instead.
Ran unit tests.
Summary | ID |
---|---|
4865f784cdf233789f9e9ccc96528bf589b09ac6 |
- Commits:
-
Summary ID a06e10626df204c34ea172234458e8355dea536e d830b7b3ea93f8daa4a7b54dea5973ab4ebaeafa - Diff:
-
Revision 2 (+334 -280)
Checks run (2 succeeded)
-
-
Purely optional work (because I'm sure you're really hoping for more things to do), but something that's stood out for a long time (and this would feel like the right time to fix it) is that our unit tests are using
=
(or now.set()
) when the probably shouldn't.=/set()
will first delete all entries on the relation and then replace them. This is one more SQL query than we usually need in tests. We could (ever so slightly) speed up the test suite by using.add()
instead in most cases.But I'm also fine not worrying about it at all for this change. Mostly putting that out there and on the radar.
- Description:
-
Using the assignment operator for setting the far side of a relation
such as ManyToManyField
orForeignKey
now generates an error~ instructing us to use .set()
instead. This is a relatively~ straightforward update. ~ instructing us to use .set()
instead.~ + In many cases, we were using
.set()
to set a list of relations on+ newly-created objects within tests. In these cases, we can save one + query per use by using .add()
instead. - Commits:
-
Summary ID d830b7b3ea93f8daa4a7b54dea5973ab4ebaeafa 4865f784cdf233789f9e9ccc96528bf589b09ac6 - Diff:
-
Revision 3 (+326 -310)