Skip to content

Add 'default' value in OPTIONS #3773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed

Add 'default' value in OPTIONS #3773

wants to merge 3 commits into from

Conversation

cyberj
Copy link
Contributor

@cyberj cyberj commented Dec 24, 2015

For some frameworks who rely on automatic form generation (Angular, Polymer..) we need to see the default option provided in OPTIONS metadata.

I saw your concerns on Model's default attribute in #2683 so I have not included this on ModelSerializer for now.

But I think DRF shoul'd pass the model's default attribute in the rest_framework.field.Field when using a ModelSerializer to let thoses frameworks know what will be the value if not provided.

@xordoquy
Copy link
Collaborator

Hi, thanks for the PR.
Is it possible to include a test with a CurrentUserDefault to make sure your change won't break this sort of default ?

@Ernest0x
Copy link
Contributor

@cyberj I had the same need so I created pull request #3750. I think the 'initial' field is exactly what frontends should take into account for form building. The 'default' field is for setting a value in the backend side if no input has been given.

@cyberj
Copy link
Contributor Author

cyberj commented Dec 27, 2015

@Ernest0x So i case of a ModelSerializer we have to manually provide an initial attribute for each Fields? I can understand the logic, and Django use the same with forms, but it can save time to grab the Model's default to populate the initial attribute.

@cyberj
Copy link
Contributor Author

cyberj commented Dec 28, 2015

Yeah it make more sense to use the initial attribute in metadatas.
Implementing something to pass the default attribute from models to Serializer's initial is more a design decision.

@Ernest0x
Copy link
Contributor

@cyberj Yes, If we also want to base the initial attribute of a ModelSerializer field to the default attribute of the corresponding model field, that is another decision to be taken. As an inspiration, as stated in the official documentation, Django's ModelForm (the equivalent of a ModelSerializer) uses in some cases the default value of a model field as an initial selection:

If the model field has choices set, then the form field’s widget will be set to Select, with choices coming from the model field’s choices. The choices will normally include the blank choice which is selected by default. If the field is required, this forces the user to make a selection. The blank choice will not be included if the model field has blank=False and an explicit default value (the default value will be initially selected instead).

@xordoquy
Copy link
Collaborator

@cyberj @Ernest0x thanks for sharing your thoughts, this will help taking a decision on that topic.

@tomchristie
Copy link
Member

I'm not in favor of us making any changes to how we handle OPTIONS requests right now, at least until there's eg. some structured content type or schema that we're working against.

Custom requirements should implement this as a custom metadata class. For bonus points package it up and we can reference it from the docs.

http://www.django-rest-framework.org/api-guide/metadata/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants