djangosnippets.org: Latest snippets tagged with 'mixin'http://djangosnippets.org/tags/mixin/2018-12-12T09:19:21.107136-06:00Different Response Data/Serializer for DRF Views/Viewsets
2018-12-12T09:19:21.107136-06:00shivanshs9http://djangosnippets.org/snippets/10656/<p>Mixin for Django Rest Framework View/Viewsets to use different response data/serializers without unnecessary hacks.</p>
<p>Just extend your DRFs view/viewset with this mixin after the DRF's APIView (sub)classes.</p>
Freely redistributableMixin to enable admin permissions without using PermissionsMixin and models
2016-11-16T15:33:42.431238-06:00thneehttp://djangosnippets.org/snippets/10593/<p>Implements necessary permission checks on a user model to be compatible with django admin, but just return true on all permissions without actually checking it against anything. Useful when you have a user model that should always be allowed to use django admin, and you don't care about using django's …</p>
Freely redistributablePaginationMixin: Get a controlled paginate_by value from the querystring or use a default.
2015-10-05T10:02:01.693942-05:00AgustinLadohttp://djangosnippets.org/snippets/10532/<p>What the docstring says. To not use some functionality, e.g. managing the value in the User's Profile model, delete the corresponding lines (when getting the page_size and when saving it.</p>
<p>Add the Mixin before the View class. e.g.: <code>class ItemList(PaginationMixin, generic.ListView):</code></p>
Freely redistributableRemove a clause from a queryset
2015-07-16T21:03:37.791043-05:00wahunekehttp://djangosnippets.org/snippets/10515/<p>I want to create Mixins for QuerySet objects that will by default filter out certain records (e.g. filter out "deleted" records, or filter out "unapproved" records, etc). I'd like those to be separate independent mixins. So in each of those, I override all() to filter out deleted or unapproved, etc. …</p>
Freely redistributableCancel URL Mixin
2015-05-20T04:24:54.190419-05:00halfnibblehttp://djangosnippets.org/snippets/10488/<p><strong>CancelMixin</strong></p>
<p>A simple mixin to use with <code>generic.CreateView</code> and <code>generic.UpdateView</code> view form templates to effortlessly implement a "Cancel" button. </p>
<p>This smart mixin will add a URL to your context, <code>{{ cancel_url }}</code>, that can be used as a cancel link in your form template. If no referrer URL is …</p>
Freely redistributableCBV: PreviewMixin
2015-03-29T12:36:15.828121-05:00jannhhttp://djangosnippets.org/snippets/10475/<p>PreviewMixin adds a preview page for Django's CBV (FormView, UpdateView, CreateView). After a form has been submitted, it is returned again, optionally in a different template to confirm. If the form is submitted with the same data, the default "form_valid" function is executed.</p>
<p>Features:
1. <code>process_preview</code> - function executed after …</p>
Freely redistributableSeeded Randomized Querysets w/ Pagination Mixin
2015-02-25T15:51:23.691037-06:00ScottBarkmanhttp://djangosnippets.org/snippets/10468/<p>Mixin to support pagination when randomizing querysets. </p>
<p>Requirements: Postgres, Django Sessions</p>
<p>Note: This shouldn't be used on large complex datasets. It utilizes the relatively slow method of '?' randomized sorting. Use with caution. </p>
<p>Todo: MySQL support, Support for larger datasets</p>
Freely redistributableDjango BasicAuthMixin
2014-06-08T05:06:10.760337-05:00icrnihttp://djangosnippets.org/snippets/3073/<p>Basic Authentication mixin for Django. Use in your class based views.</p>
Freely redistributableModel Mixin to Save Only Changed Fields
2013-08-25T23:18:29.836753-05:00karanlyonshttp://djangosnippets.org/snippets/2985/<p><strong><a href="https://github.com/karanlyons/django-save-the-change">Improved and Released as Save The Change.</a></strong></p>
<p>Django 1.5 added the <code>update_fields</code> <code>kwarg</code> to <code>Model.save()</code>, which allows the developer to specify that only certain fields should actually be committed to the database. However, Django provides no way to automatically commit only changed fields if they're not specified.</p>
<p>This mixin …</p>
Freely redistributableBasic PDF view mixin and utils using reportlab.
2013-07-19T02:13:35.464470-05:00elequhttp://djangosnippets.org/snippets/2975/<p>Simplified version of the snippet that renders model to PDF <a href="http://djangosnippets.org/snippets/2540/">http://djangosnippets.org/snippets/2540/</a> </p>
<p>This PDF view mixin for Django Class Based Views.</p>
<p>See working project example: https://github.com/elena/django-pdfmixin-example</p>
<hr />
<p>This is based on the case scenario where you have a model which has a <code>DetailView</code>.</p>
<p>You then construct a bespoke PDF for the …</p>
Freely redistributableClass-based view mixin for flatpages
2013-02-26T07:56:24.001204-06:00schwukhttp://djangosnippets.org/snippets/2896/<p>Allows you to include content from flatpages in class-based views. You can specify the url for the flatpage you want, or let it be determined by request.path.</p>
Freely redistributableRelatedMixin for Details and Updates with Related Object Lists
2012-05-22T14:28:39.648001-05:00christhekeelehttp://djangosnippets.org/snippets/2756/<p>Code for a RelatedMixin I whipped up, useful in instances where you wish to expose details of a single object, including a related group of owned objects, in the same view. Works well with Django's generic DetailView and UpdateView, or any subclass of SingleObjectMixin.</p>
<p>It's a little cleaner than overriding …</p>
Freely redistributableUnit testing Django model mixins
2012-01-26T13:43:07.672809-06:00michaelmiorhttp://djangosnippets.org/snippets/2663/<p>This facilitates unit tests for model mixins in Django. For more info, see <a href="https://michael.mior.ca/blog/unit-testing-django-model-mixins/">my blog</a>.</p>
Freely redistributableView mixin and utils to generate PDF documents from html using xhtml2pdf
2011-09-06T10:17:53.590136-05:00frankbanhttp://djangosnippets.org/snippets/2540/<p>View mixin and utils to generate PDF documents from html using <em>xhtml2pdf</em>.</p>
<p>The most interesting thing here is <em>PDFTemplateResponseMixin</em>.
Adding this mixin to class based views allows automatic pdf generation using
the view context and a customized template.</p>
<p>There is also the lower level function <em>render_to_pdf</em>, similar …</p>
Freely redistributableModel Locking Mixin & Decorator (MySQL Advisory Locks)
2011-05-20T15:39:27.902324-05:00piohttp://djangosnippets.org/snippets/2443/<p>This code provides a mixin and decorator which, when used together, can provide advisory locking on model methods. It provides locking by using MySQL's advisory lock system. See the example at the bottom of the code.<br />
</p>
<p>This is a convenient and easy way to guarantee your model methods have exclusive …</p>
Freely redistributable