I am trying to create a slug field in one of my DB models. I am using Flask-SQLAlchemy and Flask-Admin. My Class is defined as:
class Merchant(db.Model):
__tablename__ = 'merchants'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String())
...
slug = db.Column(db.String())
def __repr__(self):
return 'Merchant: {}'.format(self.name)
I have attempted to implement something like this from this question:
def __init__(self, *args, **kwargs):
if not 'slug' in kwargs:
kwargs['slug'] = slugify(kwargs.get('name', ''))
super().__init__(*args, **kwargs)
However it does not work when I am creating the new Merchant using Flask-Admin.
In trying to determine why, I change the init function to simply print kwargs
. When I create a new Merchant in Flask-Admin, it prints {}
, however when I do it in the python shell, like Merchant(name ='test')
, it prints {'name' : 'test'}
.
Does anyone know how I can access the arguments being passed to my class at init from Flask-Admin?