diff options
Diffstat (limited to 'ishtar_common/wizards.py')
-rw-r--r-- | ishtar_common/wizards.py | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/ishtar_common/wizards.py b/ishtar_common/wizards.py index 8fe39b054..2fbe30e0e 100644 --- a/ishtar_common/wizards.py +++ b/ishtar_common/wizards.py @@ -583,6 +583,12 @@ class Wizard(NamedUrlWizardView): old_m2ms = {} for model in whole_associated_models: related_model = getattr(obj, model + 's') + # manage through + if hasattr(related_model, 'through') and related_model.through: + related_set_name = str( + related_model.through.__name__ + '_set').lower() + if hasattr(obj, related_set_name): + related_model = getattr(obj, related_set_name) # clear real m2m if hasattr(related_model, 'clear'): old_m2ms[model] = [] @@ -609,6 +615,9 @@ class Wizard(NamedUrlWizardView): if value not in m2m_items[key]: if type(value) == dict: model = related_model.model + if hasattr(related_model, 'through') and \ + related_model.through: + model = related_model.through # not m2m -> foreign key if not hasattr(related_model, 'clear'): assert hasattr(model, 'MAIN_ATTR'), \ @@ -633,6 +642,11 @@ class Wizard(NamedUrlWizardView): else: if issubclass(model, models.BaseHistorizedItem): value['history_modifier'] = self.request.user + if hasattr(model, 'RELATIVE_MODELS') and \ + self.get_saved_model() in \ + model.RELATIVE_MODELS: + value[model.RELATIVE_MODELS[ + self.get_saved_model()]] = obj value = model.objects.create(**value) value.save() # check that an item is not add multiple times (forged forms) @@ -1003,8 +1017,18 @@ class Wizard(NamedUrlWizardView): if not hasattr(obj, key): return initial keys = c_form.form.base_fields.keys() - query = getattr(obj, key) - if not obj._meta.ordering: + related = getattr(obj, key) + # manage through + through = False + if hasattr(related, 'through') and related.through: + related_set_name = str( + related.through.__name__ + '_set').lower() + if hasattr(obj, related_set_name): + through = True + related = getattr(obj, related_set_name) + + query = related + if not through and not obj._meta.ordering: query = query.order_by('pk') for child_obj in query.all(): if not keys: |