Reputation: 3068
I have a django model where i use natural keys:
class AcademicProgramsManager(models.Manager):
def get_by_natural_key(self, acad_program_id, program_title, required_credits):
return self.get(acad_program_id = acad_program_id, program_title = program_title, required_credits = required_credits)
class AcademicPrograms(models.Model):
objects = AcademicProgramsManager()
acad_program_id = models.IntegerField(primary_key=True)
acad_program_category = models.ForeignKey(AcademicProgramCategories)
acad_program_type = models.ForeignKey(AcademicProgramTypes)
acad_program_code = models.CharField(max_length=25)
program_title = models.CharField(max_length=64)
required_credits = models.IntegerField()
min_gpa = models.FloatField()
description = models.CharField(max_length=1000)
class StudentAcademicPrograms(models.Model):
student = models.ForeignKey(Students)
academic_program = models.ForeignKey(AcademicPrograms)
credits_completed = models.IntegerField()
academic_program_gpa = models.FloatField()
primary_program = models.BooleanField()
But my serializer still does not output the elements of the foreign key
[
{
"pk": 1,
"model": "studentapp.studentacademicprograms",
"fields": {
"academic_program": 124,
"credits_completed": 32,
"primary_program": true,
"student": 1206,
"academic_program_gpa": 3.7
}
},
{
"pk": 2,
"model": "studentapp.studentacademicprograms",
"fields": {
"academic_program": 123,
"credits_completed": 32,
"primary_program": false,
"student": 1206,
"academic_program_gpa": 3.4
}
}
]
Where am i going wrong?
Upvotes: 1
Views: 884
Reputation: 13178
You need to add a def natural_key(self)
method to your AcademicPrograms
model. You must also pass use_natural_foreign_keys=True
or use_natural_primary_keys=True
when serializing. See the django docs for an example.
Upvotes: 2