我知道这里有很多帖子有一个非常相似的问题,但没有一个帮助过我。
当我尝试从管理面板访问专业化时,我收到此错误。它在我在医生实体中添加sub_specialization并使专业化成为外键之后就开始发生了。
回溯:
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)