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
Arguments with the types of `datetime`, `date`, `time`, `timedelta`, and `UUID` can be coerced by using a type annotation in the action method.
80
+
81
+
```{note}
82
+
[Django's `dateparse` methods](https://docs.djangoproject.com/en/stable/ref/utils/#module-django.utils.dateparse) are used to convert strings to the date-related type.
83
+
```
84
+
81
85
```{note}
82
-
Strings will be converted to `datetime` if they are successfully parsed by Django's [`parse_datetime`](https://docs.djangoproject.com/en/stable/ref/utils/#django.utils.dateparse.parse_datetime) method.
86
+
Both integer and float epochs can also be coerced into `datetime` or `date` objects.
87
+
```
88
+
89
+
```python
90
+
# argument_type_hints.py
91
+
from django_unicorn.components import UnicornView
92
+
from datetime import date, datetime
93
+
from uuid importUUID
94
+
95
+
classArgumentTypeHintsView(UnicornView):
96
+
defis_datetime(self, obj: datetime):
97
+
asserttype(obj) is datetime
98
+
99
+
defis_uuid(self, obj: UUID):
100
+
asserttype(obj) isUUID
101
+
102
+
defis_date(self, _date: date =None):
103
+
asserttype(obj) is _date
104
+
```
105
+
106
+
```html
107
+
<!-- argument-type-hints.html -->
108
+
<div>
109
+
<buttonunicorn:click="is_datetime('2020-09-12T01:01:01')">Check datetime with string</button>
110
+
<buttonunicorn:click="is_datetime(1691499534)">Check datetime with epoch</button>
Django models can be instantiated as an argument or with a `pk` kwarg and a type annotation.
119
+
120
+
```python
121
+
# argument_model.py
122
+
from django_unicorn.components import UnicornView
123
+
from django.contrib.auth.models import User
124
+
125
+
classArgumentModelView(UnicornView):
126
+
defis_user_via_arg(self, obj: User):
127
+
asserttype(obj) is User
128
+
129
+
defis_user_via_kwarg(self, pk: User=None):
130
+
asserttype(obj) is User
83
131
```
84
132
85
-
````{note}
86
-
Enums themselves cannot be passed as an argument, but the enum _value_ can be since that is a Python primitive. In the component's view, use the enum's constructor to convert the primitive back into the enum if needed.
133
+
```html
134
+
<!-- argument-model.html -->
135
+
<div>
136
+
<buttonunicorn:click="is_user_via_arg(1)">Gets user with pk of 1</button>
137
+
<buttonunicorn:click="is_user_via_kwarg(pk=2)">Gets user with pk of 2</button>
138
+
</div>
139
+
```
140
+
141
+
#### Custom types
142
+
143
+
Custom objects can also be used as a type annotation and `Unicorn` will attempt to instantiate the object with the value that is passed in as the argument.
Enums themselves cannot be passed as an argument, but the enum _value_ can be since that is a Python primitive. Use the enum as a type annotation to coerce the value into the specified enum.
87
168
88
169
```python
89
170
# enum.py
@@ -100,8 +181,8 @@ class EnumView(UnicornView):
100
181
color = Color.RED
101
182
purple_color = Color.PURPLE
102
183
103
-
def set_color(self, color_int: int):
104
-
self.color = Color(color_int)
184
+
defset_color(self, color: Color):
185
+
self.color =color
105
186
```
106
187
107
188
```html
@@ -121,8 +202,6 @@ class EnumView(UnicornView):
121
202
</div>
122
203
```
123
204
124
-
````
125
-
126
205
## Set shortcut
127
206
128
207
Actions can also set properties without requiring an explicit method.
@@ -309,7 +388,7 @@ Sometimes you need to trigger a method on a component from regular JavaScript. T
309
388
</button>
310
389
```
311
390
312
-
Passing arguments the method call is also supported.
391
+
Passing arguments to the method call is also supported.
0 commit comments