Closed kolod closed 3 years ago
This is normal: pyfields
follows strictly python's behaviour with default
.
I think that in your case you probably wish to use default_factory
as suggested in the doc:
The behaviour with default values is the same than python's default: the same value is used for all objects. Therefore if your default value is a mutable object (e.g. a list) you should not use this mechanism, otherwise the same value will be shared by all instances that use the default
So you can use
field(default_factory=lambda obj: [])
or
field(default_factory=copy_value([]))
Let me know if this works for you.
@kolod did the above answer work for you ? I'm closing the ticket now, please feel free to reopen if this is not the case.
I want to get a list of objects. One of the fields in this object is a list. When adding items to the field of one of the objects. This element is added to the fields of all elements. More precisely, all objects use one copy of the list.
Example test: