Python Django:无法将关键字解析为字段。选择包括:

Python Django:无法将关键字解析为字段。选择包括:,python,django,Python,Django,我知道这里有很多帖子都有类似的问题,但是没有一个能帮到我 当我试图从管理面板访问专门化时,出现了这个错误。在我在doctor实体中添加sub_专门化并将专门化设置为外键之后,它就开始发生了 回溯: 25. return func(self, *args2, **kwargs2) File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changelist_view

我知道这里有很多帖子都有类似的问题,但是没有一个能帮到我

当我试图从管理面板访问专门化时,出现了这个错误。在我在doctor实体中添加sub_专门化并将专门化设置为外键之后,它就开始发生了

回溯:

  25.                 return func(self, *args2, **kwargs2)
File "/Library/Python/2.7/site-packages/django/contrib/admin/options.py" in changelist_view
  1306.                 self)
File "/Library/Python/2.7/site-packages/django/contrib/admin/views/main.py" in __init__
  74.         self.root_queryset = model_admin.get_queryset(request)
File "/Users/harisaghadi/code/meddy Production App/m1/admin.py" in get_queryset
  50.         return qs.annotate(doctor_count=Count('doctor')).order_by('-doctor_count')
File "/Library/Python/2.7/site-packages/django/db/models/query.py" in annotate
  717.                 is_summary=False)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in add_aggregate
  988.                 field_list, opts, self.get_initial_alias())
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in setup_joins
  1363.             names, opts, allow_many, allow_explicit_fk)
File "/Library/Python/2.7/site-packages/django/db/models/sql/query.py" in names_to_path
  1283.                                      "Choices are: %s" % (name, ", ".join(available)))

Exception Type: FieldError at /admin/m1/specialization/
Exception Value: Cannot resolve keyword 'doctor' into field. Choices are: doctor_specialization, doctor_subSpecialization, id, meta, name, page_name
这是admin.py

    class SpecializationAdmin(admin.ModelAdmin):

        formfield_overrides = {
            models.CharField: {'widget': TextInput(attrs={'size':'200'})},
            models.TextField: {'widget': Textarea(attrs={'rows':4, 'cols':40})},

        }

        list_display = ['name','doctor_count_display', 'meta', 'page_name']

        def get_queryset(self, request):
            qs = super(SpecializationAdmin, self).get_queryset(request)
            return qs.annotate(doctor_count=Count('doctor')).order_by('-doctor_count')

        def doctor_count_display(self, obj):
            return obj.doctor_count
        doctor_count_display.short_description = 'Number of doctors'
        doctor_count_display.admin_order_field = 'doctor_count'

admin.site.register(Specialization, SpecializationAdmin)
Models.py

class Specialization(models.Model):
    name = models.CharField(max_length=30)
    meta = models.CharField(max_length=160, blank = True, null = True)
    page_name = models.CharField(max_length=55, blank = True, null = True)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ('name',)

class Doctor(models.Model):
    name = models.CharField(max_length=1300)
    specialization = models.ForeignKey(Specialization, related_name = "doctor_specialization")
    sub_specialization = models.ForeignKey(Specialization, related_name = "doctor_subSpecialization", null = True, blank = True)
    clinic = models.ForeignKey(Clinic)

您不再有
医生
字段;因为您给了它一个自定义的
相关名称
;所以这个
qs.annotate(doctor\u count=count('doctor'))
不起作用。谢谢@BurhanKhalid!您可以添加它作为答案,我可以勾选它。正如@BurhanKhalid所说的,将这一行更改为qs.annotate(doctor\u count=count('doctor\u specialization'))。order\u by('-doctor\u count')和一点未来提示:样式指南说您不应该在模型字段定义(和函数等)中使用
=
周围的空格:
ForeignKey(blank=True,null=True)
你不再有
doctor
字段了;因为你给了它一个自定义的
相关的\u名称
;所以这个
qs.annotate(doctor\u count=count('doctor'))
不起作用。谢谢@BurhanKhalid!你可以添加它作为答案,我可以勾选它。正如@BurhanKhalid所说的,将这一行改为qs.annotate(doctor_count=count('doctor_specialization'))。order_by('-doctor_count')和未来的一点提示:样式指南规定,在模型字段定义(和函数等)中不应在
=
周围使用空格:
ForeignKey(blank=True,null=True)
你不再有
医生
字段;因为你给了它一个自定义的
相关的\u名称
;所以这个
qs.annotate(doctor\u count=count('doctor'))
不起作用了。谢谢@BurhanKhalid!你可以把它作为一个答案添加,我可以勾选它。正如@BurhanKhalid所说的,把这行改为qs.annotate(doctor\u count=count)('doctor_specialization'))。按('-doctor_count')排序和一点未来提示:样式指南规定,在模型字段定义(和函数等)中不应在
=
周围使用空格:
ForeignKey(blank=True,null=True)