简体   繁体   English

Django admin代理模型编辑表单,仅在表单级别进行预过滤

[英]Django admin proxy model edit form, prefilter on the form level only

I made a proxy model for these two models : 我为这两个模型制作了代理模型:

Class Student(models.Model):
   name:
   is_special :
   foo_n :
   assigned_teacher (foreign_key to Teacher model) :

and : 和:

Class Teacher(models.Model):
   name:
   has_certification :
   foo_n :

I made a proxy model to manage the students because one of my user only needs a list with a few fields to magage special students. 我创建了一个代理模型来管理学生,因为我的一个用户只需要一个包含几个字段的列表即可管理特殊学生。

 Class ManageSpecialStudent(Student):
      class Meta:
         proxy = True

The idea is to have a list of all the special students and a list of all the teachers that have a certification. 这个想法是列出所有特殊学生的清单和所有拥有证书的教师的清单。

Then in my admin.py I made an admin class for that proxy model 然后在admin.py中,为该代理模型创建了一个管理类

class ManageSpecialStudentAdmin(admin.ModelAdmin):
       list_display = ('name','foo','assigned_teacher','is_special',...)
       fields = ('name','foo','assigned_teacher',...)

But I want to filter the available assigned_teacher depending on a value on the teacher model. 但是我想根据教师模型上的值来过滤可用的Assigned_teacher。 If the teacher has the certification to teach to a special kid, show him in the list of the admin form of that proxy model. 如果教师具有可以教给特殊孩子的证书,请在该代理模型的管理表单列表中显示他。

What I did is 我所做的是

 def get_queryset(self, request):
          return self.model.objects.filter(assigned_teacher__has_certification=True, is_special=True)

And it kinda of works, the problem is that query affects also the list_display. 这有点奏效,问题在于查询也会影响list_display。 All the students who don't have a teacher assigned are filtered out, and I don't want this. 所有没有指派老师的学生都被过滤掉,我不希望这样。

Is there a way just to apply the filter to assigned_teacher only on the form(fields) of that proxy ? 有没有办法仅将过滤器应用于该代理的表单(字段)上的assigned_teacher?

Thanks 谢谢

I managed to do it with the following code : 我设法做到以下代码:

def get_queryset(self, request):
    return self.model.objects.filter(is_special=True,)

def render_change_form(self, request, context, *args, **kwargs):
     context['adminform'].form.fields['assigned_teacher'].queryset = Teacher.objects.filter(has_certification=True)
     return super(ManageSpecialStudent, self).render_change_form(request, context, *args, **kwargs)

An easier solution at the admin level, rather than modifying the models (when you have proxy admin models, not proxy models) is to do the following: 在管理员级别,比修改模型(当您具有代理管理模型,而不是代理模型)更简单的解决方案是执行以下操作:

class ManageSpecialStudentAdmin(admin.ModelAdmin):
    list_display = ('name','foo','assigned_teacher','is_special',...)
    fields = ('name','foo','assigned_teacher',...)

    def get_queryset(self, request):
        qs = super().get_queryset(request)
        return qs.filter(assigned_teacher__has_certification=True, is_special=True)

For more information about using django admin as above please check out this . 有关如上使用django admin的更多信息,请查看this

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM