[英]Django model foreign key to Superuser
I have a django model Request which has a field Approver which is set to the User which has the superuser status. 我有一个Django模型Request,它的字段Approver设置为具有超级用户状态的User。 I would like to assign value to this field automatically such that it rotates among the project admins.
我想自动为该字段分配值,以使其在项目管理员之间轮换。 Any suggestions are welcome.
欢迎任何建议。 Thanks in advance.
提前致谢。
A easy solution that comes to my mind is just in the html template that uses the request model, you can do a function that evaluates a possible asigned user.. something like that: 1. Add a field in your request model that is a foreign key to your user model Example: 我想到的一个简单解决方案就是使用请求模型的html模板中,您可以执行一个函数来评估可能的分配用户..像这样:1.在请求模型中添加一个外部字段用户模型的关键示例:
class Request(models.Model):
some other code...
assigned_user=models.ForeignKey(User, on_delete=models.CASCADE)
And add an asigned boolean in your User Model: 并在用户模型中添加一个赋值布尔值:
class User(models.Model):
assigned=models.BooleanField(default=False)
You can do something like: 您可以执行以下操作:
{% for user in Users %}
{% if user.isAdmin %}
{% if not user.assigned %}
<input name="request.varName">{{User.id}}</input>
{% endif %}
{% endif %}
{% endfor %}
Remember that in your view you have to call the request model and your Users model.. You can achieve this in this way: 请记住,您必须在视图中调用请求模型和用户模型。您可以通过以下方式实现此目的:
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
I hope to be usefull here. 我希望在这里有用。
You can pass functions as defaults. 您可以将函数作为默认值传递。 Define a function like so
像这样定义一个函数
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
and add this to your Approver field: 并将其添加到您的审批者字段中:
# 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)
Edit: Added a return value to default_func. 编辑:将返回值添加到default_func。 Whoops.
哎呀
I wrote a function in the Admin form which sets the approver to the User which has the least ammount of requests. 我在Admin表单中编写了一个函数,该函数将批准者设置为请求数量最少的User。 Works fine for me.
对我来说很好。
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.