Template Data

15 views
Skip to first unread message

dtdave

unread,
Nov 18, 2016, 12:08:36 PM11/18/16
to Django users
As still learning django I have a simple issue.

I have the following models:


class Contactnumber(TimeStampedModel):
    phone_number
= models.CharField(max_length=100, unique=True)
    contact
= models.ForeignKey(‘contacts.Contact')



class Contact(TimeStampedModel):
    contact
= models.CharField(max_length=100, db_index=True,
                               verbose_name
='Contact Name')
    practice
= models.ManyToManyField(‘practice.Practice’,
                                      related_name
=“contacts",)



class Practice(TimeStampedModel):
    practice
= models.CharField(max_length=150, db_index=True,
                                verbose_name
='Practice Name',
                                help_text
="include location for multi-branch”)



I have a template including data tables in which I want to show the following:
Practice, Contact, Contact Phone

The following gives me the contact for the practice:   
<td>{% for contact in practice.contacts.all %}
               {{ contact }}
           {% endfor %}
</td>

My question is how do  include the contact phone number as I seem a bit lost.
Any help would be appreciated. Apologies if this is a simple question!

Vijay Khemlani

unread,
Nov 18, 2016, 1:42:41 PM11/18/16
to django...@googlegroups.com
In your model a contact may have multiple phone numbers (multiple Contactnumber instances with foreign key to the same Contact instance) so your question is a bit vague.

If you wanted to list all numbers for a given contact you could do somethinkg like

<td>{% for contact in practice.contacts.all %}
    {{ contact }}
    <ul>
    {% for contactnumber in contact.contactnumber_set.all %}
        <li>{{ contactnumber.phone_number }}</li>
    {% endfor %}
    </ul>
{% endfor %}</td>


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0d8490a2-db29-435a-9477-e26dfa03c560%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Turner

unread,
Nov 18, 2016, 2:52:07 PM11/18/16
to django...@googlegroups.com
Thankyou for this, although for some reason it does not work.
What other infromation would you need to further advise me.
Thanks

--
You received this message because you are subscribed to a topic in the Google Groups "Django users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-users/iGLMpw_nkac/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-users+unsubscribe@googlegroups.com.

To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.

Vijay Khemlani

unread,
Nov 18, 2016, 2:59:28 PM11/18/16
to django...@googlegroups.com
Test the "contact.contactnumber_set.all" in a shell, I'm not sure 100% sure on the syntax

Reply all
Reply to author
Forward
0 new messages