2017-06-19 59 views
0

我有一个Django模型请求,它有一个字段Approver,它被设置为具有超级用户状态的用户。我想自动为该字段赋值,以便在项目管理员之间轮换。欢迎任何建议。提前致谢。超级用户Django模型外键

+1

欢迎来到StackOverflow。请花时间在[如何问一个聪明的问题](https://meta.stackexchange.com/questions/18584/how-to-ask-a-smart-question)上阅读这篇文章,以及如何提供一个[最小,完整,可验证的例子](https://stackoverflow.com/help/mcve)并相应地修改你的问题。 [如何提出一个好问题](https://stackoverflow.com/help/how-to-ask)上的这些提示也可能有用。 – Jeril

+0

你可以使用你的模型实例的id,并除以你的超级用户使用%来获得余数来选择一个。 –

+0

我想我们可能需要更多的想法。可以做的是将其分配给分配给它们的请求数最少的管理员。但总的来说,这个问题需要更多的信息 –

回答

0

我在Admin窗体中编写了一个函数,该函数将批准者设置为请求数量最少的用户。对我来说工作得很好。

def get_recipient(self): 

    approvers = User.objects.annotate(num_of_requests=models.Count('model_field_related_name')).order_by('num_of_requests') 
    return approvers.first() 
1

我想到的一个简单的解决方案就是在使用请求模型的html模板中,您可以执行评估可能的已签名用户的函数..类似于: 1.在请求中添加一个字段模型,该模型是一个外键您的用户模型 实施例:

class Request(models.Model): 
some other code... 
    assigned_user=models.ForeignKey(User, on_delete=models.CASCADE) 

而用户模型添加asigned布尔:

class User(models.Model): 
    assigned=models.BooleanField(default=False) 
  • 然后在HTML
  • 你可以这样做:

    {% for user in Users %} 
        {% if user.isAdmin %} 
         {% if not user.assigned %} 
          <input name="request.varName">{{User.id}}</input> 
         {% endif %} 
        {% endif %} 
    {% endfor %} 
    

    请记住,在你看来,你必须打电话请求模型和用户模型。您可以通过这种方式实现这一目标创造一个请求的代码:

    class RequestCreate(CreateView): 
        ... some other code.. 
         def get_context_data(self, **kwargs): 
          context = super(RequestCreate, self).get_context_data(**kwargs) 
          context['Users'] = User.objects.all() 
          #context['venue_list'] = Venue.objects.all() 
          #context['festival_list'] = Festival.objects.all() 
          # And so on for more models 
        return context 
    

    我希望在这里有用。

    0

    您可以将函数作为默认值传递。定义一个函数,像这样

    def default_func(): 
        last_assigned = Request.objects.latest().Approver # gets latest assigned Approver 
        # the below if statement always looks for the next superuser pk 
        if User.objects.filter(is_superuser=True, pk__gt=last_assigned.pk).exists(): 
         next_assigned_superuser = User.objects.filter(is_superuser=True, pk__gt=last_assigned.pk)\ 
          .order_by('pk')[0] 
        # if there isn't a superuser with a higher pk than the last_assigned_superuser, it will choose 
        # the superuser below or equal to it with the lowest pk. This will handle the case where there is 
        # only one superuser without any fuss. 
        else: 
         next_assigned_superuser = User.objects.filter(is_superuser=True, pk__lte=last_assigned.pk) \ 
          .order_by('pk')[0] 
    
        return next_assigned_superuser 
    

    ,并添加到您的审批领域:

    # you must pass the function without parenthesis at the end, or else it will 
    # set the default to whatever the value is at server run time. 
    # If you pass the function itself, it will be evaluated every time a 
    # default is needed. 
    Approver = models.ForeignKey(User, default=default_func) 
    

    编辑:增加了一个返回值default_func。哎呦。