mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-29 20:06:25 -06:00
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:
parent
41361ce2a2
commit
60f0e057d7
@ -195,12 +195,12 @@ def render_jinja2(template_code, context):
|
|||||||
return Environment().from_string(source=template_code).render(**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.
|
applicable.
|
||||||
"""
|
"""
|
||||||
params = {}
|
params = QueryDict(mutable=True)
|
||||||
for field_name in getattr(instance, 'clone_fields', []):
|
for field_name in getattr(instance, 'clone_fields', []):
|
||||||
field = instance._meta.get_field(field_name)
|
field = instance._meta.get_field(field_name)
|
||||||
field_value = field.value_from_object(instance)
|
field_value = field.value_from_object(instance)
|
||||||
@ -217,12 +217,17 @@ def prepare_cloned_fields(instance):
|
|||||||
if is_taggable(instance):
|
if is_taggable(instance):
|
||||||
params['tags'] = ','.join([t.name for t in instance.tags.all()])
|
params['tags'] = ','.join([t.name for t in instance.tags.all()])
|
||||||
|
|
||||||
# Concatenate parameters into a URL query string
|
return params
|
||||||
param_string = '&'.join(
|
|
||||||
['{}={}'.format(k, v) for k, v in params.items()]
|
|
||||||
)
|
|
||||||
|
|
||||||
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):
|
def shallow_compare_dict(source_dict, destination_dict, exclude=None):
|
||||||
|
@ -283,7 +283,11 @@ class ObjectEditView(GetReturnURLMixin, View):
|
|||||||
|
|
||||||
# If the object has clone_fields, pre-populate a new instance of the form
|
# If the object has clone_fields, pre-populate a new instance of the form
|
||||||
if hasattr(obj, 'clone_fields'):
|
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(url)
|
||||||
|
|
||||||
return redirect(request.get_full_path())
|
return redirect(request.get_full_path())
|
||||||
|
Loading…
Reference in New Issue
Block a user