Save the token instance. If no key is provided, generates a cryptographically secure key. For new tokens, ensures they are inserted as new (not updated).
(self, *args, **kwargs)
| 27 | verbose_name_plural = _("Tokens") |
| 28 | |
| 29 | def save(self, *args, **kwargs): |
| 30 | """ |
| 31 | Save the token instance. |
| 32 | |
| 33 | If no key is provided, generates a cryptographically secure key. |
| 34 | For new tokens, ensures they are inserted as new (not updated). |
| 35 | """ |
| 36 | if not self.key: |
| 37 | self.key = self.generate_key() |
| 38 | # For new objects, force INSERT to prevent overwriting existing tokens |
| 39 | if self._state.adding: |
| 40 | kwargs['force_insert'] = True |
| 41 | return super().save(*args, **kwargs) |
| 42 | |
| 43 | @classmethod |
| 44 | def generate_key(cls): |