Skip to content

Commit 798ac97

Browse files
Hongbin Huangfreemindcore
authored andcommitted
feat: Model is now optional for initializing a BaseService
1 parent ced7e9c commit 798ac97

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

easy/domain/orm.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,20 @@
1212

1313

1414
class DjangoOrmModel(CrudModel):
15-
def __init__(self, model: Type[models.Model]):
15+
def __init__(self, model: Optional[Type[models.Model]] = None) -> None:
1616
self.model = model
17-
config = ModelMetaConfig()
18-
exclude_list = config.get_final_excluded_list(self.model())
19-
self.m2m_fields_list: List = list(
20-
_field
21-
for _field in self.model._meta.get_fields(include_hidden=True)
22-
if (
23-
isinstance(_field, models.ManyToManyField)
24-
and ((_field not in exclude_list) if exclude_list else True)
17+
if self.model:
18+
config = ModelMetaConfig()
19+
exclude_list = config.get_final_excluded_list(self.model())
20+
self.m2m_fields_list: List = list(
21+
_field
22+
for _field in self.model._meta.get_fields(include_hidden=True)
23+
if (
24+
isinstance(_field, models.ManyToManyField)
25+
and ((_field not in exclude_list) if exclude_list else True)
26+
)
2527
)
26-
)
27-
super().__init__(self.model)
28+
super().__init__(self.model)
2829

2930
def _separate_payload(self, payload: Dict) -> Tuple[Dict, Dict]:
3031
m2m_fields = {}

easy/services/base.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Type
2+
from typing import Optional, Type
33

44
from django.db import models
55

@@ -10,9 +10,6 @@
1010

1111

1212
class BaseService(CrudService, PermissionService):
13-
def __init__(
14-
self,
15-
model: Type[models.Model],
16-
):
13+
def __init__(self, model: Optional[Type[models.Model]] = None):
1714
self.model = model
1815
super().__init__(model=self.model)

easy/services/crud.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import logging
2-
from typing import Any, Type
2+
from typing import Any, Optional, Type
33

44
from asgiref.sync import sync_to_async
55
from django.db import models
@@ -10,7 +10,7 @@
1010

1111

1212
class CrudService(DjangoOrmModel):
13-
def __init__(self, model: Type[models.Model]):
13+
def __init__(self, model: Optional[Type[models.Model]] = None):
1414
super().__init__(model)
1515
self.model = model
1616

0 commit comments

Comments
 (0)