Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango query output from backwards ForeignKey relation
    primarykey
    data
    text
    <p>I have a django Model, <code>Foo</code>, which has various known and consistently-named attributes (<code>alpha</code>, <code>beta</code>, ...) as well as a variable number of instances of <code>Bar</code>; <code>Bar</code> is itself a model with attributes (<code>name</code>, <code>value</code>, ...). I can't have each <code>bar</code>'s name and value etc. as a column in the <code>Foo</code> model because there are too many of them, they have many different names, and some apply to only a few foos. So I have a ForeignKey relationship within the <code>Bar</code> model, so each <code>bar</code> knows which <code>foo</code> it belongs to, and I can use Django's <code>select_related()</code>, e.g.:</p> <pre><code>foos = Foo.objects.filter(alpha__lte=10).select_related() for foo in foos: bars = foos.bar_set.filter(name__in=('prop1', 'prop2', 'prop5')) for bar in bars: print '%s = %d' % (bar.name, bar.value) </code></pre> <p>This seems to work... but is it really the best way to organise my data with MySQL?</p> <p>If I'm on the right track, I have another question: how can I speed this up? I should add that I have 2,700,000 <code>Foos</code> and 16,900,000 <code>Bars</code> in total (with indexes in place, etc.). Particularly slow is my output - I'd like to be able to write text files with columns of the properties: <code>foo.alpha</code>, <code>foo.beta</code>, <code>foo.bar1.value</code>, <code>foo.bar2.value</code>, etc... where <code>bar1</code>, <code>bar2</code>, etc. are chosen by their <code>name</code> attributes. I've been experimenting with a list of output fields:</p> <pre><code>bar_output_fields = ['prop1', 'prop2', 'prop5'] all_output_fields = [('%4d', 'alpha'), (%12.6f', 'prop1'), (%10.3e', 'beta'), (%10.3e', 'prop2') ('%2d', 'prop5')] fo = open('output.txt', 'w') for foo in foos: bars = foos.bar_set.filter(name__in=bar_output_fields) for bar_output_field in bar_output_fields: bar = bars.filter(name=bar_output_field).get() setattr(foo, bar_output_field, bar.value) for (fmt, name) in all_output_fields: print &gt;&gt;fo, fmt % getattr(foo, name), print &gt;&gt;fo fo.close() </code></pre> <p>But this is extremely slow when the query returns >1000s of <code>foo</code>s. Anyone got any suggestions for improving performance here? I've a feeling I'm missing something big...</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload