If I got your question correctly, you're just asking what you should do in order to get this JSON response from your DRF API:
{
"student": { "name" : ..., "last_name" : ...}
"class": { ... },
"subjects": {
"mandatory": {...},
"language": {...},
"elective": {...}
}
}
I'm sorry, but what you're asking for is basically writing models.py
for you. As much as I do not aspire to become another Stack Overflow sherrif - this is not a place where you ask for completing a task for you. I can however point out a few issues that prevent your code from working, no matter what serializer would you pass it through.
Not really preventing your code form working, but you need to learn about using Code Style. This can be very helpful - it's not a code style guide, it's from Django contributors documentation. Some of it does not apply to your project, but there's a lot of good practices you should definitely follow, especially regarding naming conventions. Also, learn and use PEP 8
You're using class
as a field name in your model student
. Names like class
, def
, type
are keywords in Python and (in short) you can't use them as your variable names. For more details see this post
You need to learn about how relational DB, Django models and their relations work. I assume you're working on some kind of simple school ERP system where you want to store data about students, classes and subjects. If that's the case, you definitely need more than just one model with JSON fields. If you don't know why, look at your model, imagine it actually works and try to think of: A) How would you select all students that are assigned for a certain class? B) How would you select all students that have some subject assigned? C) There is a subject that is teached in more than one class. How do you store that information?
Read carefully this article from Django documentation and think about what models do you need for your app and what are the relations between them. There's definitely more than one way to do this, and it depends on what your assumptions and requirements are. Here are however a few simple facts that you can start with:
Person
exists
Student
is a specific type of a person (not every person is necessarily a student)
School
exists and it has some students
Classes
exist and they belong to some specific School
Subjects
exist in a school, sometimes they're just related to one class, but sometimes to more than one
- Every
Subject
can be: mandatory
or not, elective
or not, and be of one of some defined list of subject types: e.g. science
, history
, language
...
I hope it helps. Don't hesitate to ask more questions in comments, and let me know if it helped you with your problem.