emmett-framework / emmett

The web framework for inventors
BSD 3-Clause "New" or "Revised" License
1.06k stars 71 forks source link

Default value presentation in select widgets and other relation fields #226

Closed yevheniidehtiar closed 6 years ago

yevheniidehtiar commented 6 years ago

Hi! It's not good in view, when I see IDs instead names on relation fields in form. ` class Category(Model):

name = Field()
iso_code = Field.int()
can_be_main = Field.bool()

refers_to({'parent': 'self'})
has_many({'children': 'self.parent'})

` Field parent in form is select widget and values looking like with 1,2. But I want to see the name of category. I found 'representation' feature in docs, but example in docs is not fully understandably. Can you help me with it. Please

josejachuf commented 6 years ago

Hi @yevheniidehtiar You can see in the documentation

http://www.weppy.org/docs/1.2/validations -> Inclusion in database sets I believe that is what you are needed

yevheniidehtiar commented 6 years ago

Thank you. And what's about multiple select for many2many fields. For example, I must to pick many groups to user on User's model form?

josejachuf commented 6 years ago

@yevheniidehtiar, I'm not sure, but I think with 'multiple': True it could work

gi0baro commented 6 years ago

@yevheniidehtiar you might want to add the format attribute to your model:

class Category(Model):
    format = "%(name)s"
yevheniidehtiar commented 6 years ago

How about multiple select fields for relation. Can you give example, if it's possible?

gi0baro commented 6 years ago

@yevheniidehtiar I'm sorry, but weppy doesn't provide any "ready to go" form for many to many relations. You should create a custom form and consequent logics for that.

yevheniidehtiar commented 6 years ago

@gi0baro I understand, but I found multiple option in weppy/forms.py - widget_multiple I tried to use @josejachuf way with multiple: True , but it's return error. I hardcore it, but working only widget_select.
Do you have some example with hand-coded form with many2many widget default rw logic? I think that weppy must to have "ready to go" widgets with customization in model.form_widgets. For example, to o2m we can choose: 'radiobutton', 'select', 'select_auto` (with autocomplete). To m2m - 'multiselect', 'multiselect_auto'. Bootstrap has it.

gi0baro commented 6 years ago

@yevheniidehtiar No, multiple is used for list fields. As of today, weppy forms interacts with the referred model only, which means you can have widgets for belongs_to and refers_to because those are fields on the model, while has_many and has_one are relations that involve separated models, not the one you're editing.

I'm sorry, but it's totally unplanned right now to have such functionality in weppy. If you want such functions you should implements all the logics to save related models in the route, and if you manage to do it with some "reusable logic", I'm open to pull requests and further discussions :)