Serializing one-to-one relationships
See original GitHub issueI’m trying to serialize a one-to-one relationship and I can’t figure out to to setup my models and serializers to make this work.
// models/user.js
import { Model } from 'ember-cli-mirage';
export default Model.extend({
// Should some relationship go here?
})
// models/address.js
import { Model, belongsTo } from 'ember-cli-mirage';
export default Model.extend({
user: belongsTo()
});
// serializers/user.js
import ApplicationSerializer from './application';
export default ApplicationSerializer.extend({
relationships: ['address']
});
With this setup, we have address.user
and address.userId
, but there is no user.address
. When serializing user, the address isn’t included in the payload. I was expecting a hasOne()
model relationship since we’re dealing with foreign keys in a database, but I see this doesn’t exist.
This may be a conceptual misunderstanding so maybe something could be added to the models
documentation to clarify how this should work.
Issue Analytics
- State:
- Created 8 years ago
- Reactions:3
- Comments:11 (4 by maintainers)
Top Results From Across the Web
How to serialize a relation OneToOne in Django with Rest ...
You can create custom serializer fields to create your custom PersonSerializer. You can add fields to get values from Enviroment .
Read more >Nested Relationships in Serializers for OneToOne fields in ...
The solution was to use DRF's Nested Relationships in serialization. I shall assume you have a fair working knowledge of Python, virtualenv, pip ......
Read more >Serializer relations - Django REST framework
Relational fields are used to represent model relationships. They can be applied to ForeignKey , ManyToManyField and OneToOneField relationships, ...
Read more >Django Rest Framework API #19 / One To One Relationship ...
Django Rest Framework API #19,You Will Learn:1- How to link two models using One To One relationship.2- How get and show the nested...
Read more >Serializer Relations - Django REST Framework - GeeksforGeeks
It has an employee field that holds a many-to-one relationship with the Employee model.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I would say to probably use hasMany on this side of the relationship, and clean up the data in
Serializer#serialize
.I am actually working through the ORM updates right now, and I’m about halfway through. I have one-to-one and reflexive relationships working at the moment. I’d estimate a few more weeks before this all makes it into master.
This is now supported in the 0-3 series! Read this post for more information.