Changes between Initial Version and Version 1 of Ticket #28344, comment 31


Ignore:
Timestamp:
Dec 11, 2023, 10:17:19 AM (12 months ago)
Author:
Simon Charette

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #28344, comment 31

    initial v1  
    1414{{{#!python
    1515queryset = Author.objects.all()  # As opposed to `_base_manager` the default manager could be doing `filter(deleted_at=None)`
    16 author.refresh_from_db(from_queryset=queryset)
     16author.refresh_from_db(from_queryset=queryset)  # Which would respect raising `DoesNotExist`
    1717}}}
     18
     19That would make implementing a model mixin that supports a `select_for_update` override quite easy
     20
     21{{{#!python
     22class BaseModel(models.Model):
     23    class Meta:
     24        abstract = True
     25
     26    def refresh_from_db(self, using=None, fields=None, from_queryset=None, select_for_update=False):
     27        if select_for_update:
     28            from_queryset = (
     29                self.__class__._base_manager.db_manager(using)
     30                if from_queryset is None
     31                else from_queryset
     32            ).select_for_update()
     33        return super().refresh_from_db(fields=fields, from_queryset=from_queryset)
     34}}}
Back to Top