Changes between Initial Version and Version 1 of Ticket #24804, comment 1
- Timestamp:
- May 15, 2015, 1:43:39 AM (10 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Ticket #24804, comment 1
initial v1 5 5 When a model class is built its defined attributes such as fields are not necessarily ordered the way they are defined; they are collected in an non-ordered mapping (`dict`) and thus the declared ordering is lost. 6 6 7 In your example you're assuming the `auto` field will be added after the `name` o en because the former is declared before. What's happening here is that `auto` is ordered before `name` in `attrs` and thus its `contribute_to_class` [https://github.com/django/django/blob/dce004ab72e26103a86e98994de7f2cef9321f9b/django/db/models/base.py#L156-L158 is called before].7 In your example you're assuming the `auto` field will be added after the `name` one because the latter is declared before. What's happening here is that `auto` is ordered before `name` in `attrs` and thus its `contribute_to_class` [https://github.com/django/django/blob/dce004ab72e26103a86e98994de7f2cef9321f9b/django/db/models/base.py#L156-L158 is called before]. 8 8 9 9 You shouldn't assume that `dict` will be ordered in any way. For example, if you change the seed used to define a an order over unordered collection (`python -R`) you should be able to make the following example print either an empty tuple or a singleton containing the `name` field: