Displaying Data from Multiple Tables
Hi all, This is a difficulty related to me being new to Django/Python I assume, so I hope you can forgive my ignorance and help me out :) I have 4 tables - User(overriding the user object), Copy, Query, QueryClassification. The main tables are Copy and Query. There can be many Queries for each Copy, so... Query has: copy = models.ForeignKey(Copy) classification = models.ManyToManyField(QueryClassification) Copy has: user = models.ForeignKey(User) Basically it isn't displaying any of the fields from Query or QueryClassification and I'm not quite sure how to reference the other foreign tables in reference to a one-to-many field. Clearly I can iterate through the Copy items, but I can't get it to print the multiple Query items next to the appropriate Copy items. Below is some code for context: #views.py def copy_query(request, copy, query): obj_list = copy.objects.select_related() template_name = 'query/copyquery.html' return render_to_response(template_name, {'object_list': obj_list,}) The page outputs the following multi-column list. Where you see brackets, that's me isolating where data particularly isn't showing. The data that isn't showing is the data from Query and QueryClassification. Copy with Queries * 34 1085 alexk 292 ST-S (--distpool) (--q) (--c) (-- req) Yes - Q8 = Pp, Q9 = No (--expl) * 34 1081 alexk 292 ST-S (--distpool) (--q) (--c) (-- req) Yes - Q8 = Pa, Q9 = No (--expl) * 34 1079 alexk 292 ST-S (--distpool) (--q) (--c) (-- req) Yes - Q8 = Pr, Q9 = No (--expl) * 34 1073 alexk 292 ST-S (--distpool) (--q) (--c) (-- req) Yes - Q8 = Py, Q9 = No (--expl) Regards and thanks, Alex --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Displaying Data from Multiple Tables
On Mon, 2009-02-02 at 20:52 -0800, Alexiski wrote: > Hi all, > This is a difficulty related to me being new to Django/Python I > assume, so I hope you can forgive my ignorance and help me out :) > > I have 4 tables - User(overriding the user object), Copy, Query, > QueryClassification. The main tables are Copy and Query. There can be > many Queries for each Copy, so... > > Query has: > copy = models.ForeignKey(Copy) > classification = models.ManyToManyField(QueryClassification) > Copy has: > user = models.ForeignKey(User) > > > Basically it isn't displaying any of the fields from Query or > QueryClassification and I'm not quite sure how to reference the other > foreign tables in reference to a one-to-many field. Clearly I can > iterate through the Copy items, but I can't get it to print the > multiple Query items next to the appropriate Copy items. Given a Copy instance, obj, you can access the related Query instances (in a template) as obj.query.all. So iterate over that to get all the instances you're after. Regards, Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Displaying Data from Multiple Tables
Hi Malcolm, Thanks for your response, but I'm not quite sure how to iterate using obj.query.all to get all the instances I'm after. Could you please provide a code snippet or direct me to a relevant page? Thanks again, Alex On Feb 3, 4:04 pm, Malcolm Tredinnick wrote: > On Mon, 2009-02-02 at 20:52 -0800, Alexiski wrote: > > Hi all, > > This is a difficulty related to me being new to Django/Python I > > assume, so I hope you can forgive my ignorance and help me out :) > > > I have 4 tables - User(overriding the user object), Copy, Query, > > QueryClassification. The main tables are Copy and Query. There can be > > many Queries for each Copy, so... > > > Query has: > > copy = models.ForeignKey(Copy) > > classification = models.ManyToManyField(QueryClassification) > > Copy has: > > user = models.ForeignKey(User) > > > Basically it isn't displaying any of the fields from Query or > > QueryClassification and I'm not quite sure how to reference the other > > foreign tables in reference to a one-to-many field. Clearly I can > > iterate through the Copy items, but I can't get it to print the > > multiple Query items next to the appropriate Copy items. > > Given a Copy instance, obj, you can access the related Query instances > (in a template) as obj.query.all. So iterate over that to get all the > instances you're after. > > Regards, > Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Displaying Data from Multiple Tables
On Mon, 2009-02-02 at 21:21 -0800, Alexiski wrote: > Hi Malcolm, > > Thanks for your response, but I'm not quite sure how to iterate using > obj.query.all to get all the instances I'm after. > > Could you please provide a code snippet or direct me to a relevant > page? Iteration in templates is done with the forloop template tag. You must already be using that to iteratate over all the copy instances. Regards, Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Displaying Data from Multiple Tables
On Tue, 2009-02-03 at 16:28 +1100, Malcolm Tredinnick wrote: > On Mon, 2009-02-02 at 21:21 -0800, Alexiski wrote: > > Hi Malcolm, > > > > Thanks for your response, but I'm not quite sure how to iterate using > > obj.query.all to get all the instances I'm after. > > > > Could you please provide a code snippet or direct me to a relevant > > page? > > Iteration in templates is done with the forloop template tag. Err .. the *for* template tag, rather. Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Displaying Data from Multiple Tables
> On Mon, 2009-02-02 at 21:21 -0800, Alexiski wrote: > > Hi Malcolm, > > > Thanks for your response, but I'm not quite sure how to iterate using > > obj.query.all to get all the instances I'm after. > > > Could you please provide a code snippet or direct me to a relevant > > page? > > Iteration in templates is done with the forloop template tag. You must > already be using that to iteratate over all the copy instances. Correct I am using the {% for Copy in object_list %} tag. I access the fields like so - {{ Copy.id }} {{ Copy.question }} In order to access the n Query objects per Copy object, do I nest {% for Query in object_list %} and access using {{ Query.id }} {{ Query.requirements }} ? Alex --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Displaying Data from Multiple Tables
On Mon, 2009-02-02 at 21:37 -0800, Alexiski wrote: > > > On Mon, 2009-02-02 at 21:21 -0800, Alexiski wrote: > > > Hi Malcolm, > > > > > Thanks for your response, but I'm not quite sure how to iterate using > > > obj.query.all to get all the instances I'm after. > > > > > Could you please provide a code snippet or direct me to a relevant > > > page? > > > > Iteration in templates is done with the forloop template tag. You must > > already be using that to iteratate over all the copy instances. > > Correct I am using the {% for Copy in object_list %} tag. I access the > fields like so - {{ Copy.id }} {{ Copy.question }} > > In order to access the n Query objects per Copy object, do I nest {% > for Query in object_list %} and access using {{ Query.id }} > {{ Query.requirements }} ? No, because object_list is an sequence of Copy instances, not a sequence of Query instances. You need to iterate over Copy.query_set.all, using your variable names, in the nested loop. That is, for each copy instance loop over the related Query instances. Note that I probably made a typo earlier, it should be Copy.query_set.all, not Copy.query.all. In case I've continued to get it wrong, read the documentation for the queryset API, in particular the part about accessing reverse relations. Regards, Malcolm --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Displaying Data from Multiple Tables
Alex, would you mind posting your complete template at dpaste.org for us to look at? It might clarify how you're trying to do the output. Thanks, Jon. On Feb 2, 11:37 pm, Alexiski wrote: > > On Mon, 2009-02-02 at 21:21 -0800, Alexiski wrote: > > > Hi Malcolm, > > > > Thanks for your response, but I'm not quite sure how to iterate using > > > obj.query.all to get all the instances I'm after. > > > > Could you please provide a code snippet or direct me to a relevant > > > page? > > > Iteration in templates is done with the forloop template tag. You must > > already be using that to iteratate over all the copy instances. > > Correct I am using the {% for Copy in object_list %} tag. I access the > fields like so - {{ Copy.id }} {{ Copy.question }} > > In order to access the n Query objects per Copy object, do I nest {% > for Query in object_list %} and access using {{ Query.id }} > {{ Query.requirements }} ? > > Alex --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Displaying Data from Multiple Tables
Oops... I meant dpaste.com On Feb 3, 8:54 am, Jon Loyens wrote: > Alex, would you mind posting your complete template at dpaste.org for > us to look at? It might clarify how you're trying to do the output. > > Thanks, > > Jon. > > On Feb 2, 11:37 pm, Alexiski wrote: > > > > On Mon, 2009-02-02 at 21:21 -0800, Alexiski wrote: > > > > Hi Malcolm, > > > > > Thanks for your response, but I'm not quite sure how to iterate using > > > > obj.query.all to get all the instances I'm after. > > > > > Could you please provide a code snippet or direct me to a relevant > > > > page? > > > > Iteration in templates is done with the forloop template tag. You must > > > already be using that to iteratate over all the copy instances. > > > Correct I am using the {% for Copy in object_list %} tag. I access the > > fields like so - {{ Copy.id }} {{ Copy.question }} > > > In order to access the n Query objects per Copy object, do I nest {% > > for Query in object_list %} and access using {{ Query.id }} > > {{ Query.requirements }} ? > > > Alex --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Displaying Data from Multiple Tables
Here Jon et al, http://dpaste.com/116323/ Keep in mind this is very draft-y... I'm pretty much just doing this to test and play around with getting the data. Hope this helps and thank you. -Alex On Feb 4, 1:54 am, Jon Loyens wrote: > Oops... I meant dpaste.com > > On Feb 3, 8:54 am, Jon Loyens wrote: > > > Alex, would you mind posting your complete template at dpaste.org for > > us to look at? It might clarify how you're trying to do the output. > > > Thanks, > > > Jon. > > > On Feb 2, 11:37 pm, Alexiski wrote: > > > > > On Mon, 2009-02-02 at 21:21 -0800, Alexiski wrote: > > > > > Hi Malcolm, > > > > > > Thanks for your response, but I'm not quite sure how to iterate using > > > > > obj.query.all to get all the instances I'm after. > > > > > > Could you please provide a code snippet or direct me to a relevant > > > > > page? > > > > > Iteration in templates is done with the forloop template tag. You must > > > > already be using that to iteratate over all the copy instances. > > > > Correct I am using the {% for Copy in object_list %} tag. I access the > > > fields like so - {{ Copy.id }} {{ Copy.question }} > > > > In order to access the n Query objects per Copy object, do I nest {% > > > for Query in object_list %} and access using {{ Query.id }} > > > {{ Query.requirements }} ? > > > > Alex --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Displaying Data from Multiple Tables
> Note that I probably made a typo earlier, it should be > Copy.query_set.all, not Copy.query.all. In case I've continued to get it > wrong, read the documentation for the queryset API, in particular the > part about accessing reverse relations. Thanks Malcolm, i'll check this out and hopefully it'll do the trick :) I appreciate your help! -Alex --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---
Re: Displaying Data from Multiple Tables
Can anyone help with this - I found some code in the QuerySet API reference # Hits the database. e = Entry.objects.select_related().get(id=5) # Doesn't hit the database, because e.blog has been prepopulated # in the previous query. b = e.blog As opposed to getting a single id, I couldn't find anywhere that would show how to iterate through this, and couldn't understand how to use the iterator(). Could someone give an example of how to match up all of the entries to their appropriate blogs? I believe this would be identical to matching up my Query to my Copy, as there are multiple Queries to a single Copy, only we're using an example from the API :) Thanks in advance, -Alex --~--~-~--~~~---~--~~ 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 django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---