#28866: merge method of class Media doesn't work properly
---------------------------------+--------------------------------------
Reporter: clincher | Owner: nobody
Type: Bug | Status: new
Component: Forms | Version: 2.0
Severity: Release blocker | Resolution:
Keywords: forms, media | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
---------------------------------+--------------------------------------
Comment (by Johannes Hoppe):
Hi @clincher,
let me try to go though you points one by one:
This
{{{
for item in list_2:
if item not in list_1:
list_1.append(item)
}}}
does not work. It avoids duplicates but does not preserve the order. This
was the whole point of #28377
This I don't really understand, but it does not preserve the order ether.
You will have to go though it reversed.
{{{
order_list = []
for item in list_2:
try:
order_list.append(list_1.index(item))
except ValueError:
pass
if order_list is not sort(order_list):
OrderWarning(...)
}}}
Your example
{{{
list_1= [path1, path2, path3]
list_2 = [path4, path2, path5]
}}}
does work, as such:
{{{
>>> list_1= ['path1', 'path2', 'path3']
>>> list_2 = ['path4', 'path2', 'path5']
>>> from django.forms.widgets import Media
>>> Media.merge(list_1, list_2)
['path1', 'path4', 'path2', 'path3', 'path5']
}}}
It does preserve the order of both initial lists.
I know this is a difficult topic and the algorithm isn't easy to
understand, but to my knowledge it's the only way to achieve both
uniqueness and order.
The warning is just so that you are alerted that your widget setup might
now work, which it doesn't in your case. I would consider this a good
thing, because it alerted you to the issue of sorting.
--
Ticket URL: <https://code.djangoproject.com/ticket/28866#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.7a31f5a912ad2d58afd59b5dbd9e5431%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.