Skip to content
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

POST on Browsable API fails because of get_initial #63

Open
jonathan-golorry opened this issue Jul 7, 2017 · 0 comments
Open

POST on Browsable API fails because of get_initial #63

jonathan-golorry opened this issue Jul 7, 2017 · 0 comments

Comments

@jonathan-golorry
Copy link

The get_initial of a serializer normally looks like this:

def get_initial(self):
    if hasattr(self, 'initial_data'):
        return OrderedDict([
            (field_name, field.get_value(self.initial_data))
            for field_name, field in self.fields.items()
            if (field.get_value(self.initial_data) is not empty) and
            not field.read_only
        ])
     
    return OrderedDict([
        (field.field_name, field.get_initial())
        for field in self.fields.values()
        if not field.read_only
    ])

This breaks with AttributeError: 'list' object has no attribute 'get' if initial data is a list, such as after a POST on the browsable api (using the raw data tab to submit multiple objects).

I've fixed this in my code by switching get_initial on BulkSerializerMixin to:

def get_initial(self):
    return OrderedDict([
        (field.field_name, field.get_initial())
        for field in self.fields.values()
        if not field.read_only
    ])

A better solution might be to not set initial_data during a POST, but this was easier for me to find.

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

No branches or pull requests

1 participant