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

Allow meta attributes in relationship fields #406

Open
svametcalf opened this issue Jan 31, 2018 · 3 comments
Open

Allow meta attributes in relationship fields #406

svametcalf opened this issue Jan 31, 2018 · 3 comments
Labels

Comments

@svametcalf
Copy link

I have a setup where we are using a ManyToMany field with a through model, similar to the situation described in this forum post from the json-api spec .

It seems like there are two ways of approaching this. One would be to use the meta key in the relationship, such as:

{
  "type": "objective",
  "id": "00559422-02E7-11DB-8468-BA18C3C820AE",
  "relationships": {
    "concepts": {
      "data": [
        { "type": "concept", "id": "02B7", "meta": { "emphasis": "primary" } },
        { "type": "concept", "id": "3AF6", "meta": { "emphasis": "secondary" } },
        { "type": "concept", "id": "CB23", "meta": { "emphasis": "tertiary" } }
      ]
    }
  }
}

Is this possible in using this library?

On the other hand, I could have the relationship point to the through model, which would be formatted as such:

{
  "type": "ObjectiveConceptLink", 
  "id": "....", 
  "attributes": {
     "emphasis": "primary", 
  }, 
  "relationships": {
     "concept": {
        "data": {
           "type": "concept", 
           "id": "...."
        }
     }, 
     "objective": {
        "data": {
           "type": "objective", 
           "id": "...."
        }
     }
  }
}

Right now in my app, the response does not respect the through key when using a ResourceRelatedField. The resulting response relationships key has a type of objective, as opposed to objective-concept-link.

Are either of these solutions possible with this library? If so, how?

@sliverc
Copy link
Member

sliverc commented Jun 12, 2018

Currently meta is not supported with ResourceRelatedField but would be a good addition.

That your second approach is not working is most likely a bug. What you could do is to work around it to define a included_serializers with the objective relationship and define the correct resource_name.

Marking this issue as a bug, any investigation and PRs are most welcome.

@sliverc sliverc changed the title representing many to many relationship with attributes Allow meta attributes in relationship fields Jan 14, 2019
@ghost
Copy link

ghost commented Oct 28, 2023

Is Meta still not supported with ResourceRelatedField (as shown in the first example above)?

Even if it is not supported I figured it should be possible with a similar approach as used in #1018.

Any suggestion what function to overwrite if I would like to add 1 meta field to a SerializerMethodResourceRelatedField (to data, not links)?

@sliverc
Copy link
Member

sliverc commented Oct 30, 2023

No, this has not been implemented yet.

To implement it, I would suggest that a get_meta(rself, esource) returning a dict is added to ResourceRelatedField and then called at the appropriate spot in the renderer. A user can then create its own ResourceRelatedField with the desired meta fields.

Any PR is welcome.

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

No branches or pull requests

2 participants