Skip to content

I can not assign an id "id": null #404

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
agalera opened this issue Jan 19, 2018 · 2 comments
Closed

I can not assign an id "id": null #404

agalera opened this issue Jan 19, 2018 · 2 comments

Comments

@agalera
Copy link

agalera commented Jan 19, 2018

When doing a response with a dict, I have no way to return a different id, what can I do? I leave the code fragment and the answer I get

example_data = {
                "id": example.id,
                 .....
                "ref": example.ref,
                "error_url": self.data["attributes"]["error_url"],
                "return_url": self.data["attributes"]["return_url"],
            }
            
            serializer = self.get_serializer(data=example_data)
            serializer.is_valid(raise_exception=True)
            return Response(
                serializer.data,
                status=self.get_status_code(serializer.data),
                **kwargs
            )

We have trying to get one response from a new serializer whose content is the mix between two different models. We don't have a pk, because there are two models

The response returns this:

"data":{"type":"example","id":null,"attributes":{"...."}}

how could I get an id with a right value?

Thanks!

@kaigezhang
Copy link

kaigezhang commented Mar 9, 2018

Use you own renderer to rewrite the JSONRenderer method:
this line:

('id', encoding.force_text(resource_instance.pk) if resource_instance else resource.get('id'))
@classmethod
    def build_json_resource_obj(cls, fields, resource, resource_instance, resource_name,
                                force_type_resolution=False):
        # Determine type from the instance if the underlying model is polymorphic
        if force_type_resolution:
            resource_name = utils.get_resource_type_from_instance(resource_instance)
        resource_data = [
            ('type', resource_name),
            ('id', encoding.force_text(resource_instance.pk) if resource_instance else resource.get('id')),
            ('attributes', cls.extract_attributes(fields, resource)),
        ]
        relationships = cls.extract_relationships(fields, resource, resource_instance)
        if relationships:
            resource_data.append(('relationships', relationships))
        # Add 'self' link if field is present and valid
        if api_settings.URL_FIELD_NAME in resource and \
                isinstance(fields[api_settings.URL_FIELD_NAME], relations.RelatedField):
            resource_data.append(('links', {'self': resource[api_settings.URL_FIELD_NAME]}))
        return OrderedDict(resource_data)

@sliverc
Copy link
Member

sliverc commented Jun 8, 2018

There is already a discussion #155 on how support could be added to DJA.

Closing this in favor of that issue. In your case you might also use a namedtuple though which has a pk property to represent the primary key.

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

3 participants