Source

django-generic-images / docs / _build / html / index.html

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django-generic-images’s documentation &mdash; django-generic-images v0.35 documentation</title>
    <link rel="stylesheet" href="_static/default.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '',
        VERSION:     '0.35',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="top" title="django-generic-images v0.35 documentation" href="" />
    <link rel="next" title="Upgrade notes" href="upgrade.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="upgrade.html" title="Upgrade notes"
             accesskey="N">next</a> |</li>
        <li><a href="">django-generic-images v0.35 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="django-generic-images-s-documentation">
<h1>django-generic-images&#8217;s documentation<a class="headerlink" href="#django-generic-images-s-documentation" title="Permalink to this headline"></a></h1>
<p>django-generic-images is a generic images pluggable django app.</p>
<p>This app provides image model (useful managers, methods and fields)
that can be attached to any other Django model using generic relations.
It also provides admin multi-image uploader (based on
<a class="reference" href="http://bitbucket.org/kmike/gearsuploader/">GearsUploader</a> ) with client-side
image resizing, animated progress bar and before-upload image previews.</p>
<p>Requirements: django 1.1 (or trunk).</p>
<p><a class="reference" href="http://bitbucket.org/daevaorn/django-composition/">django-composition</a> is
required if you want to use
<a title="generic_images.fields.ImageCountField" class="reference" href="#generic_images.fields.ImageCountField"><tt class="xref docutils literal"><span class="pre">ImageCountField</span></tt></a> or
<a title="generic_images.fields.UserImageCountField" class="reference" href="#generic_images.fields.UserImageCountField"><tt class="xref docutils literal"><span class="pre">UserImageCountField</span></tt></a>.</p>
<p>There is an image gallery app
(<a class="reference" href="http://bitbucket.org/kmike/django-photo-albums/">django-photo-albums</a>)
based on django-generic-images.</p>
<ul>
<li class="toctree-l1"><a class="reference" href="upgrade.html">Upgrade notes</a><ul>
<li class="toctree-l2"><a class="reference" href="upgrade.html#from-0-35-7-to-0-35-8">From 0.35.7 to 0.35.8</a></li>
</ul>
</li>
</ul>
<div class="section" id="installation">
<h2>Installation<a class="headerlink" href="#installation" title="Permalink to this headline"></a></h2>
<div class="highlight-python"><pre>$ pip install django-generic-images</pre>
</div>
<p>or:</p>
<div class="highlight-python"><pre>$ easy_install django-generic-images</pre>
</div>
<p>or:</p>
<div class="highlight-python"><pre>$ hg clone http://bitbucket.org/kmike/django-generic-images/
$ cd django-generic-images
$ python setup.py install</pre>
</div>
<p>Then add &#8216;generic_images&#8217; to your <tt class="docutils literal"><span class="pre">INSTALLED_APPS</span></tt> in settings.py and run</p>
<div class="highlight-python"><pre>$ manage.py syncdb</pre>
</div>
<p>If you want <tt class="docutils literal"><span class="pre">ImageCountField</span></tt> and <tt class="docutils literal"><span class="pre">UserImageCountField</span></tt> then follow
installation instructions at  <a class="reference" href="http://bitbucket.org/daevaorn/django-composition/">http://bitbucket.org/daevaorn/django-composition/</a>
to install django-composition.</p>
<p>For admin uploader to work <tt class="docutils literal"><span class="pre">generic_images</span></tt> folder from
<tt class="docutils literal"><span class="pre">generic_images/media/</span></tt> should be copied to project&#8217;s <tt class="docutils literal"><span class="pre">MEDIA_ROOT</span></tt>.</p>
</div>
<div class="section" id="usage">
<h2>Usage<a class="headerlink" href="#usage" title="Permalink to this headline"></a></h2>
<div class="section" id="generic-images">
<h3>Generic Images<a class="headerlink" href="#generic-images" title="Permalink to this headline"></a></h3>
<p>The idea is to provide an infrastructure for images that can be attached
to any django model using generic relations.</p>
<div class="section" id="models">
<h4>Models<a class="headerlink" href="#models" title="Permalink to this headline"></a></h4>
<dl class="class">
<dt id="generic_images.models.AttachedImage">
<em class="property">
class </em><tt class="descclassname">generic_images.models.</tt><tt class="descname">AttachedImage</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_images.models.AttachedImage" title="Permalink to this definition"></a></dt>
<dd>Image model that can be attached to any other Django model using
generic relations. It is simply non-abstract subclass of
<a title="generic_images.models.AbstractAttachedImage" class="reference" href="#generic_images.models.AbstractAttachedImage"><tt class="xref docutils literal"><span class="pre">AbstractAttachedImage</span></tt></a></dd></dl>

<dl class="class">
<dt id="generic_images.models.AbstractAttachedImage">
<em class="property">
class </em><tt class="descclassname">generic_images.models.</tt><tt class="descname">AbstractAttachedImage</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_images.models.AbstractAttachedImage" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <a title="generic_images.models.ReplaceOldImageModel" class="reference" href="#generic_images.models.ReplaceOldImageModel"><tt class="xref docutils literal"><span class="pre">generic_images.models.ReplaceOldImageModel</span></tt></a>, <a title="generic_utils.models.GenericModelBase" class="reference" href="#generic_utils.models.GenericModelBase"><tt class="xref docutils literal"><span class="pre">generic_utils.models.GenericModelBase</span></tt></a></p>
<p>Abstract Image model that can be attached to any other Django model
using generic relations.</p>
<dl class="attribute">
<dt id="AbstractAttachedImage.is_main">
<tt class="descname">is_main</tt><a class="headerlink" href="#AbstractAttachedImage.is_main" title="Permalink to this definition"></a></dt>
<dd>BooleanField. Whether the image is the main image for object.
This field is set to False automatically for all images attached to
same object if image with is_main=True is saved to ensure that there
is only 1 main image for object.</dd></dl>

<dl class="attribute">
<dt id="AbstractAttachedImage.order">
<tt class="descname">order</tt><a class="headerlink" href="#AbstractAttachedImage.order" title="Permalink to this definition"></a></dt>
<dd>IntegerField to support ordered image sets.
On creation it is set to max(id)+1.</dd></dl>

<dl class="method">
<dt id="generic_images.models.AbstractAttachedImage.get_file_name">
<tt class="descname">get_file_name</tt><big>(</big><em>filename</em><big>)</big><a class="headerlink" href="#generic_images.models.AbstractAttachedImage.get_file_name" title="Permalink to this definition"></a></dt>
<dd>Returns file name (without path and extenstion)
for uploaded image. Default is &#8216;max(pk)+1&#8217;.
Override this in subclass or assign another functions per-instance
if you want different file names (ex: random string).</dd></dl>

<dl class="method">
<dt id="generic_images.models.AbstractAttachedImage.get_order_in_album">
<tt class="descname">get_order_in_album</tt><big>(</big><em>reversed_ordering=True</em><big>)</big><a class="headerlink" href="#generic_images.models.AbstractAttachedImage.get_order_in_album" title="Permalink to this definition"></a></dt>
<dd>Returns image order number. It is calculated as (number+1) of images
attached to the same content_object whose order is greater
(if &#8216;reverse_ordering&#8217; is True) or lesser (if &#8216;reverse_ordering&#8217; is
False) than image&#8217;s order.</dd></dl>

<dl class="method">
<dt id="generic_images.models.AbstractAttachedImage.get_upload_path">
<tt class="descname">get_upload_path</tt><big>(</big><em>filename</em><big>)</big><a class="headerlink" href="#generic_images.models.AbstractAttachedImage.get_upload_path" title="Permalink to this definition"></a></dt>
<dd><p>Override this in proxy subclass to customize upload path.
Default upload path is
<tt class="docutils literal"><span class="pre">/media/images/&lt;user.id&gt;/&lt;filename&gt;.&lt;ext&gt;</span></tt>
or <tt class="docutils literal"><span class="pre">/media/images/common/&lt;filename&gt;.&lt;ext&gt;</span></tt> if user is not set.</p>
<p><tt class="docutils literal"><span class="pre">&lt;filename&gt;</span></tt> is returned by
<a title="generic_images.models.AbstractAttachedImage.get_file_name" class="reference" href="#generic_images.models.AbstractAttachedImage.get_file_name"><tt class="xref docutils literal"><span class="pre">get_file_name()</span></tt></a>
method. By default it is probable id of new image (it is
predicted as it is unknown at this stage).</p>
</dd></dl>

<dl class="method">
<dt id="generic_images.models.AbstractAttachedImage.next">
<tt class="descname">next</tt><big>(</big><big>)</big><a class="headerlink" href="#generic_images.models.AbstractAttachedImage.next" title="Permalink to this definition"></a></dt>
<dd>Returns next image for same content_object and None if image is
the last.</dd></dl>

<dl class="method">
<dt id="generic_images.models.AbstractAttachedImage.previous">
<tt class="descname">previous</tt><big>(</big><big>)</big><a class="headerlink" href="#generic_images.models.AbstractAttachedImage.previous" title="Permalink to this definition"></a></dt>
<dd>Returns previous image for same content_object and None if image
is the first.</dd></dl>

<dl class="attribute">
<dt id="generic_images.models.AbstractAttachedImage.content_object">
<tt class="descname">content_object</tt><a class="headerlink" href="#generic_images.models.AbstractAttachedImage.content_object" title="Permalink to this definition"></a></dt>
<dd>Provides a generic relation to any object through content-type/object-id
fields.</dd></dl>

<dl class="attribute">
<dt id="generic_images.models.AbstractAttachedImage.objects">
<tt class="descname">objects</tt><a class="headerlink" href="#generic_images.models.AbstractAttachedImage.objects" title="Permalink to this definition"></a></dt>
<dd>Default manager of <a title="generic_images.managers.AttachedImageManager" class="reference" href="#generic_images.managers.AttachedImageManager"><tt class="xref docutils literal"><span class="pre">AttachedImageManager</span></tt></a>
type.</dd></dl>

<dl class="attribute">
<dt id="generic_images.models.AbstractAttachedImage.user">
<tt class="descname">user</tt><a class="headerlink" href="#generic_images.models.AbstractAttachedImage.user" title="Permalink to this definition"></a></dt>
<dd>A ForeignKey to associated user, for example user who uploaded image.
Can be empty.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="generic_images.models.BaseImageModel">
<em class="property">
class </em><tt class="descclassname">generic_images.models.</tt><tt class="descname">BaseImageModel</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_images.models.BaseImageModel" title="Permalink to this definition"></a></dt>
<dd><p>Simple abstract Model class with image field.</p>
<dl class="attribute">
<dt id="BaseImageModel.image">
<tt class="descname">image</tt><a class="headerlink" href="#BaseImageModel.image" title="Permalink to this definition"></a></dt>
<dd><tt class="docutils literal"><span class="pre">models.ImageField</span></tt></dd></dl>

<dl class="method">
<dt id="generic_images.models.BaseImageModel.get_upload_path">
<tt class="descname">get_upload_path</tt><big>(</big><em>filename</em><big>)</big><a class="headerlink" href="#generic_images.models.BaseImageModel.get_upload_path" title="Permalink to this definition"></a></dt>
<dd>Override this to customize upload path</dd></dl>

</dd></dl>

<dl class="class">
<dt id="generic_images.models.ReplaceOldImageModel">
<em class="property">
class </em><tt class="descclassname">generic_images.models.</tt><tt class="descname">ReplaceOldImageModel</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_images.models.ReplaceOldImageModel" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <a title="generic_images.models.BaseImageModel" class="reference" href="#generic_images.models.BaseImageModel"><tt class="xref docutils literal"><span class="pre">generic_images.models.BaseImageModel</span></tt></a></p>
<p>Abstract Model class with image field.
If the file for image is re-uploaded, old file is deleted.</p>
</dd></dl>

</div>
<div class="section" id="module-generic_images.admin">
<h4>Admin<a class="headerlink" href="#module-generic_images.admin" title="Permalink to this headline"></a></h4>
<dl class="attribute">
<dt id="generic_images.admin.AttachedImageAdminForm">
<tt class="descclassname">generic_images.admin.</tt><tt class="descname">AttachedImageAdminForm</tt><a class="headerlink" href="#generic_images.admin.AttachedImageAdminForm" title="Permalink to this definition"></a></dt>
<dd><p>Form for AttachedImage model to be used in inline admin</p>
<p>alias of <tt class="xref docutils literal"><span class="pre">_AttachedImageAdminForm</span></tt></p>
</dd></dl>

<dl class="attribute">
<dt id="generic_images.admin.AttachedImagesInline">
<tt class="descclassname">generic_images.admin.</tt><tt class="descname">AttachedImagesInline</tt><a class="headerlink" href="#generic_images.admin.AttachedImagesInline" title="Permalink to this definition"></a></dt>
<dd><p>InlineModelAdmin for attached images.
Adds multi-image uploader with progress bar, before-upload image
previews and client-side resizing. Uploader is based
on GearsUploader (<a class="reference" href="http://bitbucket.org/kmike/gearsuploader/">http://bitbucket.org/kmike/gearsuploader/</a>) project.</p>
<p>To make this work copy <tt class="docutils literal"><span class="pre">generic_images</span></tt> folder from
<tt class="docutils literal"><span class="pre">generic_images/media/</span></tt> to your <tt class="docutils literal"><span class="pre">MEDIA_ROOT</span></tt>. Then use
<tt class="docutils literal"><span class="pre">AttachedImagesInline</span></tt> class for you inlines:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c">#admin.py</span>

<span class="kn">from</span> <span class="nn">django.contrib</span> <span class="kn">import</span> <span class="n">admin</span>
<span class="kn">from</span> <span class="nn">generic_images.admin</span> <span class="kn">import</span> <span class="n">AttachedImagesInline</span>

<span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span><span class="n">AttachedImagesInline</span><span class="p">]</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">MyModel</span><span class="p">,</span> <span class="n">MyModelAdmin</span><span class="p">)</span>
</pre></div>
</div>
<p>Just before standard formset the following uploader is displayed:</p>
<img alt="_images/admin-with-formset.png" src="_images/admin-with-formset.png" />
<p>Gears plugin is here</p>
<img alt="_images/admin-nogears.png" src="_images/admin-nogears.png" />
<p>Message is displayed if Gears plugin is not available</p>
<img alt="_images/admin-previews.png" src="_images/admin-previews.png" />
<p>User can select several files at once using Ctrl or Shift keys
(Cmd on Mac) in standard OS file selection dialog. He can also remove
images from selection by clicking on thumbnails. Several files can also
be selected by opening file selection dialog several times.</p>
<img alt="_images/admin-uploading.png" src="_images/admin-uploading.png" />
<p>User presses &#8216;Upload&#8217; button and upload process begins</p>
<p>By default the &#8216;Resize ..&#8217; checkbox is unchecked and the input field is
blank. If it is unchecked then images are not resized before uploading.
User can check it and set his max image width.</p>
<p>In order to set the default value and mark the checkbox as checked by
default create customized <tt class="docutils literal"><span class="pre">AttachedImagesInline</span></tt> class using
<a title="generic_images.admin.attachedimages_inline_factory" class="reference" href="#generic_images.admin.attachedimages_inline_factory"><tt class="xref docutils literal"><span class="pre">attachedimages_inline_factory()</span></tt></a> function. This function can also be
used to change uploader language (language auto-discovering is not
implemented):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">generic_images.admin</span> <span class="kn">import</span> <span class="n">attachedimages_inline_factory</span>

<span class="k">class</span> <span class="nc">MyModelAdmin</span><span class="p">(</span><span class="n">admin</span><span class="o">.</span><span class="n">ModelAdmin</span><span class="p">):</span>
    <span class="n">inlines</span> <span class="o">=</span> <span class="p">[</span><span class="n">attachedimages_inline_factory</span><span class="p">(</span><span class="n">lang</span><span class="o">=</span><span class="s">&#39;ru&#39;</span><span class="p">,</span> <span class="n">max_width</span><span class="o">=</span><span class="mf">1024</span><span class="p">)]</span>

<span class="n">admin</span><span class="o">.</span><span class="n">site</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">MyModel</span><span class="p">,</span> <span class="n">MyModelAdmin</span><span class="p">)</span>
</pre></div>
</div>
<p>alias of <tt class="xref docutils literal"><span class="pre">_AttachedImagesInline</span></tt></p>
</dd></dl>

<dl class="function">
<dt id="generic_images.admin.attachedimage_form_factory">
<tt class="descclassname">generic_images.admin.</tt><tt class="descname">attachedimage_form_factory</tt><big>(</big><em>lang='en'</em><big>)</big><a class="headerlink" href="#generic_images.admin.attachedimage_form_factory" title="Permalink to this definition"></a></dt>
<dd>Returns ModelForm class to be used in admin.
&#8216;lang&#8217; is the language for GearsUploader (can be &#8216;en&#8217; and &#8216;ru&#8217; at the
moment).</dd></dl>

<dl class="function">
<dt id="generic_images.admin.attachedimages_inline_factory">
<tt class="descclassname">generic_images.admin.</tt><tt class="descname">attachedimages_inline_factory</tt><big>(</big><em>lang='en'</em>, <em>max_width=''</em><big>)</big><a class="headerlink" href="#generic_images.admin.attachedimages_inline_factory" title="Permalink to this definition"></a></dt>
<dd>Returns InlineModelAdmin for attached images.
&#8216;lang&#8217; is the language for GearsUploader (can be &#8216;en&#8217; and &#8216;ru&#8217; at the
moment). &#8216;max_width&#8217; is default resize width parameter to be set in
widget.</dd></dl>

</div>
<div class="section" id="module-generic_images.managers">
<h4>Managers<a class="headerlink" href="#module-generic_images.managers" title="Permalink to this headline"></a></h4>
<dl class="class">
<dt id="generic_images.managers.AttachedImageManager">
<em class="property">
class </em><tt class="descclassname">generic_images.managers.</tt><tt class="descname">AttachedImageManager</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_images.managers.AttachedImageManager" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <a title="generic_utils.managers.GenericModelManager" class="reference" href="#generic_utils.managers.GenericModelManager"><tt class="xref docutils literal"><span class="pre">generic_utils.managers.GenericModelManager</span></tt></a></p>
<p>Manager with helpful functions for attached images</p>
<dl class="method">
<dt id="generic_images.managers.AttachedImageManager.get_main_for">
<tt class="descname">get_main_for</tt><big>(</big><em>model</em><big>)</big><a class="headerlink" href="#generic_images.managers.AttachedImageManager.get_main_for" title="Permalink to this definition"></a></dt>
<dd>Returns main image for given model</dd></dl>

</dd></dl>

<dl class="class">
<dt id="generic_images.managers.ImagesAndUserManager">
<em class="property">
class </em><tt class="descclassname">generic_images.managers.</tt><tt class="descname">ImagesAndUserManager</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_images.managers.ImagesAndUserManager" title="Permalink to this definition"></a></dt>
<dd><p>Useful manager for models that have AttachedImage (or subclass) field
and &#8216;injector=GenericIngector()&#8217; manager.</p>
<dl class="method">
<dt id="generic_images.managers.ImagesAndUserManager.select_with_main_images">
<tt class="descname">select_with_main_images</tt><big>(</big><em>limit=None</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_images.managers.ImagesAndUserManager.select_with_main_images" title="Permalink to this definition"></a></dt>
<dd>Select all objects with filters passed as kwargs.   
For each object it&#8217;s main image instance is accessible as <tt class="docutils literal"><span class="pre">object.main_image</span></tt>.
Results can be limited using <tt class="docutils literal"><span class="pre">limit</span></tt> parameter.
Selection is performed using only 2 or 3 sql queries.</dd></dl>

</dd></dl>

</div>
<div class="section" id="module-generic_images.forms">
<h4>Forms<a class="headerlink" href="#module-generic_images.forms" title="Permalink to this headline"></a></h4>
<dl class="class">
<dt id="generic_images.forms.AttachedImageForm">
<em class="property">
class </em><tt class="descclassname">generic_images.forms.</tt><tt class="descname">AttachedImageForm</tt><a class="headerlink" href="#generic_images.forms.AttachedImageForm" title="Permalink to this definition"></a></dt>
<dd><p>Bases: <tt class="xref docutils literal"><span class="pre">django.forms.models.ModelForm</span></tt></p>
<p>Simple form for AttachedImage model with <tt class="docutils literal"><span class="pre">image</span></tt> and <tt class="docutils literal"><span class="pre">caption</span></tt> fields.</p>
</dd></dl>

</div>
<div class="section" id="module-generic_images.fields">
<h4>Fields for denormalisation<a class="headerlink" href="#module-generic_images.fields" title="Permalink to this headline"></a></h4>
<p>django-generic-images provides fields for storing information about 
attached images count. Value is stored in model that images are 
attached to. Value is updated automatically when image is saved or deleted.
Access to this value is much faster than additional &#8220;count()&#8221; queries.</p>
<dl class="class">
<dt id="generic_images.fields.ImageCountField">
<em class="property">
class </em><tt class="descclassname">generic_images.fields.</tt><tt class="descname">ImageCountField</tt><big>(</big><em>native=None</em><big>)</big><a class="headerlink" href="#generic_images.fields.ImageCountField" title="Permalink to this definition"></a></dt>
<dd><p>Field with model&#8217;s attached images count.
Value of this field is updated automatically when 
image is added or removed. Access to this field
doesn&#8217;t produce additional &#8216;select count(*)&#8217; query,
data is stored in table.</p>
<p>Example 1:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">generic_images.fields</span> <span class="kn">import</span> <span class="n">ImageCountField</span>

<span class="k">class</span> <span class="nc">MyModel1</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c">#... fields definitions</span>
    <span class="n">image_count</span> <span class="o">=</span> <span class="n">ImageCountField</span><span class="p">()</span>
</pre></div>
</div>
<p>Example 2:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyModel2</span><span class="p">(</span><span class="n">models</span><span class="o">.</span><span class="n">Model</span><span class="p">):</span>
    <span class="c">#... fields definitions</span>
    <span class="n">image_count</span> <span class="o">=</span> <span class="n">ImageCountField</span><span class="p">(</span><span class="n">native</span><span class="o">=</span><span class="n">models</span><span class="o">.</span><span class="n">IntegerField</span><span class="p">(</span><span class="s">u&#39;MyModel2 Images count&#39;</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="mf">0</span><span class="p">))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="generic_images.fields.UserImageCountField">
<em class="property">
class </em><tt class="descclassname">generic_images.fields.</tt><tt class="descname">UserImageCountField</tt><big>(</big><em>native=None</em>, <em>user_attr='user'</em><big>)</big><a class="headerlink" href="#generic_images.fields.UserImageCountField" title="Permalink to this definition"></a></dt>
<dd><p>Field that should be put into user&#8217;s profile (AUTH_PROFILE_MODULE). 
It will contain number of images that are attached to corresponding User.</p>
<p>This field is useful when you want to use something like 
<a title="generic_images.fields.ImageCountField" class="reference" href="#generic_images.fields.ImageCountField"><tt class="xref docutils literal"><span class="pre">ImageCountField</span></tt></a> for <tt class="docutils literal"><span class="pre">User</span></tt> model. 
It is not possible to add a field to User model without 
duck punching (monkey patching). <tt class="docutils literal"><span class="pre">UserImageCountField</span></tt> should be 
put into user&#8217;s profile (same model as defined in AUTH_PROFILE_MODULE). 
It will contain number of images that are attached to corresponding User.
FK attribute to User model is considered <tt class="docutils literal"><span class="pre">'user'</span></tt> by default, but this 
can be overrided using <tt class="docutils literal"><span class="pre">user_attr</span></tt> argument to <tt class="docutils literal"><span class="pre">UserImageCountField</span></tt> 
constructor. As with <a title="generic_images.fields.ImageCountField" class="reference" href="#generic_images.fields.ImageCountField"><tt class="xref docutils literal"><span class="pre">ImageCountField</span></tt></a>, 
<tt class="docutils literal"><span class="pre">UserImageCountField</span></tt> constructor accepts also <tt class="docutils literal"><span class="pre">native</span></tt> argument - an 
underlying field.</p>
</dd></dl>

<dl class="function">
<dt id="generic_images.fields.force_recalculate">
<tt class="descclassname">generic_images.fields.</tt><tt class="descname">force_recalculate</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#generic_images.fields.force_recalculate" title="Permalink to this definition"></a></dt>
<dd><p>Recalculate all ImageCountField and UserImageCountField fields
in object <tt class="docutils literal"><span class="pre">obj</span></tt>.</p>
<p>This should be used if auto-updating of these fields was disabled for
some reason.</p>
<p>To disable auto-update when saving AttachedImage instance 
(for example when you need to save a lot of images and want to 
recalculate denormalised values only after all images are saved) use 
this pattern:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">image</span> <span class="o">=</span> <span class="n">AttachedImage</span><span class="p">(</span><span class="o">...</span><span class="p">)</span>
<span class="n">image</span><span class="o">.</span><span class="n">send_signal</span> <span class="o">=</span> <span class="bp">False</span>
<span class="n">image</span><span class="o">.</span><span class="n">save</span><span class="p">()</span>
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="module-generic_images.context_processors">
<h4>Context processors<a class="headerlink" href="#module-generic_images.context_processors" title="Permalink to this headline"></a></h4>
<dl class="function">
<dt id="generic_images.context_processors.thumbnail_types">
<tt class="descclassname">generic_images.context_processors.</tt><tt class="descname">thumbnail_types</tt><big>(</big><em>request</em><big>)</big><a class="headerlink" href="#generic_images.context_processors.thumbnail_types" title="Permalink to this definition"></a></dt>
<dd>A context processor to add possible thumbnail sizes to the current Context.
Useful for managing possible sorl.thumbnails thumbnail&#8217;s sizes</dd></dl>

</div>
</div>
<div class="section" id="generic-utils">
<h3>Generic Utils<a class="headerlink" href="#generic-utils" title="Permalink to this headline"></a></h3>
<div class="section" id="module-generic_utils.app_utils">
<h4>Pluggable app utils<a class="headerlink" href="#module-generic_utils.app_utils" title="Permalink to this headline"></a></h4>
<dl class="class">
<dt id="generic_utils.app_utils.PluggableSite">
<em class="property">
class </em><tt class="descclassname">generic_utils.app_utils.</tt><tt class="descname">PluggableSite</tt><big>(</big><em>instance_name</em>, <em>app_name</em>, <em>queryset=None</em>, <em>object_regex=None</em>, <em>lookup_field=None</em>, <em>extra_context=None</em>, <em>template_object_name='object'</em>, <em>has_edit_permission=&lt;function &lt;lambda&gt; at 0x1d75570&gt;</em>, <em>context_processors=None</em>, <em>object_getter=None</em><big>)</big><a class="headerlink" href="#generic_utils.app_utils.PluggableSite" title="Permalink to this definition"></a></dt>
<dd><p>Base class for reusable apps.
The approach is similar to django AdminSite.
For usage case please check photo_albums app.</p>
<dl class="method">
<dt id="generic_utils.app_utils.PluggableSite.check_permissions">
<tt class="descname">check_permissions</tt><big>(</big><em>request</em>, <em>object</em><big>)</big><a class="headerlink" href="#generic_utils.app_utils.PluggableSite.check_permissions" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="generic_utils.app_utils.PluggableSite.get_common_context">
<tt class="descname">get_common_context</tt><big>(</big><em>obj</em><big>)</big><a class="headerlink" href="#generic_utils.app_utils.PluggableSite.get_common_context" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

<dl class="method">
<dt id="generic_utils.app_utils.PluggableSite.make_regex">
<tt class="descname">make_regex</tt><big>(</big><em>url</em><big>)</big><a class="headerlink" href="#generic_utils.app_utils.PluggableSite.make_regex" title="Permalink to this definition"></a></dt>
<dd><p>Make regex string for <tt class="docutils literal"><span class="pre">PluggableSite</span></tt> urlpatterns: prepend url
with parent object&#8217;s url and app name.</p>
<p>See also: <a class="reference" href="http://code.djangoproject.com/ticket/11559">http://code.djangoproject.com/ticket/11559</a>.</p>
</dd></dl>

<dl class="method">
<dt id="generic_utils.app_utils.PluggableSite.patterns">
<tt class="descname">patterns</tt><big>(</big><big>)</big><a class="headerlink" href="#generic_utils.app_utils.PluggableSite.patterns" title="Permalink to this definition"></a></dt>
<dd><p>This method should return url patterns (like urlpatterns variable in
<tt class="docutils literal"><span class="pre">urls.py</span></tt>). It is helpful to construct regex with
<a title="generic_utils.app_utils.PluggableSite.make_regex" class="reference" href="#generic_utils.app_utils.PluggableSite.make_regex"><tt class="xref docutils literal"><span class="pre">make_regex()</span></tt></a> method.
Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">return</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;photo_albums.views&#39;</span><span class="p">,</span>
                    <span class="n">url</span><span class="p">(</span>
                        <span class="bp">self</span><span class="o">.</span><span class="n">make_regex</span><span class="p">(</span><span class="s">&#39;/&#39;</span><span class="p">),</span>
                        <span class="s">&#39;show_album&#39;</span><span class="p">,</span>
                        <span class="p">{</span><span class="s">&#39;album_site&#39;</span><span class="p">:</span> <span class="bp">self</span><span class="p">},</span>
                        <span class="n">name</span> <span class="o">=</span> <span class="s">&#39;show_album&#39;</span><span class="p">,</span>
                    <span class="p">),</span>
               <span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="method">
<dt id="generic_utils.app_utils.PluggableSite.reverse">
<tt class="descname">reverse</tt><big>(</big><em>url</em>, <em>args=None</em>, <em>kwargs=None</em><big>)</big><a class="headerlink" href="#generic_utils.app_utils.PluggableSite.reverse" title="Permalink to this definition"></a></dt>
<dd>Reverse an url taking self.app_name in account</dd></dl>

<dl class="attribute">
<dt id="generic_utils.app_utils.PluggableSite.urls">
<tt class="descname">urls</tt><a class="headerlink" href="#generic_utils.app_utils.PluggableSite.urls" title="Permalink to this definition"></a></dt>
<dd><p>Use it in <tt class="docutils literal"><span class="pre">urls.py</span></tt>.
Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">urlpatterns</span> <span class="o">+=</span> <span class="n">patterns</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">,</span> <span class="n">url</span><span class="p">(</span><span class="s">r&#39;^my_site/&#39;</span><span class="p">,</span> <span class="n">include</span><span class="p">(</span><span class="n">my_pluggable_site</span><span class="o">.</span><span class="n">urls</span><span class="p">)),)</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

<dl class="function">
<dt id="generic_utils.app_utils.get_site_decorator">
<tt class="descclassname">generic_utils.app_utils.</tt><tt class="descname">get_site_decorator</tt><big>(</big><em>site_param='site'</em>, <em>obj_param='obj'</em>, <em>context_param='context'</em><big>)</big><a class="headerlink" href="#generic_utils.app_utils.get_site_decorator" title="Permalink to this definition"></a></dt>
<dd><p>It is a function that returns decorator factory useful for PluggableSite
views. This decorator factory returns decorator that do some
boilerplate work and make writing PluggableSite views easier.
It passes PluggableSite instance to decorated view,
retreives and passes object that site is attached to and passes
common context. It also passes and all the decorator factory&#8217;s
keyword arguments.</p>
<p>For example usage please check photo_albums.views.</p>
<p>Btw, this decorator seems frightening for me. It feels that
&#8220;views as PluggableSite methods&#8221; approach can easily make this decorator
obsolete. But for now it just works.</p>
</dd></dl>

<dl class="function">
<dt id="generic_utils.app_utils.simple_getter">
<tt class="descclassname">generic_utils.app_utils.</tt><tt class="descname">simple_getter</tt><big>(</big><em>queryset</em>, <em>object_regex=None</em>, <em>lookup_field=None</em><big>)</big><a class="headerlink" href="#generic_utils.app_utils.simple_getter" title="Permalink to this definition"></a></dt>
<dd>Returns simple object_getter function for use with PluggableSite.
It takes &#8216;queryset&#8217; with QuerySet or Model instance, &#8216;object_regex&#8217; with
url regex and &#8216;lookup_field&#8217; with lookup field.</dd></dl>

</div>
<div class="section" id="id1">
<h4>Models<a class="headerlink" href="#id1" title="Permalink to this headline"></a></h4>
<dl class="class">
<dt id="generic_utils.models.GenericModelBase">
<em class="property">
class </em><tt class="descclassname">generic_utils.models.</tt><tt class="descname">GenericModelBase</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_utils.models.GenericModelBase" title="Permalink to this definition"></a></dt>
<dd><p>Abstract base class for models that will be attached using
generic relations.</p>
<dl class="attribute">
<dt id="generic_utils.app_utils.GenericModelBase.object_id">
<tt class="descname">object_id</tt><a class="headerlink" href="#generic_utils.app_utils.GenericModelBase.object_id" title="Permalink to this definition"></a></dt>
<dd>A PositiveIntegerField containing the primary key of the object the model
is attached to.</dd></dl>

<dl class="attribute">
<dt id="generic_utils.app_utils.GenericModelBase.content_type">
<tt class="descname">content_type</tt><a class="headerlink" href="#generic_utils.app_utils.GenericModelBase.content_type" title="Permalink to this definition"></a></dt>
<dd>A ForeignKey to ContentType; this is the type of the object the model is 
attached to.</dd></dl>

<dl class="attribute">
<dt id="generic_utils.models.GenericModelBase.content_object">
<tt class="descname">content_object</tt><a class="headerlink" href="#generic_utils.models.GenericModelBase.content_object" title="Permalink to this definition"></a></dt>
<dd>A GenericForeignKey attribute pointing to the object the comment is
attached to. You can use this to get at the related object
(i.e. my_model.content_object). Since this field is a
GenericForeignKey, it’s actually syntactic sugar on top of two underlyin
attributes, described above.</dd></dl>

<dl class="attribute">
<dt id="generic_utils.models.GenericModelBase.injector">
<tt class="descname">injector</tt><a class="headerlink" href="#generic_utils.models.GenericModelBase.injector" title="Permalink to this definition"></a></dt>
<dd><a title="generic_utils.managers.GenericInjector" class="reference" href="#generic_utils.managers.GenericInjector"><tt class="xref docutils literal"><span class="pre">GenericInjector</span></tt></a> manager.</dd></dl>

<dl class="attribute">
<dt id="generic_utils.models.GenericModelBase.objects">
<tt class="descname">objects</tt><a class="headerlink" href="#generic_utils.models.GenericModelBase.objects" title="Permalink to this definition"></a></dt>
<dd>Default manager. It is of type <a title="generic_utils.managers.GenericModelManager" class="reference" href="#generic_utils.managers.GenericModelManager"><tt class="xref docutils literal"><span class="pre">GenericModelManager</span></tt></a>.</dd></dl>

</dd></dl>

<dl class="class">
<dt id="generic_utils.models.TrueGenericModelBase">
<em class="property">
class </em><tt class="descclassname">generic_utils.models.</tt><tt class="descname">TrueGenericModelBase</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_utils.models.TrueGenericModelBase" title="Permalink to this definition"></a></dt>
<dd>It is similar to <a title="generic_utils.models.GenericModelBase" class="reference" href="#generic_utils.models.GenericModelBase"><tt class="xref docutils literal"><span class="pre">GenericModelBase</span></tt></a> but
with TextField object_id instead of PositiveIntegerField.</dd></dl>

</div>
<div class="section" id="module-generic_utils.managers">
<h4>Generic relation helpers<a class="headerlink" href="#module-generic_utils.managers" title="Permalink to this headline"></a></h4>
<dl class="class">
<dt id="generic_utils.managers.GenericInjector">
<em class="property">
class </em><tt class="descclassname">generic_utils.managers.</tt><tt class="descname">GenericInjector</tt><big>(</big><em>fk_field='object_id'</em>, <em>ct_field='content_type'</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_utils.managers.GenericInjector" title="Permalink to this definition"></a></dt>
<dd><p>RelatedInjector but for GenericForeignKey&#8217;s.
Manager for selecting all generic-related objects in one (two) SQL queries.
Selection is performed for a list of objects. Resulting data is aviable as attribute
of original model. Only one instance per object can be selected. Example usage:
select (and make acessible as user.avatar) all avatars for a list of user when
avatars are AttachedImage&#8217;s attached to User model with is_main=True attributes.</p>
<p>Example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django.contrib.auth.models</span> <span class="kn">import</span> <span class="n">User</span>
<span class="kn">from</span> <span class="nn">generic_images.models</span> <span class="kn">import</span> <span class="n">AttachedImage</span>

<span class="n">users</span> <span class="o">=</span> <span class="n">User</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()[:</span><span class="mf">10</span><span class="p">]</span>
<span class="n">AttachedImage</span><span class="o">.</span><span class="n">injector</span><span class="o">.</span><span class="n">inject_to</span><span class="p">(</span><span class="n">users</span><span class="p">,</span> <span class="s">&#39;avatar&#39;</span><span class="p">,</span> <span class="n">is_main</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>

<span class="c"># i=0..9: users[i].avatar is AttachedImage objects with is_main=True.</span>
<span class="c"># If there is no such AttachedImage (user doesn&#39;t have an avatar),</span>
<span class="c"># users[i].avatar is None</span>
</pre></div>
</div>
<dl class="docutils">
<dt>For this example 2 or 3 sql queries will be executed:</dt>
<dd><ol class="first last arabic simple">
<li>one query for selecting 10 users,</li>
<li>one query for selecting all avatars (images with is_main=True) for selected users</li>
<li>and maybe one query for selecting content-type for User model</li>
</ol>
</dd>
</dl>
<p>One can reuse GenericInjector manager for other models that are supposed to
be attached via generic relationship. It can be considered as an addition to
GFKmanager and GFKQuerySet from djangosnippets for different use cases.</p>
<dl class="method">
<dt id="generic_utils.managers.GenericInjector.inject_to">
<tt class="descname">inject_to</tt><big>(</big><em>objects</em>, <em>field_name</em>, <em>get_inject_object=&lt;function &lt;lambda&gt; at 0x1a4a4b0&gt;</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_utils.managers.GenericInjector.inject_to" title="Permalink to this definition"></a></dt>
<dd><dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">objects</span></tt> is an iterable. Images (or other generic-related model instances)</dt>
<dd>will be attached to elements of this iterable.</dd>
</dl>
<p><tt class="docutils literal"><span class="pre">field_name</span></tt> is the attached object attribute name</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">get_inject_object</span></tt> is a callable that takes object in <cite>objects</cite> iterable.</dt>
<dd>Image will be available as an attribute of the result of
<cite>get_injector_object(object)</cite>. Images attached to <cite>get_injector_object(object)</cite>
will be selected.</dd>
</dl>
<p>All other kwargs will be passed as arguments to queryset filter function.</p>
<p>Example: you have a list of comments. Each comment has &#8216;user&#8217; attribute.
You want to fetch 10 comments and their authors with avatars. Avatars should
be accessible as <cite>user.avatar</cite>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">comments</span> <span class="o">=</span> <span class="n">Comment</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">all</span><span class="p">()</span><span class="o">.</span><span class="n">select_related</span><span class="p">(</span><span class="s">&#39;user&#39;</span><span class="p">)[:</span><span class="mf">10</span><span class="p">]</span>
<span class="n">AttachedImage</span><span class="o">.</span><span class="n">injector</span><span class="o">.</span><span class="n">inject_to</span><span class="p">(</span><span class="n">comments</span><span class="p">,</span> <span class="s">&#39;avatar&#39;</span><span class="p">,</span> <span class="k">lambda</span> <span class="n">obj</span><span class="p">:</span> <span class="n">obj</span><span class="o">.</span><span class="n">user</span><span class="p">,</span> <span class="n">is_main</span><span class="o">=</span><span class="bp">True</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="generic_utils.managers.GenericModelManager">
<em class="property">
class </em><tt class="descclassname">generic_utils.managers.</tt><tt class="descname">GenericModelManager</tt><big>(</big><em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_utils.managers.GenericModelManager" title="Permalink to this definition"></a></dt>
<dd><p>Manager with for_model method.</p>
<dl class="method">
<dt id="generic_utils.managers.GenericModelManager.for_model">
<tt class="descname">for_model</tt><big>(</big><em>model</em>, <em>content_type=None</em><big>)</big><a class="headerlink" href="#generic_utils.managers.GenericModelManager.for_model" title="Permalink to this definition"></a></dt>
<dd>Returns all objects that are attached to given model</dd></dl>

</dd></dl>

<dl class="class">
<dt id="generic_utils.managers.RelatedInjector">
<em class="property">
class </em><tt class="descclassname">generic_utils.managers.</tt><tt class="descname">RelatedInjector</tt><big>(</big><em>fk_field='object_id'</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_utils.managers.RelatedInjector" title="Permalink to this definition"></a></dt>
<dd><p>Manager that can emulate <tt class="docutils literal"><span class="pre">select_related</span></tt> fetching
reverse relations using 1 additional SQL query.</p>
<dl class="method">
<dt id="generic_utils.managers.RelatedInjector.inject_to">
<tt class="descname">inject_to</tt><big>(</big><em>objects</em>, <em>field_name</em>, <em>get_inject_object=&lt;function &lt;lambda&gt; at 0x1a4a3f0&gt;</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#generic_utils.managers.RelatedInjector.inject_to" title="Permalink to this definition"></a></dt>
<dd><dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">objects</span></tt> is an iterable. Related objects</dt>
<dd>will be attached to elements of this iterable.</dd>
</dl>
<p><tt class="docutils literal"><span class="pre">field_name</span></tt> is the attached object attribute name</p>
<dl class="docutils">
<dt><tt class="docutils literal"><span class="pre">get_injector_object</span></tt> is a callable that takes object in <cite>objects</cite></dt>
<dd>iterable. Related objects will be available as an attribute of the
result of <tt class="docutils literal"><span class="pre">get_inject_object(obj)</span></tt>. It is assumed that <tt class="docutils literal"><span class="pre">fk_field</span></tt>
points to  <tt class="docutils literal"><span class="pre">get_inject_object(obj)</span></tt>.</dd>
</dl>
<p>All other kwargs will be passed as arguments to queryset filter function.</p>
<p>For example, we need to prefetch user profiles when we display a list of
comments:</p>
<div class="highlight-python"><pre># models.py
class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    info = models.CharField(max_length=100)
    objects = models.Manager()
    injector = RelatedInjector(fk_field='user')

# views.py
def show_comments(request, obj_id):
    ...
    comments = list(Comment.objects.for_model(obj).select_related('user'))
    UserProfile.injector.inject_to(comments, '_profile_cache',
                                   lambda comment: comment.user)

    return direct_to_template('comment_list.html', {'comments': comments})

# in comment_list.html
{% for comment in comments %}
    &lt;h3&gt;{{ comment.user }}&lt;/h3&gt;
    &lt;h4&gt;{{ comment.user.get_profile.info }}&lt;/h4&gt;
    {{ comment.comment|linebreaks }}
{% endfor %}</pre>
</div>
<p><tt class="docutils literal"><span class="pre">comment.user</span></tt> attribute will be selected using <tt class="docutils literal"><span class="pre">select_related</span></tt> and
<tt class="docutils literal"><span class="pre">comment.user._profile_cache</span></tt> (exposed by get_profile method) will be
selected by our injector. So there will be only 2 SQL queries for
selecting all comments with users and user profiles.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="module-generic_utils.templatetags">
<h4>Template tag helpers<a class="headerlink" href="#module-generic_utils.templatetags" title="Permalink to this headline"></a></h4>
<dl class="exception">
<dt id="generic_utils.templatetags.InvalidParamsError">
<em class="property">
exception </em><tt class="descclassname">generic_utils.templatetags.</tt><tt class="descname">InvalidParamsError</tt><a class="headerlink" href="#generic_utils.templatetags.InvalidParamsError" title="Permalink to this definition"></a></dt>
<dd>Custom exception class to distinguish usual TemplateSyntaxErrors 
and validation errors for templatetags introduced by <tt class="docutils literal"><span class="pre">validate_params</span></tt>
function</dd></dl>

<dl class="function">
<dt id="generic_utils.templatetags.validate_params">
<tt class="descclassname">generic_utils.templatetags.</tt><tt class="descname">validate_params</tt><big>(</big><em>bits</em>, <em>arguments_count</em>, <em>keyword_positions</em><big>)</big><a class="headerlink" href="#generic_utils.templatetags.validate_params" title="Permalink to this definition"></a></dt>
<dd>Raises exception if passed params (<cite>bits</cite>) do not match signature.
Signature is defined by <cite>arguments_count</cite> (acceptible number of params) and
keyword_positions (dictionary with positions in keys and keywords in values,
for ex. {2:&#8217;by&#8217;, 4:&#8217;of&#8217;, 5:&#8217;type&#8217;, 7:&#8217;as&#8217;}).</dd></dl>

</div>
<div class="section" id="module-generic_utils.test_helpers">
<h4>Test helpers<a class="headerlink" href="#module-generic_utils.test_helpers" title="Permalink to this headline"></a></h4>
<dl class="class">
<dt id="generic_utils.test_helpers.ViewTest">
<em class="property">
class </em><tt class="descclassname">generic_utils.test_helpers.</tt><tt class="descname">ViewTest</tt><big>(</big><em>methodName='runTest'</em><big>)</big><a class="headerlink" href="#generic_utils.test_helpers.ViewTest" title="Permalink to this definition"></a></dt>
<dd><p>TestCase for view testing</p>
<dl class="method">
<dt id="generic_utils.test_helpers.ViewTest.check_login_required">
<tt class="descname">check_login_required</tt><big>(</big><em>url_name</em>, <em>kwargs=None</em>, <em>current_app=None</em><big>)</big><a class="headerlink" href="#generic_utils.test_helpers.ViewTest.check_login_required" title="Permalink to this definition"></a></dt>
<dd>Check if response is a redirect to login page (ignoring GET variables)</dd></dl>

<dl class="method">
<dt id="generic_utils.test_helpers.ViewTest.check_url">
<tt class="descname">check_url</tt><big>(</big><em>url_name</em>, <em>status=200</em>, <em>kwargs=None</em>, <em>current_app=None</em><big>)</big><a class="headerlink" href="#generic_utils.test_helpers.ViewTest.check_url" title="Permalink to this definition"></a></dt>
<dd>check_url a URL and require a specific status code before proceeding</dd></dl>

</dd></dl>

</div>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h3><a href="">Table Of Contents</a></h3>
            <ul>
<li><a class="reference" href="">django-generic-images&#8217;s documentation</a><ul>
<li><a class="reference" href="#installation">Installation</a></li>
<li><a class="reference" href="#usage">Usage</a><ul>
<li><a class="reference" href="#generic-images">Generic Images</a><ul>
<li><a class="reference" href="#models">Models</a></li>
<li><a class="reference" href="#module-generic_images.admin">Admin</a></li>
<li><a class="reference" href="#module-generic_images.managers">Managers</a></li>
<li><a class="reference" href="#module-generic_images.forms">Forms</a></li>
<li><a class="reference" href="#module-generic_images.fields">Fields for denormalisation</a></li>
<li><a class="reference" href="#module-generic_images.context_processors">Context processors</a></li>
</ul>
</li>
<li><a class="reference" href="#generic-utils">Generic Utils</a><ul>
<li><a class="reference" href="#module-generic_utils.app_utils">Pluggable app utils</a></li>
<li><a class="reference" href="#id1">Models</a></li>
<li><a class="reference" href="#module-generic_utils.managers">Generic relation helpers</a></li>
<li><a class="reference" href="#module-generic_utils.templatetags">Template tag helpers</a></li>
<li><a class="reference" href="#module-generic_utils.test_helpers">Test helpers</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>

            <h4>Next topic</h4>
            <p class="topless"><a href="upgrade.html"
                                  title="next chapter">Upgrade notes</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="_sources/index.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="upgrade.html" title="Upgrade notes"
             >next</a> |</li>
        <li><a href="">django-generic-images v0.35 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2009, Mikhail Korobov.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.2.
    </div>
  </body>
</html>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.