python - Django template - get matching foreign key objects in a template? -


i have model named circuits , each circuit may have many files associated it.

so in view getting circuits , files, trying files match circuits in template using if statement.

i have tried, below come empty, how traverse models in template find match?

file.circuit_contract_data file.circuit_contract_data__id 

thanks

view.py

@login_required     def showroom_detail(request, showroom_id):     modelshowroom = get_object_or_404(showroomconfigdata, pk=showroom_id)     modelcircuits = circuitinfodata.objects.filter(showroom_config_data=showroom_id)      modelcircuitfiles = circuitfiles.objects.filter(circuit_contract_data__showroom_config_data=showroom_id)      modelsitephotos = sitephotos.objects.filter(showroom_config_data=showroom_id)     modelsitefiles = sitefiles.objects.filter(showroom_config_data=showroom_id)          return render(request, 'service/showroom_detail.html', {         'showroom': modelshowroom,         'circuits': modelcircuits,         'circuitfiles': modelcircuitfiles,         'sitephotos': modelsitephotos,         'sitefiles': modelsitefiles,     })   

template

{% item in circuits %}     <tr class="{% cycle 'tr-1' 'tr-2' %}">           <td>{{ item.provider }}</td>         <td>{{ item.service_type }}</td>         <td>{{ item.circuit_speed }}</td>         <td>         {% file in circuitfiles %}             {% if file.circuit_contract_data == item.id %}             <a href ="{{ media_url}}{{ file.circuit_file }}" target="_blank">{{ file.file_name }}</a><br />             {% endif %}          {% endfor %}         </td>     </tr> 

model

class circuitinfodata(models.model):         showroom_config_data = models.foreignkey(showroomconfigdata,verbose_name="install showroom",blank=true)     major_site_info = models.foreignkey(majorsiteinfodata,verbose_name="install site",blank=true)             service_type = models.foreignkey(circuittypeinfodata)     circuit_speed = models.integerfield(blank=true)     circuit_bearer = models.integerfield(blank=true)     provider = models.charfield(max_length=200)     ref_no = models.charfield(max_length=200,verbose_name="reference no")     install_location = models.charfield(max_length=200)     install_date = models.datefield()     cost_per_month = models.decimalfield(decimal_places=2,max_digits=8)     contract_length = models.integerfield(verbose_name="contact length in years")     notes = models.textfield(blank=true)     service_service_contacts = models.foreignkey(servicecontacts)      class meta:         verbose_name = "circuit data"         verbose_name_plural = "circuit data"         ordering = ('showroom_config_data__location',)      def __unicode__(self):         return '%s | %s | %s | %s' % (self.showroom_config_data.location, self.provider, self.service_type, self.ref_no)  class circuitfiles(models.model):      circuit_contract_data = models.foreignkey(circuitinfodata,verbose_name="showroom",blank=true)       circuit_file = models.filefield(blank=true,upload_to=service_upload_path)          file_name = models.charfield(max_length=200,verbose_name="file name")      class meta:             verbose_name = "circuit files"             verbose_name_plural = "circuit files"                 ordering = ('circuit_contract_data',)      def __unicode__(self):         return self.file_name 

looping through , comparing objects inefficient , unnecessary because can query foreign key relationship backwards so:

{% item in circuits %}     <tr class="{% cycle 'tr-1' 'tr-2' %}">           <td>{{ item.provider }}</td>         <td>{{ item.service_type }}</td>         <td>{{ item.circuit_speed }}</td>         <td>         {% file in item.circuitfiles_set.all %}             <a href ="{{ file.circuit_file.url }}" target="_blank">{{ file.file_name }}</a><br />         {% endfor %}         </td>     </tr> {% endfor %} 

you can drop circuitfiles context. can use url property of filefield don't need prepend media_root.

also general convention in python, class names camel cased - other variables lower cased. following convention makes easier distinguish between class , object of class.


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -