> Hello.  Newbie to django and first-time poster to this group.

welcome!

  > class Employee(models.Model):
>      name = models.CharField(maxlength=50)
>      office = models.CharField(maxlength=20)
>      phone= models.CharField(maxlength=15)
> 
> class Photo(models.Model):
>      filename = models.CharField(maxlength=50)
>      master_photo = models.BooleanField()
>      employee = models.ForeignKey(Employee)
> 
> In one of my views I'd like to have a table consisting of rows like:
> 
> <td><Photo.filename></td><td><Employee.name></
> td><td><Employee.office></td>
> 
> but if I say:
> 
>     object_list = Employee.objects.order_by('name')

I think you want something like

   object_list = Photo.objects.select_related().order_by(SORT_FIELD)

and then in your template you want

   {% for photo in object_list %}
    <tr>
     <td>photo.filename</td>
     <td>photo.employee.name</td>
     <td>photo.employee.office</td>
    </tr>
   {% endfor %}

I never remember what the value for SORT_FIELD should be off the 
top of my head...so you'd have to try some combination of your 
app-name, object-name, and field-name, joined by either 
underscores or periods.  My first attempt would be

   SORT_FIELD = 'app_employee.name'

though it might be something like one of these

   SORT_FIELD = 'app_employee__name'
   SORT_FIELD = 'employee.name'
   SORT_FIELD = 'employee__name'

where "app" is the name of the app in your project.

Alternatively, you might be able to do something like

   object_list = Employee.objects.select_related().order_by('name')

and then use

   {% for emp in object_list %}
     {% for photo in emp.photo_set %}
      <tr>
       <td>photo.filename</td>
       <td>emp.name</td>
       <td>emp.office</td>
      </tr>
     {% endfor %}
   {% endfor %}

or even

   {% for emp in object_list %}
     <tr>
      <td>emp.name</td>
      <td>emp.office</td>
      <td>
       <ul>
        {% for photo in emp.photo_set %}
         <li>photo.filename</li>
        {% endfor %}
       </ul>
      </td>
     </tr>
   {% endfor %}

HTH,

-tim


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to