在Django模板中显示反向多对多 [英] Displaying reverse many-to-many in Django Templates

查看:120
本文介绍了在Django模板中显示反向多对多的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为小型销售CRM应用程序创建一个警报/通知系统。
我有一个Lead_Contact模型,用于存储客户端的名称,地址等,以及一个Contact_Notifier模型,用于跟踪客户端首次联系,最后一次联系,以及何时接下来要联系他们。

I'm in the midst of creating an alarm/notification system for a small Sales CRM app. I have a Lead_Contact model that is used to store a client's name, address, etc. as well as a Contact_Notifier models that is being used to keep track of when a client was first contacted, the last contact, and when we are going to next contact them.

为了参考,以下是模型的相关片段:

For reference, here is the relevant snippets of the models:

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以显示CRM应用程序的各个用户的所有next_contact对象,如:

I have created a view function that essentially filters Contact_Notifier to display all of my next_contacted objects for the individual users of the CRM app, like such:

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书和Django项目文档,但我似乎无法使反向关系显示工作!以下是相关的模板代码:

Now in my template, I'm displaying my queryset, but having issues when I attempt to display fields from the Lead_Contact model; I've read over the Django book and Django Project documentation, but I just can't seem to make the reverse relationship displays work! Here's the relevant template code:

{% 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 %}

我做错了什么?

推荐答案

您在 Lead_Contact Contact_Notifier 之间有三个关系,您已正确定义了 related_name 属性。所以这些是你应该用来跟随你的反向关系的名字:

You have three relationships between Lead_Contact and Contact_Notifier, and you've correctly defined related_name attributes for all of them. So those are the names you should be using to follow your reverse relationship:

{% for item in issue.first_contact.all %}

这篇关于在Django模板中显示反向多对多的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆