Django - 查找丢失的活动邀请
我需要听取 django 社区的意见。我仍在学习,层抽象是我的主要挑战(与 PHP/SQL 脚本相比)
这是我的模型的摘录:
一个非常简单的接触,但具有“级别”(如基本、黄金等) )
class customer(models.Model):
created_date = models.DateTimeField(auto_now_add = True, editable = False)
modified_date = models.DateTimeField(auto_now = True, editable = False)
surname = models.CharField(max_length=200)
lastname = models.CharField(max_length=200)
fk_level = models.ForeignKey(level, on_delete=models.CASCADE)
[...]
需要邀请客户(任意数量)参加的活动:
class event(models.Model):
created_date = models.DateTimeField(auto_now_add = True, editable = False)
modified_date = models.DateTimeField(auto_now = True, editable = False)
eventdate = models.DateField('Date de la soirée')
fk_customer = models.ManyToManyField(customer, blank=True)
[...]
针对客户的活动邀请。
class invite(models.Model):
created_date = models.DateTimeField(auto_now_add = True, editable = False)
modified_date = models.DateTimeField(auto_now = True, editable = False)
fk_customer = models.ForeignKey(customer, on_delete=models.CASCADE)
fk_event = models.ForeignKey(event, on_delete=models.CASCADE)
[...]
级别的描述,以及根据客户级别发送的邀请数量。
class level(models.Model):
created_date = models.DateTimeField(auto_now_add = True, editable = False)
modified_date = models.DateTimeField(auto_now = True, editable = False)
name = models.CharField(max_length=200)
eventinvites = models.IntegerField(default=2)
[...]
我不想(并且可能不知道如何)在将客户添加到活动后立即创建邀请。因为我需要管理删除客户,还需要管理邀请的删除。
所以我需要的是,对于每个活动,根据客户级别检查我们是否有足够的邀请。
如果“基本”客户没有“邀请”参加活动,我需要列出“eventinvites”(2) 个邀请(一旦知道有多少,我就可以管理创建)。 如果只有 1 个邀请,那么我只需要列出 1 个。如果有超过 1 个邀请,那么我不需要做任何事情。
在标准 python 代码中,我可能会遍历事件,然后对于每个事件以及每个被邀请的客户,遍历邀请并检查我是否有多少个与客户和事件匹配的邀请。如果少于“eventinvites”,我会将事件/客户对添加到“待办事项”列表中。
就像
1 missing_invites = []
2 for event in Events:
3 for invited_customer in (Customers where event.fk_customer):
4 invite_count = 0
5 for invites in (Invites where invite.fk_customer==invited_customer and
6 invite.fk_event == event.id):
7 invite_count += 1
8 if invite_count < event.fk_customer.fk_level.eventinvites:
9 missing_invites += (event, invited_customer)
在 Django 中一样,我可以在上下文中创建包含事件、邀请和客户的视图。 我可以遍历“事件”(第 2 行),然后筛选“客户”(第 3 行),并列出已创建的邀请。
views.py:
class MissingInvitesView(PermissionRequiredMixin, ListView):
model = invite
template_name = 'missing.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["Cards"] = event.objects.all() #because it's easier
to understand
context["Events"] = event.objects.all()
context["Customers"] = customer.objects.all()
和missing.html:
{% for event in Events %}
{% for customer in event.fk_customer.all %}
{% with eventInvite=Invites|matchingEvent:event.id %}
{% for customereventInvite in eventInvite|matchingCustomer:customer.id %}
<br>{{ customereventInvite }}
{%endfor%}
{%endwith%}
{% endfor%}
{%endfor%}
有了这个,我可以获得现有的邀请列表。但我想不出一种方法来计算这些并在丢失时显示一些内容。
我仍在使用通用(类)视图,因为它更容易。我应该开始学习标准(def)视图吗?行得通吗?
非常感谢您的帮助。 杰米
I need to pick the community's brain on django. I'm still learning and the layer abstraction is my major challenge (as compared to a PHP/SQL script)
Here's an extract of my models:
A very simple contact, but with a "level" (like basic, gold, ...)
class customer(models.Model):
created_date = models.DateTimeField(auto_now_add = True, editable = False)
modified_date = models.DateTimeField(auto_now = True, editable = False)
surname = models.CharField(max_length=200)
lastname = models.CharField(max_length=200)
fk_level = models.ForeignKey(level, on_delete=models.CASCADE)
[...]
An event with customers (any number) that need to be invited to:
class event(models.Model):
created_date = models.DateTimeField(auto_now_add = True, editable = False)
modified_date = models.DateTimeField(auto_now = True, editable = False)
eventdate = models.DateField('Date de la soirée')
fk_customer = models.ManyToManyField(customer, blank=True)
[...]
The invites to an event, for a customer.
class invite(models.Model):
created_date = models.DateTimeField(auto_now_add = True, editable = False)
modified_date = models.DateTimeField(auto_now = True, editable = False)
fk_customer = models.ForeignKey(customer, on_delete=models.CASCADE)
fk_event = models.ForeignKey(event, on_delete=models.CASCADE)
[...]
The description of the level, with the number of invites to be sent based on the customer's level.
class level(models.Model):
created_date = models.DateTimeField(auto_now_add = True, editable = False)
modified_date = models.DateTimeField(auto_now = True, editable = False)
name = models.CharField(max_length=200)
eventinvites = models.IntegerField(default=2)
[...]
I don't want (and probably don't know how) to create the invites as soon as I add a customer to an event. Because I would need to manage removing a customer, and also manage a deletion of an invite.
So what I need is, for each event, to check if we have enough invites based on the customer level.
If there are no "invite" to an event for a "basic" customer, I would need to list "eventinvites" (2) invites (I can manage the creation once I know how many).
If there is only 1 invite, then I would only need to list 1. And if there are more than 1, then I don't have to do anything.
In stantard python code, I would probably walk the events, then for each of them, and for each of the customers that have been invited, walk the invites and check if how many I have that match the customer and event. If less than "eventinvites", I would add the event / customer pair to a list of "to do".
Something like
1 missing_invites = []
2 for event in Events:
3 for invited_customer in (Customers where event.fk_customer):
4 invite_count = 0
5 for invites in (Invites where invite.fk_customer==invited_customer and
6 invite.fk_event == event.id):
7 invite_count += 1
8 if invite_count < event.fk_customer.fk_level.eventinvites:
9 missing_invites += (event, invited_customer)
In Django, I can create the view with Events, Invites and Customers in the context.
I can walk the Events (line 2), then filter on the Customers (line 3), and list the invites that have already been created.
views.py:
class MissingInvitesView(PermissionRequiredMixin, ListView):
model = invite
template_name = 'missing.html'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context["Cards"] = event.objects.all() #because it's easier
to understand
context["Events"] = event.objects.all()
context["Customers"] = customer.objects.all()
And missing.html:
{% for event in Events %}
{% for customer in event.fk_customer.all %}
{% with eventInvite=Invites|matchingEvent:event.id %}
{% for customereventInvite in eventInvite|matchingCustomer:customer.id %}
<br>{{ customereventInvite }}
{%endfor%}
{%endwith%}
{% endfor%}
{%endfor%}
With this, I can get the list of invites that exist. But I can't figure out a way to either count these and display something if one is missing.
I'm still working with generic (class) views, because it's easier. Should I start learning about standard (def) views ? Would it work ?
Thanks a lot for your help.
Jm
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终找到了如何完成我的挑战。
Missing.html 已更新为 :
和我的 custom_tags:
它可能不是最有效或最干净的(因为我在 custom_tags 中创建新查询),但它有效。
欢迎任何评论。
I eventually found out how to complete my challenge.
Missing.html has been updated to :
and my custom_tags:
It might not be the most efficient or the cleanest (because I create new queries in custom_tags), but it works.
Any comments welcome.