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

Control Embedded Behavior #116

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

ambsw-technology
Copy link

By default, the HalModelSerializer classifies all nested BaseSerializer instances as embedded nested objects. In reality, the REST representation need not align 1:1 with the database representation so it should be possible to override this behavior.

For example, I'm supporting a 3rd party data specification (HL7v3) which has a 1:many relationship between Patient and ID (i.e. each patient can have multiple IDs). For indexing reasons, this means that IDs need to be in their own table, but they're not really intended as embedded objects (e.g. they aren't independently linkable).

This PR creates a HalPromoteEmbeddedMixin that can be added to a nested serializer to indicate that it should be stored at the top level of the representation (and not _embedded).

P.S. I really wanted to add a check to __init__ to make sure the Meta.list_serializer_class also uses HalPromoteEmbeddedMixin. If you don't add it in both places, the many=True serializer won't pick up the behavior. The problem is (1) I don't want to raise an exception because you don't have to mess with it if you never use many and (2) we don't import logging anywhere in the package so I didn't have a standard pattern to follow.

@@ -1,7 +1,8 @@
from collections import defaultdict

from drf_hal_json import EMBEDDED_FIELD_NAME, LINKS_FIELD_NAME, URL_FIELD_NAME
from drf_hal_json.fields import HalContributeToLinkField, HalHyperlinkedIdentityField, HalIncludeInLinksMixin
from drf_hal_json.fields import HalContributeToLinkField, HalHyperlinkedIdentityField, HalIncludeInLinksMixin, \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what it's complaining about but here's another thread on the odd message houndci/hound#1769

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

Successfully merging this pull request may close these issues.

3 participants