【发布时间】:2011-04-14 21:34:06
【问题描述】:
我正在为一个小型销售 CRM 应用创建警报/通知系统。 我有一个 Lead_Contact 模型,用于存储客户的姓名、地址等,还有一个 Contact_Notifier 模型,用于跟踪客户第一次联系的时间、最后一次联系以及我们接下来的时间联系他们。
作为参考,这里是模型的相关sn-ps:
class Lead_Contact(models.Model):
first_contacted = models.ManyToManyField('Contact_Notifier', related_name='first_contact', null=True,blank=True)
last_contacted = models.ManyToManyField('Contact_Notifier', related_name='last_contact', null=True,blank=True)
next_contacted = models.ManyToManyField('Contact_Notifier', related_name='next_contact', null=True,blank=True)
和
class Contact_Notifier(models.Model):
WHEN_CHOICES = (
('F', 'First'),
('L', 'Last'),
('N', 'Next'),
)
when_contact = models.CharField(max_length=1, choices=WHEN_CHOICES)
contact_date = models.DateField(blank = True, null=True)
contact_time = models.TimeField(blank = True, null=True)
contact_message = models.TextField(blank=True)
is_finished = models.BooleanField(default=False)
我创建了一个视图函数,它实质上过滤了 Contact_Notifier 以显示我的所有 next_contacted 对象给 CRM 应用程序的各个用户,如下所示:
def urgent_notifier(request, template_name='urgent_results.html'):
error = ""
selected_user = user_filter(request)
results=Contact_Notifier.objects.filter( Q(user=selected_user) | Q(user="AU")).filter(when_contact = 'N').filter(contact_date__lte=datetime.date.today())
return render_to_response(template_name, {'issues': results, 'error': error})
现在在我的模板中,我正在显示我的查询集,但是当我尝试显示 Lead_Contact 模型中的字段时出现问题;我已经阅读了 Django book 和 Django Project 文档,但我似乎无法让反向关系显示工作!下面是相关的模板代码:
{% if issues %}
{% for issue in issues %}
<form action="/results/{{issue.id}}/normalize/" method="post">
<input type="submit" value="remove" /><b>Contact Time:</b> {{issue.contact_date}} <b> at </b> {{issue.contact_time}} <b>via</b> {{issue.get_contact_type_display}} <br>
<!-- Here is where my problems start -->
{% for item in issue.lead_contact_set.all %}
{{ item.salutation }} <a href="../results/{{issue.pk}}/"> {{ item.first_name }} {{ item.last_name }} </a> <b> Phone:</b> {{ item.phone_1 }} {{ issue.phone_2 }} <b>email:</b> {{item.email}} <br>
{% endfor %}
</form>
{% endfor %}
{% endif %}
我也尝试过使用这样的相关名称:
{% for item in issue.next_contact.all %}
我做错了什么?
【问题讨论】:
标签: django django-templates many-to-many