0

我们有这个模型,它对用户有一个外键:

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'
4

1 回答 1

0

您必须指定具有外键的模型:

class StaffMed(admin.StackedInline):
    model = MedicAdmin

然后你需要像这样过滤:

    def queryset(self, request):
         qs = super(StaffMed, self).queryset(request).filter(user.group__name='staff')
         return qs
于 2012-05-16T01:19:37.740 回答