|
1 | 1 | import logging |
2 | 2 | from dataclasses import is_dataclass |
3 | | -from typing import Any, Union |
| 3 | +from typing import Any, Dict, Set, Union |
4 | 4 |
|
5 | 5 | from django.db.models import Model, QuerySet |
6 | 6 |
|
@@ -199,10 +199,38 @@ class TestComponent(UnicornView): |
199 | 199 |
|
200 | 200 | for (idx, model) in enumerate(queryset._result_cache): |
201 | 201 | if model.pk == model_value.get("pk"): |
202 | | - queryset._result_cache[idx] = model_type(**model_value) |
| 202 | + constructed_model = _construct_model( |
| 203 | + model_type, model_value, constructed_models |
| 204 | + ) |
| 205 | + queryset._result_cache[idx] = constructed_model |
203 | 206 | model_found = True |
204 | 207 |
|
205 | 208 | if not model_found: |
206 | | - queryset._result_cache.append(model_type(**model_value)) |
| 209 | + constructed_model = _construct_model( |
| 210 | + model_type, model_value, constructed_models |
| 211 | + ) |
| 212 | + queryset._result_cache.append(constructed_model) |
207 | 213 |
|
208 | 214 | return queryset |
| 215 | + |
| 216 | + |
| 217 | +def _construct_model(model_type, model_data: Dict): |
| 218 | + if not model_data: |
| 219 | + return None |
| 220 | + |
| 221 | + model = model_type() |
| 222 | + |
| 223 | + for field_name in model_data.keys(): |
| 224 | + for field in model._meta.fields: |
| 225 | + if field.name == field_name or (field_name == "pk" and field.primary_key): |
| 226 | + # if field.is_relation: |
| 227 | + # related_model = _construct_model( |
| 228 | + # field.model, model_data[field_name] |
| 229 | + # ) |
| 230 | + # setattr(model, field.name, related_model) |
| 231 | + # else: |
| 232 | + |
| 233 | + setattr(model, field.name, model_data[field_name]) |
| 234 | + break |
| 235 | + |
| 236 | + return model |
0 commit comments