0

I have three related models and want to make a query to get a combination of fields from all three models.

invoice_select = Ordered_item.objects.filter(oi_order = o_id).select_related()

generates the SQL-statement which I can check with the debug_toolbar. The SQL statement contains all fields of the related models.

Sending the result of the query to a html-file with

 return render_to_response('invoice_select.html', {'invoice_select':invoice_select} 

provides only the the expression which was defined for the Ordered_item model with:

   def __unicode__(self):
   return u'%s -- %s -- %s' % (self.oi_order, self.oi_pos, self.oi_item) 

So the result of the query looks like:

{'invoice_select': [<Ordered_item: 1109231733 -- 01 -- BP-0516-aa>]} 

which are exactly the fields defined in the def unicode(self):

What can I do to add more fields to the result of the query? Why do the fields in the SQL not show up in the result of the query ?

Any help will be highly appreciated.

Helmut
  • 233
  • 5
  • 14
  • How exactly are you outputting `invoice_select`? If you are just putting `{{ invoice_select }}` in your view, Django is going to basically output: `{{ invoice_select.__unicode__() }}`. If you want to output more, or differently, you will need to edit your view. – Jack M. Oct 06 '11 at 16:21
  • Could you give me an example please how to access other fields – Helmut Oct 06 '11 at 19:51

2 Answers2

1

You're just looking at the string representation of the object, which obviously uses the __unicode__ method. The fields are there, of course, if you access the objects directly.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • I have no idea how to access the fields of the related objects. Could you give me an example please ? – Helmut Oct 06 '11 at 19:53
1

The __unicode__ method is automagically being called when you refer to the object as a string. You could simply put the fields you care about in the __unicode__ method (assuming a Manufacturer model):

def __unicode__(self):
   return u'%s -- %s -- %s -- %s -- %s' % (self.oi_order, self.oi_pos, self.oi_item,
       self.manufacturer.name, self.manufacturer.favorite_stuffed_animal) 

If you don't want to put that in the __unicode__ method, which does get used for other things, you can easily put it in some separate method and directly call that one rather than implicitly calling the __unicode__ one.

Also further note that if manufacturer might be null you need to handle that, perhaps with a try-catch block.

Here's what a separate method might look like:

def get_str(self):
    return u'%s %s' % (self.oi_order, self.manufacturer.name)

And then if you have an oi instantiated:

str = oi.get_str()
Dave Orr
  • 1,082
  • 1
  • 9
  • 18
  • how could the separate method look like? could you give me an example? – Helmut Oct 07 '11 at 08:48
  • Is it possible to generate in a view a second query on a different model and pass the result of two different queries with one return_to_response call over to the template ? – Helmut Oct 07 '11 at 08:51
  • I'll update the entry with a simple method, but it should be pretty straightforward. I'm not sure exactly what you're asking with your second comment, that's probably worthy of a separate post. – Dave Orr Nov 03 '11 at 18:08