石墨烯的通用创建模型变异

时间:2019-12-16 21:58:28

标签: python graphql graphene-python graphene-sqlalchemy

我正在尝试为烧瓶应用在石墨烯中创建某种通用的create突变。为了创建突变,语法通常如下:

class RouteDateView(ListView):
   model = Tblsummary
   template_name = 'reviewrouteassess/selectdaterange.html'

   def get_context_data(self, *args, **kwargs):
       context = super(RouteDateView, self).get_context_data(*args, **kwargs)

       context['allroute'] = Tblsummary.objects.filter(off_no=self.request.GET.get('depotcode')[:4])

       for p in context['allroute']:
          context[p.res_route] = Tblsummary.objects.filter(res_route=p.res_route)

       return context    

我想创建某种通用的create变异类。为此,我需要动态创建Arguments类,然后将其传递给mutate。我已经知道我可以使用<table> {% for a in allroute %} <tr> <td>{{ a.res_route }}</td> <td>{{ a.hcalls }}</td> </tr> {% for d in a.res_route %} <tr> <td>{{ d.res_route }}</td> <td>{{ d.hcalls }}</td> </tr> {% endfor %} {% endfor %} 从sqlalchemy模型中获取突变所需的属性,但是我很难在给定这些列的情况下弄清楚如何创建Arguments类。

1 个答案:

答案 0 :(得分:1)

尝试一下:

def create_class(args: dict[str, str]):
    class Arguments: pass
    for arg in args:
        setattr(Arguments, arg, args[arg])
    return Arguments

x = create_class({'thing': '100'}); assert x.thing == '100';```