django admin

1、querySet过滤

 def get_queryset(self, request):
        task = Task.objects.all()
        if request.user.is_superuser:
            return task
        return task.filter(create_by_user=request.user)

2、外键条件过滤

  def formfield_for_foreignkey(self, db_field, request, **kwargs):
        if not request.user.is_superuser:
            if db_field.name == "create_by_user":
                kwargs["queryset"] = User.objects.filter(id=request.user.id)
        return super(TaskAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)

3、修改导出字段

class TaskResource(resources.ModelResource):
    system = Field()
    start_time = Field()

    class Meta:
        model = Task
        fields = ('start_time', 'system', 'content', 'progress', 'create_by_user', 'note')
        export_order = ('start_time', 'system', 'content', 'progress', 'create_by_user', 'note')
        skip_unchanged = True

    def dehydrate_start_time(self, task):
        return timezone.localtime(task.start_time).strftime("%Y/%m/%d")

    def dehydrate_system(self, task):
        return task.system.name

你可能感兴趣的:(django admin)