非用户的基于用户名的Django管理员过滤器列表过滤器

时间:2018-09-29 14:28:29

标签: django filtering

我正在使用django管理员端,我想使用仅适用于非管理员用户的信息来过滤列表过滤器项。他甚至无法在过滤器端看到其他用户。

有人可以帮我做到这一点吗?

我已经在模型和管理员中尝试过

def get_queryset(self, request):
        if not self.request.user.is_authenticated() or not self.request.user.is_superuser:
            user = request.user
            qs = super(ParkingModelAdmin, self).get_queryset(request)
            return qs.filter(user=request.user)

enter image description here

在这种情况下,登录的用户是elise.cohen,因此她应该只在其中看到自己的详细信息。谢谢!

 list_filter = ["parking_on", "user"]

我的管理员:

class ParkingModelAdmin(admin.ModelAdmin):
    list_display = ["user", "location","parking_on"]
    list_display_links = [ "location"]
    list_editable = [ "parking_on"]
    list_filter = ["parking_on", "user"]
    search_fields = ["location", "parking_on"]
    date_hierarchy = 'parking_on'

    class Meta:
        model = Parking

    def get_form(self, request, obj=None, **kwargs):
        form = super().get_form(request, obj, **kwargs)
        if not obj:
            user = request.user
            form.base_fields['user'].initial = user
            form.base_fields['email'].initial = user.email
        return form

    def get_queryset(self, request):
        if not request.user.is_authenticated() or not request.user.is_superuser:
            user = request.user
            qs = super(ParkingModelAdmin, self).get_queryset(request)
            return qs.filter(user=user)

    def save_model(self, request, obj, form, change):  # add an additional message
        user = request.user
        messages.info(request, "Dear " + str(user)+ " "+ " please note that your parking plot has been reserved")
        super(ParkingModelAdmin, self).save_model(request, obj, form, change)

admin.site.register(Parking, ParkingModelAdmin)

我的模型:

class ParkingManager(models.Manager):
    def active(self, *args, **kwargs):
        return super(ParkingManager, self).filter(draft=False).filter(parking_on__lte=datetime.now())

class Parking(models.Model):
    PARKING_PLOT = (
        ('P1', 'Parking #1'),('P2', 'Parking #2'), ('P3', 'Parking #3'),
        ('P4', 'Parking #4'),('P5', 'Parking #5'), ('P6', 'Parking #6'),
        ('P7', 'Parking #7'),('P8', 'Parking #8'), ('P9', 'Parking #9'),
        ('P10', 'Parking #10'),('P11', 'Parking #11'), ('P12', 'Parking #12'),
        ('P13', 'Parking #13'),('P14', 'Parking #14'), ('P15', 'Parking #15')

    )
    user = models.ForeignKey(settings.AUTH_USER_MODEL,blank=True, null=True, default=1, on_delete=True)
    email = models.EmailField(blank=True, null=True)
    parking_on = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True,help_text='Please select the date you want to come in the office.',)
    parking_off = models.DateField(auto_now=False, auto_now_add=False, blank=True, null=True,help_text='Please select the date when you leave')
    numar_masina = models.CharField(max_length=8, default="IF77WXV", blank=True, null=True,help_text='Please insert your license plate number')
    location = models.CharField(max_length=3, blank=True, default="P1", null=True, choices=PARKING_PLOT,help_text='Please select the desired parking plot.')
    updated = models.DateTimeField(auto_now=True, auto_now_add=False, blank=True, null=True)
    timestamp = models.DateTimeField(auto_now=False, auto_now_add=True, blank=True, null=True)

    objects = ParkingManager()

    def __str__(self):
        return self.location + " | " + str(self.parking_on) + " | " + str(self.parking_off)

1 个答案:

答案 0 :(得分:0)

因此,如果要基于登录的用户限制用户过滤器中的选项,则可以尝试使用自定义filter作为:

class UserFilter(admin.SimpleListFilter):
    title = 'User'
    parameter_name = 'user'

    def lookups(self, request, model_admin):
        user = request.user
        qs = YourUserModel.objects.filter(id=request.user.id)
        return ((obj.id, obj.username) for obj in qs)

    def queryset(self, request, queryset):
        if self.value():
            queryset = queryset.filter(user_id=self.value())
        return queryset

现在,在lookups方法中,可以根据您的用户模型来限制基于登录用户显示的选择。