您可以合并权限吗?

时间:2019-04-26 18:55:57

标签: python django

我需要知道在Django Python项目中是否可以合并两个权限?我的老板要我将clock_in和clock_out权限合并为一个名为clock_in_out的权限。我还想只授予一个权限,使其能够包含添加,更改,查看和删除项目,而不是将单独的项目分配给管理员。

我尝试将元数据添加到模型类中,并在views.py文件中的适用def之前添加@permission_required('manager.manage_project')。我是python的新手,所以让我知道这是否可行。我运行了python3 manage.py makemigrations并进行了迁移,但仍然无法正常工作。

models.py

class Entry(models.Model):

   #uses EntryManager for querysets
   objects = EntryManager()
   no_join = models.Manager()

   class Meta:
       db_table = 'timepiece_entry'  # Using legacy table name
       ordering = ('-start_time',)
       verbose_name_plural = 'entries'
       permissions = (
           ('can_clock_in_out', 'Can use Pendulum to clock in and out'),
       )

views.py

@permission_required('entries.can_clock_in_out')
@transaction.atomic
def clock_in(request):
    """For clocking the user into a project."""
    user = request.user
    # Lock the active entry for the duration of this transaction, to prevent
    # creating multiple active entries.
    active_entry = utils.get_active_entry(user, select_for_update=True)

    initial = dict([(k, v) for k, v in request.GET.items()])
    data = request.POST or None
    form = ClockInForm(data, initial=initial, user=user, active=active_entry)
    if form.is_valid():
        entry = form.save()
        message = 'You have clocked into {0}.'.format(
             entry.project)
        messages.info(request, message)
        return HttpResponseRedirect(reverse('dashboard'))

    return render(request, 'timepiece/entry/clock_in.html', {
        'form': form,
        'active': active_entry,
    })

我希望能够为该用户分配can_clock_in_out并使其正常工作。

0 个答案:

没有答案