0
我有以下代碼,它的工作原理,但它返回以下內容。我怎樣才能改變它顯示主題字段,而不是返回完整的對象(主題對象)???有任何想法嗎?謝謝。返回特定對象屬性序列化器Django Rest框架
{
"name": "Andres",
"nick": "andresito",
"avatar_s": "",
"rate": 50,
"id": 3,
"subjects": [
"subjects object",
"subjects object"
]
}
,它應該返回
{
"name": "Andres",
"nick": "andresito",
"avatar_s": "",
"rate": 50,
"id": 3,
"subjects": [
"Web Development",
"User Experience"
]
}
我的models.py是:
class subjects(models.Model):
#id = models.IntegerField(db_column='ID', primary_key=True) # Field name made lowercase.
id_user = models.IntegerField(db_column='ID_user') # Field name made lowercase.
s = models.CharField(max_length=90)
grade = models.IntegerField()
ss = models.CharField(max_length=100)
id_school = models.IntegerField(db_column='ID_school') # Field name made lowercase.
subject_area = models.CharField(max_length=20)
subject_number = models.CharField(max_length=30)
subject_section = models.CharField(max_length=10)
current = models.IntegerField()
class Meta:
db_table = 'subjects'
class users(models.Model):
#id = models.IntegerField(db_column='ID') # Field name made lowercase.
nick = models.CharField(unique=True, max_length=60)
name = models.CharField(max_length=80)
rate = models.CharField(max_length=2)
avatar_s = models.CharField(max_length=160)
def user_subjects(self):
subject = subjects.objects.filter(id_user=self.id)
return subject
class Meta:
db_table = 'users'
我Serializers.py是
類UserKarmaSerializer(serializers.HyperlinkedModelSerializer):
subjects = serializers.Field(source='user_subjects')
class Meta:
model = users
fields = ('name', 'nick', 'avatar_s', 'rate', 'id', 'subjects')