fix get parameters lost on create and add another

- add get_clone_fields to get clone fields get parameters as QueryDict
- change prepare_clone_fields to use get_clone_fields and build
querystring with urlencode function instead of manual building.
- add non clone_field GET-parameters to params QueryDict on
ObjectEditView.post()

fix #4629
This commit is contained in:
Julian Jacobi 2020-05-12 10:48:02 +02:00
parent 41361ce2a2
commit 60f0e057d7
2 changed files with 18 additions and 9 deletions

View File

@ -195,12 +195,12 @@ def render_jinja2(template_code, context):
return Environment().from_string(source=template_code).render(**context)
def prepare_cloned_fields(instance):
def get_cloned_fields(instance):
"""
Compile an object's `clone_fields` list into a string of URL query parameters. Tags are automatically cloned where
Compile an object's `clone_fields` list into a QueryDict. Tags are automatically cloned where
applicable.
"""
params = {}
params = QueryDict(mutable=True)
for field_name in getattr(instance, 'clone_fields', []):
field = instance._meta.get_field(field_name)
field_value = field.value_from_object(instance)
@ -217,12 +217,17 @@ def prepare_cloned_fields(instance):
if is_taggable(instance):
params['tags'] = ','.join([t.name for t in instance.tags.all()])
# Concatenate parameters into a URL query string
param_string = '&'.join(
['{}={}'.format(k, v) for k, v in params.items()]
)
return params
return param_string
def prepare_cloned_fields(instance):
"""
Compile an object's `clone_fields` list into a string of URL query parameters. Tags are automatically cloned where
applicable.
"""
params = get_cloned_fields(instance)
return params.urlencode()
def shallow_compare_dict(source_dict, destination_dict, exclude=None):

View File

@ -283,7 +283,11 @@ class ObjectEditView(GetReturnURLMixin, View):
# If the object has clone_fields, pre-populate a new instance of the form
if hasattr(obj, 'clone_fields'):
url = '{}?{}'.format(request.path, prepare_cloned_fields(obj))
params = get_cloned_fields(obj)
for field, value in request.GET.items():
if field not in params:
params[field] = value
url = '{}?{}'.format(request.path, params.urlencode())
return redirect(url)
return redirect(request.get_full_path())