File: forms.py

package info (click to toggle)
python-django-tree-queries 0.20-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 388 kB
  • sloc: python: 2,269; makefile: 26; sh: 6
file content (24 lines) | stat: -rw-r--r-- 720 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from django import forms


class TreeNodeIndentedLabels:
    def __init__(self, queryset, *args, **kwargs):
        if hasattr(queryset, "with_tree_fields"):
            queryset = queryset.with_tree_fields()
        if "label_from_instance" in kwargs:
            self.label_from_instance = kwargs.pop("label_from_instance")
        super().__init__(queryset, *args, **kwargs)

    def label_from_instance(self, obj):
        depth = getattr(obj, "tree_depth", 0)
        return "{}{}".format("".join(["--- "] * depth), obj)


class TreeNodeChoiceField(TreeNodeIndentedLabels, forms.ModelChoiceField):
    pass


class TreeNodeMultipleChoiceField(
    TreeNodeIndentedLabels, forms.ModelMultipleChoiceField
):
    pass