You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/content/fields/form.md
+4-3Lines changed: 4 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -5,6 +5,7 @@ title: FormField
5
5
::: api formidable.FormField
6
6
:show_members: false
7
7
:::
8
+
8
9
----
9
10
10
11
This is a form field that contains another form as its value. For example:
@@ -28,22 +29,22 @@ class Profile(f.Form):
28
29
29
30
To the user, this will probably look as part of the same form, why bother then? Well, this field isn't about showing the form, it's about how the data is saved.
30
31
31
-
When saving a form with a `FormField`, the contents of it comes encapsulated in their own object or dictionary:
32
+
When saving a form with a `FormField`, the contents of it come encapsulated in their own object or dictionary:
32
33
33
34
```python
34
35
print(form.save())
35
36
36
37
{
37
38
"name": "My name",
38
39
"settings": {
39
-
"locale": "en_us"
40
+
"locale": "en_us",
40
41
"timezone": "utc",
41
42
"email_notifications": True,
42
43
},
43
44
}
44
45
```
45
46
46
-
This field is useful when you want to store those group of fields separated, for example:
47
+
This field is useful when you want to store that group of fields separated, for example:
47
48
48
49
In a different model with a one-to-one relationship to the main model
Copy file name to clipboardExpand all lines: docs/content/nested.md
+3-3Lines changed: 3 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,7 +196,7 @@ for example, to the `static/js/` folder.
196
196
197
197
Then, add this to the header of your base template:
198
198
199
-
```html {hk_lines="4-8"}
199
+
```html {hl_lines="4-8"}
200
200
<html>
201
201
<head>
202
202
...
@@ -219,7 +219,7 @@ class TodoListForm(f.Form):
219
219
todo = f.NestedForms(TodoForm, allow_delete=True)
220
220
```
221
221
222
-
... and a template. I'm using a macro to render the `TodoForm` (because later we are going to need to render it more than once:
222
+
... and a template. I'm using a macro to render the `TodoForm` (because later we are going to need to render it more than once):
223
223
224
224
```html+jinja
225
225
{% macro render_todo(form, label) -%}
@@ -301,7 +301,7 @@ The JS script needs something to clone so it can add a new nested form, let's ad
301
301
</form>
302
302
```
303
303
304
-
**Note that we call the macro using `form.todo.empty_form`**. This is a special attribute of a `NestedForms` field that generates an empty instance of a nested form, excatly for using it for this cases.
304
+
**Note that we call the macro using `form.todo.empty_form`**. This is a special attribute of a `NestedForms` field that generates an empty instance of a nested form, exactly for using it for these cases.
305
305
306
306
It's important to put it *inside* the element with the `data-nestedform` attribute (the form tag in our example).
Copy file name to clipboardExpand all lines: docs/content/orm.md
+42-5Lines changed: 42 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,12 +31,16 @@ After calling `form.save()`, you only need to commit the changes to the database
31
31
32
32
### SQLAlchemy and SQLModel
33
33
34
-
Because these ORMs work with a session pattern, you must add these two methods to a base form:
34
+
Because these ORMs work with a session pattern, Formidable's default `ObjectManager` needs to be extended. There are two ways to do this:
35
35
36
-
```python {title="forms/base.py"}
37
-
import formidable as f
36
+
#### Option A: Add methods to a model base class
38
37
39
-
classBaseForm(f.Form):
38
+
Formidable's `ObjectManager` calls `orm_cls.create(**data)` when creating new objects and `object.delete()` when deleting them. You can add these methods to a shared model base class:
39
+
40
+
```python {title="models/base.py"}
41
+
from sqlalchemy.orm import DeclarativeBase
42
+
43
+
classBase(DeclarativeBase):
40
44
@classmethod
41
45
defcreate(cls, **kwargs):
42
46
instance =cls(**kwargs)
@@ -49,7 +53,40 @@ class BaseForm(f.Form):
49
53
50
54
```
51
55
52
-
and make all your forms inherit from it:
56
+
Then use `Base` as the base for all your models:
57
+
58
+
```python {title="models/page.py"}
59
+
classPage(Base):
60
+
__tablename__ ="pages"
61
+
title: Mapped[str]
62
+
content: Mapped[str]
63
+
64
+
```
65
+
66
+
#### Option B: Provide a custom ObjectManager
67
+
68
+
Alternatively, you can subclass `ObjectManager` to handle the session directly:
0 commit comments