I have a flask-admin project, and this is the mongoengine docs I am working on:
class UserEmailTemplatesCategoriesTranslations(mongo_db.EmbeddedDocument):
language_code = mongo_db.StringField()
text = mongo_db.StringField()
class UserEmailTemplatesCategories(mongo_db.DynamicDocument):
meta = {
'collection': 'user_email_templates_categories',
'indexes': [
{'fields': ('category', 'subcategory'), 'unique': True}
]
}
category = mongo_db.StringField(choices=em_templates.get_category_names(), required=True)
subcategory = mongo_db.StringField(required=True)
default_text = mongo_db.StringField(default=None)
text_translation = mongo_db.ListField(mongo_db.EmbeddedDocumentField(UserEmailTemplatesCategoriesTranslations))
is_active = mongo_db.BooleanField(default=True)
I have been trying to figure out how I can turn UserEmailTemplatesCategoriesTranslations language_code field into a SelectField, I figured it out after reading this: Trying to add form_widget_args to sub document form in mongoengine modelview
so I applied it like this and it worked:
form_subdocuments = {
'text_translation': {
'form_subdocuments': {
None: {
'form_overrides': {
'language_code': SelectField
},
'form_args': {
'language_code': {'choices': []}
}
}
}
},
}
but now I am facing a new dilemma which I have failed to find a solution for, I have this code that brings me a list of languages:
def get_language_choices():
retrieved_languages = consumer_models.Languages.objects().only('language_code', 'language_name').order_by(
'language_name')
return [
(
obj.language_code,
f"{str(obj.language_code).upper()} - {str(obj.language_name).title()}"
) for obj in retrieved_languages
]
I want this code to be applied to language_code, on create and edit, but I can’t seem to figure how to do it, I have searched but to no avail.