Django permission

使用django的权限管理系统permission

1、为model添加权限

class OrderList(models.Model):
    id = BigIntegerAutoField(primary_key=True)
    buyer_name = models.CharField(default="",max_length=32, verbose_name=u'买手')
    order_amount = models.FloatField(default=0, verbose_name=u'金额')
    created = models.DateField(auto_now_add=True, verbose_name=u'订货日期')
    updated = models.DateTimeField(auto_now=True, verbose_name=u'更新日期')

    class Meta:
        db_table = 'suplychain_flashsale_orderlist'
        verbose_name = u'**表'
        verbose_name_plural = u'**表'
        permissions = [("change_order_list_inline", u"**"),]

    def __unicode__(self):
        return '<%s,%s,%s>' % (str(self.id or ''), self.id, self.buyer_name)

2、views中可以使用如下方法来操作权限

request.user.has_perm('dinghuo.change_order_list_inline')

3、在template中使用权限方法:

{% if perms.conf %} <p>You have permission to do something in the foo app.</p> <p>You can vote!</p> {% endif %} {% if perms.conf.oprater_task %} <p>You can drive!</p> <p>You don't have permission to do anything in the foo app.</p> {% endif %}

你可能感兴趣的:(django,权限)