我们有这个模型,它对用户有一个外键:
class Medic(models.Model):
user = models.ForeignKey(User)
first_name = models.CharField(max_length=100, blank=True)
middle_name = models.CharField(max_length=100, blank=True)
last_name = models.CharField(max_length=100, blank=True)
在 admin.py 中,我们尝试将 Medic 页面上显示的已注册用户过滤为仅属于名为“staff”的组的用户:
from django.contrib import admin
from django.contrib.auth.models import User
from profiles.models import Medic
class StaffMed(admin.StackedInline):
model = User
def queryset(self, request):
qs = super(StaffMed, self).queryset(request).filter(group__name='staff')
return qs
class MedicAdmin(admin.ModelAdmin):
inlines = [StaffMed]
#model = Medic
admin.site.register(Medic, MedicAdmin)
然后我们得到这个异常:
class 'django.contrib.auth.models.User' has no foreignKey to class 'staff.models.Medic'