generated from app-generator/django-soft-ui-dashboard
/
models.py
42 lines (32 loc) · 1.02 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import datetime
from django.db import models
from rest_framework import serializers
import sys
import inspect
class Utils:
@staticmethod
def get_class(config, name: str) -> models.Model:
return Utils.model_name_to_class(config[name])
@staticmethod
def get_manager(config, name: str) -> models.Manager:
return Utils.get_class(config, name).objects
@staticmethod
def get_serializer(config, name: str):
class Serializer(serializers.ModelSerializer):
class Meta:
model = Utils.get_class(config, name)
fields = '__all__'
return Serializer
@staticmethod
def model_name_to_class(name: str):
all_classes = inspect.getmembers(sys.modules[__name__], inspect.isclass)
for cls in all_classes:
if cls[0] == name:
return cls[1]
# we are confident that never returns None
return None
'''
Add your models below
'''
class Book(models.Model):
title = models.CharField(max_length=100)