def to_representation(self, obj): # 获取模型名 # print self.context ob = self.context.get('pk', None) model = modelchoice.get(ob, None) # print ob if not ob or not model: return None # print ob obj.session['model'] = ob # 获取列的数据 order_columns = [f for f in model._meta.fields] # if ob == 'equip': # order_columns = [f for f in modelchoice.get(ob)._meta.fields] t = get_template('table.html') # 获取模板内容 # 获取模板 content_html = t.render({'order_columns': order_columns}, obj) # 渲染模板生成想要的全部局部html内容,而不是某一个变量 columns = [{'data': column.attname} for column in order_columns] columns.append({'data': 'control'}) payload = { 'content_html': content_html, 'columns': columns, 'ob': ob, 'success': True} # 构造json类型数据,以方便前端处理 return payload
def ajax_tem(request): if request.is_ajax(): # 判断request请求是否是Ajax类型的 # 获取模型名 ob = request.GET.get('name') request.session['model'] = ob # 获取列的数据 order_columns = [f for f in modelchoice.get(request.GET.get('name')).objects.first()._meta.fields] t = get_template('table.html') # 获取模板内容 # 获取模板 content_html = t.render({'order_columns': order_columns}) # 渲染模板生成想要的全部局部html内容,而不是某一个变量 columns = [{'data':column.attname }for column in order_columns] payload = { 'content_html': content_html, 'columns': columns, 'ob':ob, 'success': True} # 构造json类型数据,以方便前端处理 return HttpResponse(json.dumps(payload), # 这个地方最好保证用json的方法传送数据,否则会出现意想不到的错误 content_type="application/json") # 用json类型返回数据到前端
def initialize(self, *args, **kwargs): # 获取设备model # 设置查询model self.name = self.request.session.get('model') # 设置状态查询model的id self.model_id = self.request.POST.get('model_id',None) name = self.request.POST.get('model',None) if name != None: self.name = name self.model = modelchoice.get(self.name) # 获取用户公司对应id # self.company_id = self.request.user.company.id # 获取model拥有字段名 self.columns = [f.name for f in self.model._meta.fields]
def initialize(self, *args, **kwargs): self.model = modelchoice.get(self.request.session.get('model')) self.columns = [f.name for f in self.model.objects.first()._meta.fields]