我认为可以这样解决:
class EncryptedPKQuerySet(models.query.QuerySet):
def filter(self, *args, **kwargs):
encrypted_pk = kwargs.pop('encrypted_pk', None)
if encrypted_pk:
# If found, decrypt encrypted_pk argument and set pk argument to the appropriate value
kwargs['pk'] = struct.unpack('<Q', self.model.encryption_obj.decrypt(
struct.pack('<Q', base36decode(encrypted_pk))
))[0]
print kwargs
return super(EncryptedPKQuerySet, self).filter(*args, **kwargs)
class EncryptedPKModelManager(models.Manager):
def get_query_set(self):
return EncryptedPKQuerySet(self.model, using=self._db)
因为在get_object_or_None
QuerySet 中被构建为model.manager.all().get(...)
并all()
返回 QuerySet 实例。