diff options
author | Cefin <kevon@tuta.io> | 2021-10-27 14:56:59 +0200 |
---|---|---|
committer | Cefin <kevon@tuta.io> | 2021-10-27 14:56:59 +0200 |
commit | 079f4765a762ff6656fcd8d7019d25eea38f5149 (patch) | |
tree | a2ca2ff9afc965c0be80e8426baaa94f6596be48 | |
parent | bc166dcfe47c021dd1c106b621a9f2206148d3ce (diff) | |
download | Ishtar-079f4765a762ff6656fcd8d7019d25eea38f5149.tar.bz2 Ishtar-079f4765a762ff6656fcd8d7019d25eea38f5149.zip |
can change the parent of hierarchic types
-rw-r--r-- | ishtar_common/admin.py | 35 | ||||
-rw-r--r-- | ishtar_common/templates/admin/change_parent.html | 2 |
2 files changed, 20 insertions, 17 deletions
diff --git a/ishtar_common/admin.py b/ishtar_common/admin.py index 80432caaa..a803c64cc 100644 --- a/ishtar_common/admin.py +++ b/ishtar_common/admin.py @@ -397,28 +397,31 @@ class ChangeParentAdmin: if not request.POST: return HttpResponseRedirect(return_url) - selected = request.POST.getlist("_selected_action", []) - items = {} + selected_children_pk = request.POST.getlist("_selected_action", []) choices = [] - # if hasattr(self.model, "parent"): - parents = self.model.objects.all().select_related("parent") + parents = self.model.objects.all() for obj in parents: - choices.append((obj, str(obj))) - items[str(obj)] = obj + choices.append((obj.pk, str(obj))) form = None if "apply" in request.POST: form = ChangeParentForm(choices, request.POST, request.FILES) if form.is_valid(): - form.save() - messages.add_message( - request, - messages.INFO, - str(_("{} change parent of {}.")).format( - " ; ".join(changes), str(change_parent) - ), - ) - return HttpResponseRedirect(return_url) + change_parent = form.cleaned_data["change_parent"] + change = [] + for child_id in selected_children_pk: + child = self.model.objects.get(pk=child_id) + child.parent_id = change_parent + child.save() + change.append(str(child)) + messages.add_message( + request, + messages.INFO, + str(_("{} parent(s) was change to {}.")).format( + " ; ".join(change), str(self.model.objects.get(pk=change_parent)) + ), + ) + return HttpResponseRedirect(return_url) if not form: form = ChangeParentForm(choices) return render( @@ -427,7 +430,7 @@ class ChangeParentAdmin: { "change_parent_form": form, "current_action": "change_parent_selected", - "selected_item": selected, + "selected_items": selected_children_pk, }, ) diff --git a/ishtar_common/templates/admin/change_parent.html b/ishtar_common/templates/admin/change_parent.html index dab4eb5ba..8d6744cff 100644 --- a/ishtar_common/templates/admin/change_parent.html +++ b/ishtar_common/templates/admin/change_parent.html @@ -11,7 +11,7 @@ {% for selected in selected_items %} <input type="hidden" name="_selected_action" value="{{ selected }}"/> {% endfor %} - <input type="submit" name="apply" value"{% trans 'Change parent' %}"/> + <input type="submit" name="apply" value="{% trans 'Change parent' %}"/> </form> {% endblock %} |