允许员工用户只读取数据

时间:2017-12-26 12:34:40

标签: django django-models django-admin

我想允许员工用户查看(只读)交易数据。但是当我以员工用户身份登录时,它会显示“您没有权限编辑任何内容。”#39;页面的其余部分是空白的。

models.py

 #models.py
from django.db import models
from django.contrib.auth.models import Permission, User




from django.core.validators import RegexValidator

# Create your models here.
alphanumeric = RegexValidator(r'^[0-9a-zA-Z-]*$', 'Only alphanumeric characters and "-" are allowed.')

TransactionTypes = (
    ('P','Purchase'),
    ('S','Sell'),
    )
class Transactions(models.Model):
    client_name = models.OneToOneField(User(is_staff=True))
    transaction_type = models.CharField(choices=TransactionTypes,max_length=1)
    amount = models.IntegerField()
    transaction_id = models.CharField(max_length=50, blank=True, null=True, validators=[alphanumeric])
    class Meta:
        permissions = (
            ('read_item','Can read item'),
        )

Admin.py -

#admin.py
from django.contrib import admin
from django.contrib.auth.models import User
from .models import Transactions
# Register your models here.


class TransactionAdmin(admin.ModelAdmin):
    #list_display = ('client_name', 'transaction_type', 'amount', 'transaction_id')
    list_display = ('client_name', 'transaction_type', 'amount', 'transaction_id')
    def get_readonly_fields(self, request, obj=None):
        if not request.user.is_superuser and request.user.has_perm('transactionss.read_item'):
            return [f.name for f in self.model._meta.fields]        
        return super(TransactionAdmin, self).get_readonly_fields(
            request, obj=obj
        )

admin.site.register(Transactions, TransactionAdmin)

0 个答案:

没有答案