开发者

Displaying reverse many-to-many in Django Templates

开发者 https://www.devze.com 2023-01-16 18:30 出处:网络
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 mo

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)

and

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)

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}) 

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.ge开发者_高级运维t_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 %} 

I've also tried using the related name like this:

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

What am I doing wrong?


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

精彩评论

暂无评论...
验证码 换一张
取 消