📜 ⬆️ ⬇️

5 useful batteries for Django

Developing constantly face various tasks that are often not solved in the forehead. But many problems have already been solved by someone - you just need to find this solution.

So, day by day, I collected a small collection of batteries that made my life much easier. What I want to share:

django-select2


As the name implies, the battery allows the use of the popular select2 library in its Django projects.
The application includes a set of form fields and widgets for all occasions.
')
It presents two types of widgets:

Github
Documentation

django-bitfield


Allows you to store lists of values ​​in the model field in the form of a bitmap, the length of which is limited by the maximum BigInt length in the database used.

Example of use (taken from the documentation):

from bitfield import BitField class MyModel(models.Model): flags = BitField(flags=( 'awesome_flag', 'flaggy_foo', 'baz_bar', )) 

BUT

The battery has a significant, in my opinion, drawback - it is impossible to display flags using standard means in an understandable form either in the admin panel or in its own templates. For myself, this problem I decided as follows:

models.py:

 from django.db import models from django.utils.translation import ugettext_lazy as _ from bitfield import BitField class MyModel(models.Model): FLAGS = ( ('flag1', _('flag 1 description')), ('flag2', _('flag 2 description')), ('flag3', _('flag 3 description')), ) flags = BitField([k for k, v in FLAGS]) 


forms.py

 from django import forms from .models import MyModel class MyModelForm(forms.ModelForm): def _set_choices(self, field, choices): self.fields[field].widget.choices = choices def __init__(self, *args, **kwargs): super(MyModelForm, self).__init__(*args, **kwargs) self._set_choices('flags', MyModel.FLAGS) 


admin.py

 from django.contrib import admin from .forms import MyModelForm from .models import MyModel class MyModelAdmin(admin.ModelAdmin): form = MyModelForm admin.site.register(MyModel, MyModelAdmin) 

Github

django-tagging


There are several tag implementations for Django, but I would like to draw your attention to the machinetags branch from this repository .
In this implementation of tags, the ability to divide them between namespaces (namespaces) is added and, accordingly, use different sets of tags for different fields.
It is also possible to assign additional values ​​to the tag and store sets of identical tags, but with different values. Honestly, I have not found this application yet.

My fork on github with some fixes.

But the module itself does not support autocompletion - and this, you see, is a very useful function. There are many implementations: one , two , three - these are only the first links from Google.
But none of them support the namespaces of the django-tagging version described above, so I took the liberty to eliminate this flaw. And so my fork appeared ...

django-tagging-autosuggest


I will not rewrite the documentation, so I’ll just give you an example of use:

The application must be added to the INSTALLED_APPS and to the project urls.py:
 from django.conf.urls import patterns, include, url urlpatterns = patterns('', url(r'^tagging/', include('tagging_autosuggest.urls')), ) 


And add the required fields to the model:
 from django.db import models from tagging_autosuggest.fields import TagAutosuggestField class MyModel(models.Model): tags1 = TagAutosuggestField(namespace='ns1', max_length=300) tags2 = TagAutosuggestField(namespace='ns2', max_length=300) 


Result:

Github

django-autocomplete-light


Another battery that makes it easier to work with lists. It creates convenient widgets with autocompletion in the admin panel for fields such as CharField, ForeignKey, ManyToMany, as well as for generic links (ForeignKey and M2M).

Examples of use can be found in the test project .

And this is how GenericForeignKey looks like in a real project:

Github
Documentation

My humble contribution to opensource: github.com/Yuego?tab=repositories

Error messages, typographical errors, etc., are traditionally received in a personal.

Source: https://habr.com/ru/post/187312/


All Articles