简体   繁体   中英

Django Rest Framework Serializer

I'm using Django Rest Framework, i'm trying to get some field from my models

class StudentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Student
        ordering = ['score' , 'username']
        fields = ('id', 'username', 'user' ,'first_name', 'last_name', 'reg_number' ,'score', 'classroom')

my problem is that i get just : -id -username -first_name -last_name -score

but i can't get the reg_number and classroom even if i delete all fields :

class StudentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Student
        ordering = ['score' , 'username']

i get the some result.

I have this problem just with 3 models : User & Student & Professor ( Student and Professor are inhereted from User )

my student model :

class Student(User):
    reg_number = models.IntegerField()
    classroom = models.ForeignKey(Classroom)
    score = models.IntegerField(default=0)
    skills = models.TextField(blank=True)
    interest = models.TextField(blank=True)
    about = models.TextField(blank=True)
    projects = models.TextField(blank=True)

Can you include your student model definition.?? The reason may be ur student model contains reverse relationship in any other models. You have explicitly include that field in serializer. Checkout reverse relations topic in following link http://www.django-rest-framework.org/api-guide/relations/#reverse-relations

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM