Cannot dynamically add fields alongside removal. #8528
Unanswered
mjsir911
asked this question in
Potential Issue
Replies: 1 comment 2 replies
-
I don't think I would trust that setting I haven't looked into it, but it's certainly not intentional usage. Personally if I really needed to do this I'd probably just change my outlook to be okay with this as a constraint. class TestSerializer(ModelSerializer):
generic_test = IntegerField() # Note that we dynamically update this field in `__init__`
def __init__(self, *args, direction, **kwargs):
super().__init__(*args, **kwargs)
self.fields['generic_test'] = IntegerField(source=f"{direction}_test")
class Meta:
fields = [ "generic_test" ] this "repeats unnecessary code" but does "signal properly that the initial class attribute is going to always be overwritten." Not perfect. But perfectly okay. 😌 |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi,
per the dynamically modifying fields, I would like to modify the
source
attribute of a field at runtime via__init__
Obviously I can't modify a class attribute for a single instance I want modified, so I move the attribute outside of class initialization:
but this errors out due to the field not being a class attribute at class-definition-time.
The current workaround:
this repeats unnecessary code and doesn't signal properly that the initial class attribute is going to always be overwritten.
Is the documentation's "Dynamically modifying fields" only for filtering out subsets of fields? There's no real way to modify or add fields in
__init__
currently.Beta Was this translation helpful? Give feedback.
All reactions