添加元素而不定义模型选项字段

时间:2019-04-02 03:09:09

标签: python django

我在Django中具有以下形式,可以在过滤器中使用它

class TeachersForm (forms.Form):
     teachers = forms.ModelChoiceField (
        required = False,
        queryset = Teachers.objects.all (),
        label = "Teacher"
     )

模范老师

class Docente(model.Models):
    name= model.CharField(max_length=30)
    last_name= model.CharField(max_length=30)

模范学生

class Student (model.Models):
    name= model.CharField(max_length=30)
    last_name= model.CharField(max_length=30)
    teacher = models.ForeignKey(Teacher, null=False, blank=False)

我需要一个选项来过滤尚未分配任何老师的学生,但是查询集会向我返回存在的老师对象。

我要说的是向学生展示指派的老师等于“未指派的”。

“ Unassigned”必须是Django返回None的过滤器选项

例如:

  • 未分配
  • Pepito
  • 富拉诺
  • 苏丹诺

2 个答案:

答案 0 :(得分:0)

import React from 'react';

const withClass = (WrappedComponent,className) => {
   return props => (
     <div className={className}>
      <WrappedComponent/>
     </div>
   );
};

export default withClass;

答案 1 :(得分:0)

You have made the teacher foreign key as null = False

Model Student 

class Student (model.Models):
    name= model.CharField(max_length=30)
    last_name= model.CharField(max_length=30)
    teacher = models.ForeignKey(Teacher, null=True, blank=True, on_delete=models.PROTECT,)

To get the students who have not assigned any teacher by :
     student_list = Student.objects.filter(teacher__is_null = True)