Created
September 15, 2018 18:27
-
-
Save bebosudo/0c91f943013fa3c2dab86290d62de7b5 to your computer and use it in GitHub Desktop.
Django doesn't provide id in forms or serializers.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Manually provide the id with: | |
# forms.py | |
class SomethingForm(...): | |
id = forms.IntegerField(min_value=0) | |
# serializers.py | |
class SetIDFieldFromModel: | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
model_id_field = self.Meta.model._meta.get_field('id') | |
self.fields['id'] = serializers.ModelField(model_field=model_id_field, | |
required=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment