snipt

Ctrl+h for KB shortcuts

Python

Django Admin M2M Prefetch

1
2
3
4
5
6
    def formfield_for_manytomany(self, db_field, request, **kwargs):
        if db_field.__class__.__name__ == "ManyToManyField" and \
            db_field.rel.to.__name__ == self.model.__name__:
            kwargs['queryset'] = db_field.rel.to.objects.prefetch_related("user")
        return super(UserProfileInline, self).formfield_for_manytomany(
            db_field, request, **kwargs)
https://snipt.net/embed/f6c9d40fe2fbd6120c7b7a7202c33099/
https://snipt.net/raw/f6c9d40fe2fbd6120c7b7a7202c33099/
f6c9d40fe2fbd6120c7b7a7202c33099
python
Python
6
2016-09-27T13:45:40
True
False
False
Aug 06, 2013 at 03:23 PM
/api/public/snipt/72332/
django-admin-m2m-prefetch
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a> <span class="k">def</span> <span class="nf">formfield_for_manytomany</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span> </span><span id="L-2"><a name="L-2"></a> <span class="k">if</span> <span class="n">db_field</span><span class="o">.</span><span class="n">__class__</span><span class="o">.</span><span class="n">__name__</span> <span class="o">==</span> <span class="s">&quot;ManyToManyField&quot;</span> <span class="ow">and</span> \ </span><span id="L-3"><a name="L-3"></a> <span class="n">db_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">__name__</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">model</span><span class="o">.</span><span class="n">__name__</span><span class="p">:</span> </span><span id="L-4"><a name="L-4"></a> <span class="n">kwargs</span><span class="p">[</span><span class="s">&#39;queryset&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">db_field</span><span class="o">.</span><span class="n">rel</span><span class="o">.</span><span class="n">to</span><span class="o">.</span><span class="n">objects</span><span class="o">.</span><span class="n">prefetch_related</span><span class="p">(</span><span class="s">&quot;user&quot;</span><span class="p">)</span> </span><span id="L-5"><a name="L-5"></a> <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">UserProfileInline</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">formfield_for_manytomany</span><span class="p">(</span> </span><span id="L-6"><a name="L-6"></a> <span class="n">db_field</span><span class="p">,</span> <span class="n">request</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span> </span></pre></div> </td></tr></table>
"Many To Many", Django, M2M, ManyToMany, Python, Queryset
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.