_isAdmin__ = None allModels = apps.get_app_config('main').get_models() for m in allModels: if m.__tablename__==tablename: try: __isAdmin__ = m.__isAdmin__ except: __isAdmin__ = None break
时间: 2024-02-15 16:28:12 浏览: 28
这是一个 Python 代码片段,主要用于获取指定表名的模型对象,并从中获取一个名为 `__isAdmin__` 的属性值。该代码片段包括以下几个步骤:
1. 通过 `apps.get_app_config()` 方法获取应用程序 `main` 的配置对象;
2. 调用 `get_models()` 方法获取应用程序 `main` 中注册的所有模型对象;
3. 遍历所有模型对象,查找表名与指定表名相同的模型对象;
4. 如果找到了指定表名的模型对象,则尝试获取其 `__isAdmin__` 属性值;
5. 如果获取成功,则将其赋值给变量 `__isAdmin__`,并跳出循环;
6. 如果获取失败,则将变量 `__isAdmin__` 的值设置为 `None`。
需要注意的是,代码中的 `__tablename__` 和 `__isAdmin__` 可能是模型对象的属性或方法,具体含义需要参考代码中的定义。
相关问题
def huiyuanchongzhi_save(request): ''' 后台新增 ''' if request.method in ["POST", "GET"]: msg = {"code": normal_code, "msg": mes.normal_code, "data": {}} req_dict = request.session.get("req_dict") if 'clicktime' in req_dict.keys(): del req_dict['clicktime'] tablename=request.session.get("tablename") __isAdmin__ = None allModels = apps.get_app_config('main').get_models() for m in allModels: if m.__tablename__==tablename: try: __isAdmin__ = m.__isAdmin__ except: __isAdmin__ = None break #获取全部列名 columns= huiyuanchongzhi.getallcolumn( huiyuanchongzhi, huiyuanchongzhi) if tablename!='users' and req_dict.get("userid")!=None and 'userid' in columns and __isAdmin__!='是': params=request.session.get("params") req_dict['userid']=params.get('id') if 'addtime' in req_dict.keys(): del req_dict['addtime'] error= huiyuanchongzhi.createbyreq(huiyuanchongzhi,huiyuanchongzhi, req_dict) if error!=None: msg['code'] = crud_error_code msg['msg'] = error return JsonResponse(msg)
这段代码是一个 Django 后台新增功能的视图函数。它接收一个 HTTP 请求,如果请求方法是 POST 或 GET,就会创建一个空的字典 msg 作为响应,然后从 session 中获取请求参数 req_dict 和表名 tablename。接下来,通过 Django 的 apps.get_app_config 函数获取当前应用程序中的所有模型,再通过循环找到与 tablename 对应的模型,并从模型中获取是否为管理员的信息 __isAdmin__。然后,通过调用 huiyuanchongzhi 模型的 getallcolumn 函数获取该模型的所有列名,并检查当前用户是否为管理员或者该表不存在用户id列。如果不是管理员或不存在用户id列,则从 session 中获取参数 params 并将其添加到请求字典中。最后,它调用 huiyuanchongzhi 模型的 createbyreq 函数将请求字典中的数据保存到数据库中,并将保存结果添加到响应字典中并返回。
dataInfo = new DeviceManage.设备采购(); foreach (DataGridViewRow row in dataGridView1.Rows) { if (row.Selected == true) { if (isadmin == "1") { if (row.Cells[0].Value == null) { dataInfo.buy_no = cgdh.Text = dataGridView1.Rows[i].Cells[2].Value.ToString(); dataInfo.login_no = yhbh.Text = dataGridView1.Rows[i].Cells[3].Value.ToString(); dataInfo.dev_name = xbmc.Text = dataGridView1.Rows[i].Cells[4].Value.ToString(); dataInfo.dev_model = xbxh.Text = dataGridView1.Rows[i].Cells[5].Value.ToString(); dataInfo.gys_name = gys.Text = dataGridView1.Rows[i].Cells[6].Value.ToString(); dataInfo.buy_date = dateTimePicker1.Text = dataGridView1.Rows[i].Cells[7].Value.ToString(); dataInfo.price = dj.Text = dataGridView1.Rows[i].Cells[8].Value.ToString(); dataInfo.num = sl.Text = dataGridView1.Rows[i].Cells[9].Value.ToString(); }
将上述代码中的 `for` 循环改为 `foreach` 循环,并将代码中的索引变量 `i` 替换为 `row.Index`,可以使用以下代码:
```csharp
dataInfo = new DeviceManage.设备采购();
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if (row.Selected == true)
{
if (isadmin == "1")
{
if (row.Cells[0].Value == null)
{
dataInfo.buy_no = cgdh.Text = row.Cells[2].Value.ToString();
dataInfo.login_no = yhbh.Text = row.Cells[3].Value.ToString();
dataInfo.dev_name = xbmc.Text = row.Cells[4].Value.ToString();
dataInfo.dev_model = xbxh.Text = row.Cells[5].Value.ToString();
dataInfo.gys_name = gys.Text = row.Cells[6].Value.ToString();
dataInfo.buy_date = dateTimePicker1.Text = row.Cells[7].Value.ToString();
dataInfo.price = dj.Text = row.Cells[8].Value.ToString();
dataInfo.num = sl.Text = row.Cells[9].Value.ToString();
}
}
}
}
```
在 `foreach` 循环中,`row` 直接表示集合中的每个元素,可以直接使用 `row.Cells[0]` 访问该行中的第一列单元格。使用 `row.Index` 可以获取当前行的索引。注意,`foreach` 循环中的变量是只读的,不能修改它们的值,因此需要使用 `row.Index` 来代替原来的索引变量 `i`。