question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support custom model object keys

See original GitHub issue

Hey, 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:open
  • Created 8 years ago
  • Reactions:1
  • Comments:33 (12 by maintainers)

github_iconTop GitHub Comments

4reactions
mblaymancommented, May 15, 2017

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.

2reactions
wakingyeungcommented, Apr 26, 2021

If the fields parameter of build_json_resource_obj method contains an id field, use this id field, otherwise use the default implementation. I think this is very flexible.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found