Reputation: 770
I am trying an api in sql alchemy in which i am storing details of courses into database. The below code is the one i am using in which course name, start date, end date and hours per day are from the form. Start date and end date are optional. So it can be none. But i cant store the value to database with null values.
course_db = Course(course=course, start_date=start_date, end_date=end_date, hours_per_day=hours)
My model is
class Course(Base): tablename = 'courses'
id = Column(INTEGER, primary_key=True)
course = Column(String, unique=True, nullable=False)
start_date = Column(Date, nullable=True)
end_date = Column(Date, nullable=True)
hours_per_day = Column(Integer, nullable=True)
def __repr__(self):
return "Course(%r)" % (self.course)
Can some one help me to sort this out. I am using postgresql as my database.
Upvotes: 0
Views: 121
Reputation: 770
Got the answer. Insert init in models
class Course(Base):
__tablename__ = 'courses'
id = Column(INTEGER, primary_key=True)
course = Column(String, unique=True, nullable=False)
start_date = Column(Date, nullable=True)
end_date = Column(Date, nullable=True)
hours_per_day = Column(Integer, nullable=True)
def __init__(self, course, start_date=None, end_date=None, hours_per_day=None):
self.course = course
if start_date:
self.start_date = start_date
if end_date:
self.end_date = end_date
if hours_per_day:
self.hours_per_day = hours_per_day
def __repr__(self):
return "Course(%r)" % (self.course)
Upvotes: 0