The problem
I have an application that has a User
object and a Student
object. Some users are students. All students are users. In the database (django-ORM based), this is represented as a Student
table with a foreign-key to the User
table.
I'm trying to create a REST API, and an object hierarchy in the iOS app that models this API. I'm having trouble deciding how to model this.
The Current Solution
The best I've come up with is this: Have a User
model in iOS, have a Student
model in iOS which inherits from User
and extends it with more properties. Then, have a method which receives a JSON response from the server, and creates either a User
or a Student
model, depending on the dictinoary.
Finally, the server will need to always give me the most specific type. I.e., when I log in to the server, it will decide whether I'm a student or just a regular user, and will return me the proper dictionary.
Is this the best way?
This sounds a little complicated. But any other way I've thought of modeling it, e.g. changing the way the database is laid out, gives me a design in which the database isn't aware of all the constraints. For example, Student
objects are allowed to own other objects (e..g, homework_paper
). I can model this with a foreign key to a User
object instead of to the Student
object, and say that the Student
is simply an extension of the user. But then the database doesn't force the fact that a homework_paper
has to be owned by a student.
Is there a better way to solve this problem that I'm missing?