Django search_field

时间:2019-11-27 07:44:19

标签: django

如何解决此错误?原因 每当我搜索学生用户时,我都会收到一个错误, enter image description here

错误

enter image description here

admin.py

@admin.register(StudentsEnrollmentRecord)
class StudentsEnrollmentRecordAdmin(admin.ModelAdmin):
    #inlines = [InLineSubject]
    list_display = ('lrn', 'Student_Users', 'Education_Levels', 'Courses', 'Section', 'Payment_Type', 'Discount_Type' ,'School_Year')
    #list_select_related = ('Student_Users')
    ordering = ('Education_Levels','Student_Users__lrn')
    list_filter = ('Student_Users','Education_Levels','Section','Payment_Type')
    search_fields = ('Student_Users',)


    def lrn(self, obj):
        return  obj.Student_Users.lrn

我的模型。py

class StudentsEnrollmentRecord(models.Model):
    Student_Users = models.ForeignKey(StudentProfile, related_name='students', on_delete=models.CASCADE,null=True)
    School_Year = models.ForeignKey(SchoolYear, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
    Courses = models.ForeignKey(Course, related_name='+', on_delete=models.CASCADE, null=True, blank=True)
    Section = models.ForeignKey(Section, related_name='+', on_delete=models.CASCADE, null=True,blank=True)
    Payment_Type = models.ForeignKey(PaymentType, related_name='+', on_delete=models.CASCADE, null=True)
    Discount_Type = models.ForeignKey(Discount, related_name='+', on_delete=models.CASCADE, null=True,blank=True)
    Education_Levels = models.ForeignKey(EducationLevel, related_name='+', on_delete=models.CASCADE,blank=True,null=True)

更新模型

class StudentProfile(models.Model):
   lrn = models.CharField(max_length=500,null=True)
   Firstname = models.CharField(max_length=500,null=True,blank=True)
   Middle_Initial = models.CharField(max_length=500,null=True,blank=True)
   Lastname = models.CharField(max_length=500,null=True,blank=True)
   Education_Levels= models.ForeignKey(EducationLevel, on_delete=models.CASCADE,blank=True,null=True)

1 个答案:

答案 0 :(得分:2)

您需要提供StudentProfile中的特定字段-当前您的搜索字段是

search_fields = ('Student_Users',)

表示仅模型本身。您没有发布StudentProfile的架构,但是例如,如果其中包含Lastname字段,则应这样使用它:

search_fields = ('Student_Users__Lastname',)

要包括多个字段,您可以

search_fields = ('Student_Users__Lastname', 'Student_Users__Firstname',)

您也可以这样做

search_fields = ('=Student_Users__Lastname',)

要与姓氏“完全匹配”,前面的示例检查字段是否包含查询字符串