pallets-eco / flask-admin

Simple and extensible administrative interface framework for Flask
https://flask-admin.readthedocs.io
BSD 3-Clause "New" or "Revised" License
5.75k stars 1.57k forks source link

Trying to add form_widget_args to sub document form in mongoengine modelview #361

Closed bool-dev closed 10 years ago

bool-dev commented 10 years ago

I have been trying to add form_widget_args to a field in a mongonengine EmbeddedDocument, using:

form_subdocuments = {
    'author': {
        'form_widget_args': {
            'name': {'disabled': True}
        }
    }
}

but it does not work. Is this not yet supported? p.s. - For main document fields widget_args work perfectly.

Edit: Also tried, without any luck:

form_widget_args = {
    'author.name': {'disabled': True}
}
rochacbruno commented 10 years ago

I think the sintax is a bit different, I also tried and no luck, it does not works also "form_rules" do not work.

https://github.com/pythonhub/quokka/blob/master/quokka/modules/posts/admin.py#L80

But form_columns and form_ajax_refs worked well.

    form_subdocuments = {
        'contents': {
            'form_subdocuments': {
                None: {
                    'form_columns': ('content', 'caption', 'purpose', 'order'),
                    'form_ajax_refs': {
                        'content': {
                            'fields': ['title', 'long_slug', 'summary']
                        }
                    }
                }
            }
        },
    }
rochacbruno commented 10 years ago

I cant understand the "None" as key...

mrjoes commented 10 years ago

form_widget_args are probably not passed to child models (I'm sure it is not passed to inline models - there's bug for that), I will check.

@rochacbruno: None in form_subdocuments is only necessary if you have ListField with child field. Unfortunately, it is limitation of flask-mongoengine form scaffolding functionality - it creates child field for ListField with None as name.

Currently, I'm not sure how to make it look better.

Update: I'm slightly busy this week, will go over all issues this weekend.

bool-dev commented 10 years ago

@mrjoes , I want to help, was trying to figure out a solution but haven't come up with anything yet. If you could tell me which files to look at, i could continue trying.

@rochacbruno what mrjoes said, 'None' is for ListFields.

rochacbruno commented 10 years ago

Understood! thanks.

So, I think that is the problem with form_rules, need to check if it is being passed to subdocumments inside a ListField.

mrjoes commented 10 years ago

Very sorry it took that long - I was very busy lately.

bool-dev commented 10 years ago

@mrjoes Thanks for this. :+1: Better late than never! Will also check and report if there is anything missing.

cybertoast commented 9 years ago

Hate to reopen this, but figured the issue is what's being talked about here. From what I can tell it's still a problem with EmbeddedDocumentField (ie form_subdocuments). When I attempt to set 'form_widget_args' on the subdocument it still does not work (though form_columns and form_args works fine). My definition is:

form_subdocuments = {
    'metadata': {
        'form_columns': ('artist', 'album',),
        'form_widget_args': {
            'artist': { 'rows': 50 }
        }
    }
}

The form_widget_args section does not appear to get set. Wonder if I'm doing something wrong. The field 'metadata' is just a db.EmbeddedDocumentField(MetadataDoc), so not a list.

(This is with 1.0.8, but also tried with the master branch and got the same results)

cybertoast commented 9 years ago

Actually, this is bizarre. If also add a form_rules key then it all works fine! But I don't understand why form_rules are needed in order to get the form_widget_args to work. Basically the following works:

form_subdocuments = {
    'metadata': {
        'form_rules`: ('artist', 'album',)
        'form_widget_args': {
            'artist': { 'rows': 50 }
        }
    }
}
SophiaAP commented 7 years ago

One workaround for this is to add a read-only flag to subdocuments at the database level. To do this, add SQLAlchemy's viewonly=True configuration to the relationship in your model definition.

children = relationship('Category', backref=db.backref('parent', remote_side=[id]), viewonly=True)

See SQLAlchemy Relationship Parameters.

bekab95 commented 6 years ago

@cybertoast Can you tell me how to get worked Ckeditor widget for name in EmbeddedDocument

class EmbeddedService(EmbeddedDocument): name = StringField()

class Service(Document): ... services = ListField(EmbeddedDocumentField(EmbeddedService)) ...

bekab95 commented 6 years ago

I just did it :D

capture

cllen commented 5 years ago

@ufo911 thanks!