Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

250
Views
Django DRF get ForeignKey values in serializer

I'm trying to retrieve all Benchmarks related to a Node in my serializer, but i'm not too sure how to retrieve them. Would I need to do some sort of reverse foreignkey lookup? Maybe my models are not made correctly?

class Node(models.Model):
    node_id = models.CharField(max_length=42, unique=True)
    wallet = models.CharField(max_length=42, null=True, blank=True)
    earnings_total = models.FloatField(null=True, blank=True)
    data = models.JSONField(null=True)
    online = models.BooleanField(default=False)
    version = models.CharField(max_length=5)
    updated_at = models.DateTimeField(null=True, blank=True)
    created_at = models.DateTimeField(auto_now_add=True)


class Benchmarks(models.Model):
    benchmark_score = models.IntegerField(default=0)
    benchmarked_at = models.DateTimeField(null=True, blank=True)
    provider = models.ForeignKey(Node, on_delete=models.CASCADE)

class NodeSerializer(serializers.ModelSerializer):

    class Meta:
        model = Node
        fields = ['earnings_total', 'node_id', 'data',
                  'online', 'version', 'updated_at', 'created_at', ]
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can define a serializer for the Benchmarks model:

class BenchmarkSerializer(serializers.ModelSerializer):
    
    class Meta:
        model = Benchmarks
        fields = ['benchmark_score', 'benchmarked_at']

then we can use that serializer in the NodeSerializer:

class NodeSerializer(serializers.ModelSerializer):
    benchmarks_set = BenchmarkSerializer(many=True)

    class Meta:
        model = Node
        fields = ['earnings_total', 'node_id', 'data',
                  'online', 'version', 'updated_at', 'created_at', 'benchmarks_set']

Note: normally a Django model is given a singular name, so Benchmark instead of Benchmarks.

over 4 years ago · Santiago Trujillo Report

0

Use HyperlinkedModelSerializer like this:

class BenchmarkSerializer(serializers.HyperlinkedModelSerializer):
        
    class Meta:
        model = Benchmarks
        fields = ['benchmark_score', 'benchmarked_at']

class NodeSerializer(serializers.HyperlinkedModelSerializer):
    benchmark_set = BenchmarkSerializer(many=True, read_only=True)

    class Meta:
        model = Node
        fields = ['node_id', 'wallet', 'earnings_total', 'data', 'online',  
                  'version', 'updated_at', 'created_at', 'benchmark_set']
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!