Created
June 17, 2013 03:45
-
-
Save sharoonthomas/5794541 to your computer and use it in GitHub Desktop.
A WTForms many2one field which works with nereid and Tryton
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Many2OneField(SelectField): | |
| """ | |
| A select field that works like a many2one field in tryton | |
| Additional arguments | |
| :param model: The name of the model | |
| :param domain: Tryton search domain | |
| """ | |
| def __init__(self, label=None, validators=None, | |
| model=None, domain=None, **kwargs): | |
| if model is None: | |
| raise Exception("Model name cannot be none") | |
| self.model = model | |
| self.domain = [] if domain is None else domain | |
| super(Many2OneField, self).__init__( | |
| label, validators, int, None, **kwargs | |
| ) | |
| def iter_choices(self): | |
| Model = Pool().get(self.model) | |
| for record in Model.browse(Model.search(self.domain)): | |
| yield (record.id, record.rec_name, record.id == self.data) | |
| def pre_validate(self, form): | |
| Model = Pool().get(self.model) | |
| exists = Model.search(self.domain + [('id', '=', self.data)]) | |
| if not exists: | |
| raise ValueError(self.gettext('Not a valid choice')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment