Pulling Current List From Database in Flask-Admin Form

I have a database named Account and I want to list the Account records of the current user in a select list in this database. But this list must be up to date and list all Accounts currently available. But the code I wrote does not work that way. For example, I add an Account, but I have to close and reopen the script for it to appear up to date. if I don’t do this the accounts are never listed. How can I solve this problem?

I want to show error/warning or success message on the form depending on the situation. What code can I do this with? I tried form.errors[‘general’] but I was not successful.

I would be very glad if you could help me, thank you.

class JobView(MyModelView):

column_list = ('id', 'user_id','account_name', 'api_key', 'keyword_list', 'instructions', 'language', 'product', 'search_tweet_limit', 'num_tweets', 'sleep_time')

form_columns = ['account_name', 'api_key', 'keyword_list', 'instructions', 'language', 'product', 'search_tweet_limit', 'num_tweets', 'sleep_time']

form_choices = {

'account_name': [], # Boş bırakıyoruz, dinamik olarak dolduracağız

'language': [], # Boş bırakıyoruz, dinamik olarak dolduracağız

'product': [('Latest', 'Latest'), ('Top', 'Top')]

}

def on_model_change(self, form, model, is_created):

if is_created:

model.user_id = current_user.id

super(JobView, self).on_model_change(form, model, is_created)

def is_accessible(self):

return current_user.is_authenticated

with app.app_context():

accounts = Account.query.all()

for account in accounts:

form_choices['account_name'].append((account.auth_info_1, account.auth_info_1))

languages = ['english', 'turkish', 'french', 'german', 'spanish', 'italian', 'portuguese', 'dutch', 'russian', 'japanese', 'chinese', 'arabic', 'korean']

for lang in languages:

form_choices['language'].append((lang, lang))

def create_form(self, obj=None):

form = super(JobView, self).create_form(obj)

# Populate account_name choices dynamically

form.account_name.choices = [(account.auth_info_1, account.auth_info_1) for account in Account.query.all()]

form.keyword_list.description = "Each keyword should be separated by a comma."

form.instructions = TextAreaField('Instructions', validators=[DataRequired()])

form.language.choices = [(lang, lang) for lang in nltk.corpus.stopwords.fileids()]

form.product = SelectField('Tweet Type', choices=[('Latest', 'Latest'), ('Top', 'Top')], widget=Select2Widget(), validators=[DataRequired()])

form.search_tweet_limit = IntegerField('Search Tweet Limit', validators=[NumberRange(min=20)], default=20)

form.num_tweets = IntegerField('Num Tweets', validators=[NumberRange(min=1, max=10)], default=5) # num_tweets max 10 olarak değiştirildi

form.sleep_time = IntegerField('Sleep Time', validators=[NumberRange(min=600)], default=600)

return form

def update_form(self, obj=None):

form = super(JobView, self).update_form(obj)

# Populate account_name choices dynamically

form.account_name.choices = [(account.auth_info_1, account.auth_info_1) for account in Account.query.filter_by(user_id=current_user.id)]

form.keyword_list.description = "Each keyword should be separated by a comma."

form.instructions = TextAreaField('Instructions', validators=[DataRequired()])

form.language.choices = [(lang, lang) for lang in nltk.corpus.stopwords.fileids()]

form.product = SelectField('Tweet Type', choices=[('Latest', 'Latest'), ('Top', 'Top')], widget=Select2Widget(), validators=[DataRequired()])

form.search_tweet_limit = IntegerField('Search Tweet Limit', validators=[NumberRange(min=20)], default=20)

form.num_tweets = IntegerField('Num Tweets', validators=[NumberRange(min=1, max=10)], default=5) # num_tweets max 10 olarak değiştirildi

form.sleep_time = IntegerField('Sleep Time', validators=[NumberRange(min=600)], default=600)

return form

def create_model(self, form):

try:

model = self.model()

form.populate_obj(model)

account = Account.query.filter_by(auth_info_1=model.account_name).first()

if not account:

form.errors['account_name'] = 'Account not found'

return False

model.user_id = current_user.id

self.session.add(model)

self.session.commit()

# Start job schedule

schedule_job(model.account_name, model.api_key, model.keyword_list, model.instructions, model.language, model.num_tweets, model.id, model.sleep_time, model.search_tweet_limit, model.product)

return model

except Exception as e:

if not self.handle_view_exception(e):

raise

self.session.rollback()

form.errors['general'] = str(e)

return False

def update_model(self, form, model):

try:

form.populate_obj(model)

account = Account.query.filter_by(auth_info_1=model.account_name).first()

if not account:

form.errors['account_name'] = 'Account not found'

return False

self.session.commit()

return model

except Exception as e:

if not self.handle_view_exception(e):

raise

self.session.rollback()

form.errors['general'] = str(e)

return False

def delete_model(self, model):

try:

job_id = model.id

schedule.clear(job_id)

self.session.delete(model)

self.session.commit()

return True

except Exception as e:

if not self.handle_view_exception(e):

raise

self.session.rollback()

form.errors['general'] = str(e)

return False

I expect all User’s Accounts to be listed as up-to-date when the form is loaded. However, when a new Account is added, the script must be closed and run again for it to appear.

New contributor

neuralabz is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật