Support custom model object keys
See original GitHub issueHey, thanks for your work on this project. I was investigating integrating it with our EmberJS app but I ran into an issue. We have an Employee
model that has a normal integer primary key, but also a username
attribute. I’d like to only expose the username
as the primary key, using DRF this is simple:
class EmployeeSerializer(serializers.ModelSerializer):
id = serializers.ReadOnlyField(source="user.username")
...
However in build_json_resource_obj
it just uses instance.pk:
def build_json_resource_obj(fields, resource, resource_instance, resource_name):
resource_data = [
('type', resource_name),
('id', encoding.force_text(resource_instance.pk) if resource_instance else None),
...
Can you use the serializers ID field if available, and fallback to the instances PK?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:33 (12 by maintainers)
Top Results From Across the Web
Accesing items with custom keys in object [duplicate]
You can use Object. keys() to get the keys, loop over that. You can slso use Object. values() to get the values, and...
Read more >Object.keys() - JavaScript - MDN Web Docs
The Object.keys() method returns an array of a given object's own enumerable string-keyed property names.
Read more >Create a Custom Data Model Object - Salesforce Help
Use a custom data model object (DMO) to store information that is unique to your business.Required Editions Available in: ... Select a primary...
Read more >Creating object key names - Amazon Simple Storage Service
Object keys (object key names) uniquely identify Amazon S3 objects.
Read more >Using a Custom Class as a Key in a Java HashMap | Baeldung
Another important point is: Do not change the hash code of an object while the object is being used as a key. A...
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 am overjoyed to keep this discussion technical. 😄
My last comment was to provide an update about what’s going on with the maintainers so everyone has context of the project’s status. I’m happy to get back on track with the problem reported in this issue.
If the
fields
parameter ofbuild_json_resource_obj
method contains anid
field, use thisid
field, otherwise use the default implementation. I think this is very flexible.