I am new to API and I am creating a FLask Restful API.I was wondering that do I need to create new model and resource classes for any row manipulation I want to do in my DB? For example I have created a student in my DB. On creation he does not have any grades and so I created StudentModel and StudentResource and used table Student. When I need to update grades using PUT request do I need to create a SudentGradeModel and StudentGradeResource also accessing student table?
Every Model class includes helper functions that the Resource class uses by importing the Model class. The Resource classes only have GET, POST, PUT and DELETE methods.
class StudentModel(db.Model):
__tablename__ = 'Student'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(30))
class_sec = db.Column(db.String(4))
def __init__(self, id, name, class_sec):
self.id = id
self.name= name
self.class_sec = class_sec
from flask_restful import Resource, reqparse
from models.student_model import StudenteModel
# noinspection PyMethodMayBeStatic
class StudentResource(Resource):
parser = reqparse.RequestParser()
parser.add_argument('id', type=int, required=True, help='Every Student must have an ID')
parser.add_argument('name', type=str, required=True, help='Every Student must have a name')
parser.add_argument('class', type=str, required=True, help='Every Student must be assigned a class and section')
def get(self, id):
pass
def post(self, id):
pass
class StudentGradeModel(db.Model):
__tablename__ = 'Student'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
grade = db.Column(db.String(2), primary_key=True)
def __init__(self, id, grade):
self.id = id
self.grade = grade
# noinspection PyMethodMayBeStatic
class StudentGradeResource(Resource):
parser = reqparse.RequestParser()
parser.add_argument('id', type=int, required=True, help='Student must have an ID to access table')
parser.add_argument('grade', type=str, required=True, help='Student must have a grade to be assigned')
def get(self, id):
pass
def post(self, id):
pass
Similarly if I wanted to only update the section would I have to create a similar Classe with a PUT request.
Thank You
From the question, I'm assuming that one student can only have one grade or no grade at all, because if they can have more than one, a grade must be in a separate table.
The table creation SQL looks like this:
CREATE TABLE student (
id INT PRIMARY KEY,
name VARCHAR(30) NOT NULL,
class_sec CHAR(4) NOT NULL,
grade INTEGER
);
(I changed the datatype for grade since numeric data shouldn't be stored as string)
No, you can't, and should not have two models for the same table. The model should represent the table itself.
class StudentModel(db.Model):
__tablename__ = 'Student'
__table_args__ = {'extend_existing': True}
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(30), nullable=False)
class_sec = db.Column(db.String(4), nullable=False)
grade = db.Column(db.Integer)
def __init__(self, id, name, class_sec):
self.id = id
self.name= name
self.class_sec = class_sec
On the other hand, you can have more than one resource to interface a table. However, each resource is associated with one route, and you shouldn't have a separate resource for grade, unless you need another route for that, which I think you don't.
class Student(Resource):
...
def put(self, id):
request_body = request.get_json()
# do your things here