How to develop a scheme of "private ModelAdmin.list_filter"

9 views
Skip to first unread message

郑宇恒

unread,
Mar 6, 2016, 1:09:08 PM3/6/16
to Django users
Hi,

I was developing a experiment data management site adopting "django.contrib.admin". For example I have the "Carbon" model with a foreign key "author"

******************************************************
from django.db import models
from django.contrib.auth import models as auth_mod
class Carbon(models.Model):
    sn = models.CharField(max_length=30)
    source = models.CharField(max_length=30)
    pyrolysis_temperature = models.IntegerField(default=1400)
    pyrolysis_duration = models.IntegerField(default=2)
    comments = models.TextField(default='Leave your comments here')
    status = models.CharField(max_length=1, choices=STATUS_CHOICES,default='a')
    pub_date = models.DateTimeField('date produced', auto_now=True)
    author = models.ForeignKey(auth_mod.User, default=1)
    def __str__(self):
        return self.sn
********************************************************

and I write this in "carbon/admin.py"

********************************************************
class CarbonAdmin(admin.ModelAdmin):
    list_display = ('sn', 'source','pub_date','status')
    list_filter =  ('source','status','pub_date',)
   
    exclude = ('author',)
    def save_model(self, request, obj, form, change):
        obj.author = request.user
        obj.save()
    def get_queryset(self, request):
        qs = super(CarbonAdmin, self).get_queryset(request)
        if request.user.is_superuser:
            return qs
        return qs.filter(author=request.user)
********************************************************
to achieve an "objective permission" to filter the carbons created by some specific author, 
but this method failed to control the list_filter() result.

The list_filter() always feedback a list of the global options, other than the options only created by the author.

How could I solve this by adding some filter before the list_filter() returning the global options?







Reply all
Reply to author
Forward
0 new messages