Unable to create models on Flask-admin
Take a look at the relevant part in the source code for Flask-Admin here.
The model is created without passing any arguments:
model = self.model()
So you should support a constructor that takes no arguments as well. For example, declare your __init__
constructor with default arguments:
def __init__(self, title = "", content = ""):
self.title = title.title()
self.content = content
self.created_at = datetime.datetime.now()
So, this is how I've implemented a Post class in my application:
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.Unicode(80))
body = db.Column(db.UnicodeText)
create_date = db.Column(db.DateTime, default=datetime.utcnow())
update_date = db.Column(db.DateTime, default=datetime.utcnow())
status = db.Column(db.Integer, default=DRAFT)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __init__(self, title, body, createdate, updatedate, status, user_id):
self.title = title
self.body = body
self.create_date = create_date
self.update_date = update_date
self.status = status
self.user_id = user_id
If you're going to stick with instanciating your model with a created_at
value of datetime.datetime.now()
, you may want to reference my code above, wherein the equivalent datetime.utcnow()
function is set as the default for create_date
and update_date
.
One thing I'm curious about is your use of self.title=title.title()
and self.content = content.title()
; are those values coming from a function?
If not and you're passing them as strings, I think you'd want to update those to self.title = title
and self.content = content
That could explain why you're seeing your issue. If content.title()
isn't a function, that would result in no argument for that parameter...
you might try using the following and seeing if it resolves your issue:
class Post(db.Model):
__tablename__ = 'news'
nid = db.Column(db.Integer, primary_key = True)
title = db.Column(db.String(100))
content = db.Column(db.Text)
created_at = db.Column(db.DateTime, default=datetime.datetime.now())
def __init__(self, title, content, created_at):
self.title = title
self.content = content
self.created_at = created_at