Issue
When I add a new collection through Flask-admin panel I don't get the init method to reduce the image through another function. And when I add a new collection through the Python console everything works. But Flask-admin panel...
Model:
class Collections(db.Model):
__tablename__ = 'collections'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String, nullable=False)
preview_photo = db.Column(db.String, nullable=False)
def __init__(self, name, preview_photo):
self.name = name
self.preview_photo = collections_resize(preview_photo)
def __repr__(self):
return f'{self.name}'
As you can see, in the init method I pass in self.thumb_photo the result of the collections_resize() function. But the method doesn't run through the panel, can you explain why?
View model:
class CollectionsView(ModelView):
form_columns = ['name', 'preview_photo']
admin.add_view(CollectionsView(Collections, db.session))
Solution
I ended up rewriting the view model this way and it worked, but I had to write a separate html for it and it looks really weird, but it will do. If you have any better ideas, suggest them.
class CollectionsView(ModelView):
form_columns = ['id', 'name', 'preview_photo']
@expose('/new/', methods=('GET', 'POST'))
def create_view(self):
if request.method == 'POST':
if 'file' not in request.files:
return redirect(request.url)
file = request.files['file']
if file.filename == '':
return redirect(request.url)
if file and allowed_file(file.filename):
filename = file.filename
file.save(os.path.join('path', filename))
name = request.form.get('name')
preview_photo = filename
q = Collections(name=name, preview_photo=preview_photo)
db.session.add(q)
db.session.commit()
return redirect('/admin/collections')
return render_template('create_coll.html')
Answered By - Никита К
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.