def lock(self):
        # Ensure latest state has come over from the database
        before_lock = self.__class__.objects.get(pk=self.pk)
        self.locked = before_lock.locked
        self.locked_at = before_lock.locked_at

        if self.locked:
            # Already locked
            raise DatasetLockedError(_('This dataset is currently locked by another process.'))

        new_locked_at = now()

        self.locked = True
        self.locked_at = new_locked_at

        self.save()

        # Refresh from database
        after_lock = Dataset.objects.get(id=self.id)
        self.locked = after_lock.locked
        self.locked_at = after_lock.locked_at

        if self.locked_at != new_locked_at:
            # Somebody else got the lock
            raise DatasetLockedError(_('This dataset is currently locked by another process.'))
Obtain an editing lock on this dataset.
dataset/ETHPy150Open pandaproject/panda/panda/models/dataset.py/Dataset.lock