form_list = WorkGroupForm.objects.filter(**params).values('id','name','update_time','is_active').order_by('name') ,order_by 排序不生效的原因
时间: 2023-05-20 08:02:54 浏览: 41
可能是因为参数 params 中没有包含用于排序的字段,或者是该字段的数据类型不支持排序。您可以检查一下参数 params 中是否包含了正确的排序字段,并且该字段的数据类型是否支持排序。另外,您也可以尝试使用其他排序方法来解决这个问题。
相关问题
def staff_detail(request, nid): """ 员工个人详情页面 """ staff_info = models.StaffInfo.objects.filter(RES_ID=nid).first() emergency_info = models.Emergency.objects.filter(res_id=nid).first() print(emergency_info) print(staff_info) StaffForm = StaffStaticModelForm(instance=staff_info) EmergencyForm = EmergencyStaticModelForm(instance=emergency_info) return render(request, "staff_detail.html", {"StaffForm": StaffForm, "EmergencyForm": EmergencyForm})这段代码目前能显示一条与StaffInfo关联的Emergency数据,如何得到所有关联的Emergency?
可以使用`ForeignKey`的`related_name`属性来获取所有关联的Emergency数据。在`StaffInfo`模型中,可以将`Emergency`模型的外键设置为`staff_info`,并给它设置一个`related_name`属性。然后在查询`StaffInfo`对象时,可以通过该属性来获取所有关联的`Emergency`对象。
具体实现代码如下:
```python
class StaffInfo(models.Model):
# ...
class Emergency(models.Model):
staff_info = models.ForeignKey(StaffInfo, related_name='emergency_list', on_delete=models.CASCADE)
# ...
def staff_detail(request, nid):
staff_info = models.StaffInfo.objects.filter(RES_ID=nid).first()
emergency_list = staff_info.emergency_list.all()
StaffForm = StaffStaticModelForm(instance=staff_info)
EmergencyFormset = modelformset_factory(Emergency, form=EmergencyStaticModelForm, extra=0)
EmergencyFormset = EmergencyFormset(queryset=emergency_list)
return render(request, "staff_detail.html", {"StaffForm": StaffForm, "EmergencyFormset": EmergencyFormset})
```
在`Emergency`模型中,设置`staff_info`为`StaffInfo`模型的外键,并设置`related_name`为`emergency_list`。在`staff_detail`视图函数中,使用`emergency_list`变量来获取所有关联的`Emergency`对象,并将其传递给`EmergencyFormset`。然后在模板中使用`{{ EmergencyFormset }}`来渲染所有的`Emergency`表单。
SELECT experiment_data_form_data.*, experiment_data_form.field from experiment_data_form_data LEFT JOIN experiment_data_form on experiment_data_form_data.form_id = experiment_data_form.id where experiment_data_form_data.process_id = #{id} 这里传参为null
如果你在这里传入 null,那么 SQL 查询语句会在 `where` 子句中使用 `experiment_data_form_data.process_id = null` 进行过滤,这将会返回空结果集。如果你想查询所有 `experiment_data_form_data` 表中的数据,可以考虑使用 `where experiment_data_form_data.process_id is not null` 来代替。如果你想查询特定的 `experiment_data_form_data.process_id`,则需要传入一个非空的参数值。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)