how to get back all fields of a record in a many to many relationship
I have these tables:
class Contract(models.Model):
project = models.ForeignKey(Project)
start_date = models.DateField()
agreed_contacts = models.ManyToManyField(Contact, through='ContractPartyInvolved')
class ContractPartyInvolved(models.Model):
contact = models.ForeignKey(Contact)
contract = models.ForeignKey(Contract)
role = models.ForeignKey(Role)
agreed = models.BooleanField()
I want to query to get all the contacts linked to开发者_Python百科 a specific contract so I have done this in my view:
def generate_contract(request, id):
contract = get_object_or_404(Contract, pk=id)
agreedContacts = contract.agreed_contacts.all()
return render_to_response('contract.html', {'agreedContacts' : agreedContacts })
Now in my template I have:
{% for ac in agreedContacts %}
<strong> {{ ac.agreed }} || {{ ac }}</strong>
{% endfor %}
The problem is that I'm only getting back the name, the {{ ac }} works correctly but I'm not getting back all the other fields associated to that record. For example agreed and role. How can I do this?
ac
is a Contact
instance, not a ContractPartyInvolved
one. So ac.agreed
won't work (unless you also have an agreed
field on Contact, which you don't show).
Instead of following the ManyToMany relationship and getting contract.agreed_contacts.all()
, you want to follow the reverse ForeignKey from Contact to CPI. From there you can access all the fields on CPI, and also continue following the relationship to Contact and get the contact name:
cpis = contract.contractpartyinvolved_set.select_related().all()
{% for cpi in cpis %}
{{ cpi.agreed }} || {{ cpi.contact.name }}
{% endfor %}
Note that I've used select_related
on the initial query, to save further db hits as you know you're going to be following the relationship. If you don't want to access Role as well, you can limit the select_related by specifying select_related('Contact')
.
in view:
involved_contacts = contract.contractpartyinvolved_set.all()
in template:
{% for icontact in involved_contacts %}
<strong> {{ icontact.agreed }} || {{ icontact.contact }}</strong>
{% endfor %}
精彩评论