3

I was just wondering if the order of items in a GET or POST dictionary ever changes?

if you have a list of 3 people, and you say :

template:

 for x in listof3people:
    <input type="hidden" name="x.name">
    <input type="number" name="birthday">

in views when you do a getlist(name), the order of the information never changes correct?

so if the query pulled up mark , mindy, and sam in that order, the get list would show always show:

mark and his birthday , mindy and her birthday, and sam and his birthday in that order. The reason I'm askiong is because I'm going to have to link information based on position, so find the position of "bar" and pull the data in the nest list that is in the same position.

user3806832
  • 663
  • 9
  • 22

2 Answers2

1

Behind the scenes, the QueryDict class used for GET and POST is built from the output of six.moves.urllib.parse.parse_qsl. This is based on the standard urllib.parse_qsl function, which you can see from the source code does maintain the order given in the URL:

https://github.com/python/cpython/blob/a54346b3a1232cdd503abc4d4e9e526ba65b26b3/Lib/urllib/parse.py

Such inputs are supposed to be put into the URL/encoded form data in the same order they appear in the document: Do browsers preserve order of inputs with same name on GET/POST?

Note that I am talking only about multiple values for the same key, fetched using getlist. QueryDict does not guarantee the order of dict keys any more than any other dict-derived class.

So it's possible, but personally I'd pass around an unambiguous identifier rather than rely on it.

Community
  • 1
  • 1
Peter DeGlopper
  • 36,326
  • 7
  • 90
  • 83
  • I wouldnt mind doing that but how would I go about passing around the identifier? – user3806832 Sep 16 '14 at 22:00
  • It depends on exactly what you're doing, but generally you'd want to either have another hidden input for `x.pk` and/or vary the name of the fields. Formsets do both, providing input like `id0=5&name0=mark&id1=6&name1=mindy`. – Peter DeGlopper Sep 16 '14 at 22:10
  • so add X.pk and when i do a getlist for the three inputs, call the remaining(name and birthday) information based on the position of x.pk, am i understanding it correctly? – user3806832 Sep 16 '14 at 22:44
  • That would be one way, if you pass in multiple values for each input. I don't know enough about what you're doing to give advice that I feel really confident in. – Peter DeGlopper Sep 17 '14 at 00:11
  • what im doing is a scoring app. there a several judges and each judges several people at the same time. so i have a form that shows up with name and score (name is unchangeable, and is pulled from db) I then take the pk and name and score (score that the judges put in) and send it over to be saved. this needs to create a new record in the db with pk,name and score . on every form there is a list of pk, list of names and list of scores hence the request.POST.getlist(). I couldn't find any other way to link the score with the pk, name and judge score other than position any other suggestions? – user3806832 Sep 17 '14 at 04:09
  • You probably don't actually want to store the person's name in the new db record, that's a normalization violation. Aside from that, I would probably use a formset for that case but it's kind of complicated to set one up that's pre-initialized to a specific set of people and not allow changing which person the formset row covers. Totally doable but outside what I can show in a comment. An approach that uses less of the built-in Django machinery would be to create multiple forms in your view, give them a numbered prefix and a `ModelChoiceField` with `widget=HiddenInput()` to track the person. – Peter DeGlopper Sep 17 '14 at 04:44
0

I think you are talking about a QueryDict. This is a customized Python dictionary. Normally there is no guarantee that the keys appear always in the order they are pushed in. So for your purpose you might use some extra weight values in your form so you can order the incoming data according to these values.

Norman8054
  • 804
  • 7
  • 15