Django Python urlencode тот же ключ с несколькими значениямиPython

Программы на Python
Anonymous
Django Python urlencode тот же ключ с несколькими значениями

Сообщение Anonymous »

Как следует из названия, я пытаюсь создать urlencode с использованием Ordered Dict в Python

Код: Выделить всё

def url_replace(request, field, value, direction=""):
dict_ = request.GET.copy()
if field == "order_by" and field in dict_.keys():
if dict_[field].startswith("-") and dict_[field].lstrip("-") == value:
dict_[field] = value
elif dict_[field].lstrip("-") == value:
dict_[field] = "-" + value
else:
dict_[field] = direction + value
else:
dict_[field] = direction + str(value)

print("UNORDERED___________________")
print(dict_)
print(super(MultiValueDict, dict_).items())
print("ORDERED_____________")
print(OrderedDict(super(MultiValueDict, dict_).items()))
print(OrderedDict(dict_.items()))
return urlencode(OrderedDict(dict_.items()))
Вывод приведенного выше кода

Код: Выделить всё

UNORDERED___________________

dict_items([('assigned_to_id', ['1', '2']), ('client_id', ['2', '1']), ('page', ['2'])])
OrderedDict([('assigned_to_id', '2'), ('client_id', '1'), ('page', '2')])
ORDERED_____________
OrderedDict([('assigned_to_id', ['1', '2']), ('client_id', ['2', '1']), ('page', ['2'])])
OrderedDict([('assigned_to_id', '2'), ('client_id', '1'), ('page', '2')])
Как вы можете видеть, назначенный_to_id имеет только 2 в конце.
Я ожидаю упорядоченного словаря с р>

Код: Выделить всё

OrderedDict([('assigned_to_id', '2'),('assigned_to_id', '1'), ('client_id', '1'), ('client_id', '2'),('page', '2')])
Может быть, для этого есть лучший подход, я немного новичок в Python
Моя конечная цель — вернуть словарь с несколькими ключами или что-нибудь, что можно использовать в urlencode

Подробнее здесь: https://stackoverflow.com/questions/705 ... ple-values

Вернуться в «Python»