我有一个Django模型请求,它有一个字段Approver,它被设置为具有超级用户状态的用户。我想自动为该字段赋值,以便在项目管理员之间轮换。欢迎任何建议。提前致谢。超级用户Django模型外键
0
A
回答
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。哎呦。
相关问题
- 1. MySQL和Django外键到用户模型
- 2. django用户模型外键公司
- 3. Django的 - 外键的用户模型
- 4. 外键Django模型
- 5. Django - 意外删除超级用户
- 6. Django自定义用户和超级用户模型
- 7. Django多个外键模型
- 8. Django模型ManyToMany和外键
- 9. Django模型外键过滤
- 10. django模型外键问题
- 11. Django REST组外键模型
- 12. Django模型删除外键
- 13. Django模型,循环外键
- 14. Django的模型 - 外键作为主键
- 15. 2 django模型主键和外键
- 16. 用外键保存django orm模型
- 17. 用django中的外键保存模型
- 18. Django:使用多个外键模型
- 19. 如何在Django用户模型上创建自引用外键?
- 20. 相同类型的Django模型外键
- 21. 使用与默认用户模型具有外键关系的Django模型
- 22. 不能在Django创建自定义的用户模型超级用户1.5
- 23. Django admin - 模型对超级用户可见,而非员工用户
- 24. Django管理:模型没有显示,如果用户不是超级用户
- 25. 的Django无法扩展用户模型和创建超级用户后登录
- 26. django上传到图像与用户的外键模型
- 27. Django,在模型外键中按组筛选用户
- 28. 的Django:指定当前用户为外键意见模型
- 29. 如何在django中解析外键关系到用户模型
- 30. 在Django制作外键的用户模型扩展所需
欢迎来到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
你可以使用你的模型实例的id,并除以你的超级用户使用%来获得余数来选择一个。 –
我想我们可能需要更多的想法。可以做的是将其分配给分配给它们的请求数最少的管理员。但总的来说,这个问题需要更多的信息 –