filtering_queryset.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from django import forms
  2. from django.core.exceptions import ObjectDoesNotExist
  3. import logging
  4. logger = logging.getLogger(__name__)
  5. class LimitedAdminInlineMixin(object):
  6. """
  7. InlineAdmin mixin limiting the selection of related items according to
  8. criteria which can depend on the current parent object being edited.
  9. A typical use case would be selecting a subset of related items from
  10. other inlines, ie. images, to have some relation to other inlines.
  11. Use as follows::
  12. class MyInline(LimitedAdminInlineMixin, admin.TabularInline):
  13. def get_filters(self, obj):
  14. return (('<field_name>', dict(<filters>)),)
  15. """
  16. @staticmethod
  17. def limit_inline_choices(formset, field, empty=False, **filters):
  18. """
  19. This function fetches the queryset with available choices for a given
  20. `field` and filters it based on the criteria specified in filters,
  21. unless `empty=True`. In this case, no choices will be made available.
  22. """
  23. try:
  24. assert formset.form.base_fields.has_key(field)
  25. qs = formset.form.base_fields[field].queryset
  26. if empty:
  27. logger.debug('Limiting the queryset to none')
  28. formset.form.base_fields[field].queryset = qs.none()
  29. else:
  30. qs = qs.filter(**filters)
  31. logger.debug('Limiting queryset for formset to: %s', qs)
  32. formset.form.base_fields[field].queryset = qs
  33. except:
  34. pass
  35. def get_formset(self, request, obj=None, **kwargs):
  36. """
  37. Make sure we can only select variations that relate to the current
  38. item.
  39. """
  40. formset = \
  41. super(LimitedAdminInlineMixin, self).get_formset(request,
  42. obj,
  43. **kwargs)
  44. for (field, filters) in self.get_filters(obj):
  45. if obj and filters:
  46. self.limit_inline_choices(formset, field, **filters)
  47. else:
  48. self.limit_inline_choices(formset, field, empty=True)
  49. return formset
  50. def get_filters(self, obj):
  51. """
  52. Return filters for the specified fields. Filters should be in the
  53. following format::
  54. (('field_name', {'categories': obj}), ...)
  55. For this to work, we should either override `get_filters` in a
  56. subclass or define a `filters` property with the same syntax as this
  57. one.
  58. """
  59. return getattr(self, 'filters', ())