Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

managers.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. """
  2. The manager class for use in the models.
  3. """
  4. from django.db import models
  5. from polymorphic.query import PolymorphicQuerySet
  6. __all__ = ("PolymorphicManager", "PolymorphicQuerySet")
  7. class PolymorphicManager(models.Manager):
  8. """
  9. Manager for PolymorphicModel
  10. Usually not explicitly needed, except if a custom manager or
  11. a custom queryset class is to be used.
  12. """
  13. queryset_class = PolymorphicQuerySet
  14. @classmethod
  15. def from_queryset(cls, queryset_class, class_name=None):
  16. manager = super().from_queryset(queryset_class, class_name=class_name)
  17. # also set our version, Django uses _queryset_class
  18. manager.queryset_class = queryset_class
  19. return manager
  20. def get_queryset(self):
  21. qs = self.queryset_class(self.model, using=self._db, hints=self._hints)
  22. if self.model._meta.proxy:
  23. qs = qs.instance_of(self.model)
  24. return qs
  25. def __str__(self):
  26. return "{} (PolymorphicManager) using {}".format(
  27. self.__class__.__name__,
  28. self.queryset_class.__name__,
  29. )
  30. # Proxied methods
  31. def non_polymorphic(self):
  32. return self.all().non_polymorphic()
  33. def instance_of(self, *args):
  34. return self.all().instance_of(*args)
  35. def not_instance_of(self, *args):
  36. return self.all().not_instance_of(*args)
  37. def get_real_instances(self, base_result_objects=None):
  38. return self.all().get_real_instances(base_result_objects=base_result_objects)