A decorator to add a GUID Field to a Django Model. There are other bits of code out there that do similar things, but it was important for the field to have a unique value _before_ it is saved in the database. The contribute_to_class method therefore registers the field class with the post_init signal of the class it's being added to. The handler for that signal is where field initialization is done.
Позволяет получить типизированный словарь из входных параметров.
Может быть использован, например, для дальнейшей передаче параметров в objects.filter(**rez).
Since Django 1.4 you can create your own filters for change list view.
If you want to show just used/related items in filter chooser you can use this snippet.
Original idea from [here](http://jmduke.net/post/39953950546/custom-admin-filters-in-django). Big thanks to author.
Improved class names for better clarity and use of model_admin.model instead of hardcoded model name.
In example you can see two models - City and Country. City has ForeignKey to Country.
If you use regular list_filter = ('country',) you will have all the countries in the chooser. This snippet however filters only related countries - the ones that have at least one relation to city.
Django's `floatformat` is a good way to format a number if you require a specific amount of decimals. It is, however, very slow. In testing each `floatformat` call took 200–250 us, which means it'll take a second to render a page that floatformats 4000 numbers.
Much of the time comes from using `Decimals`. I looked at using the `cdecimal` module, and while it improved the speed, each call still clocked in at between 80 and 100 us.
`fast_floatformat` is not locale aware, and doesn't look at Django settings for USE_THOUSAND_SEPARATOR, but it'll take between 1.2 and 3 us per call for ints, floats and strings, and about 12 us per call for Decimals, giving you up to 800000 floatformatted numbers per second.
All I wanted was for one to one compilation of coffeescript to javascript.
* Without special templatetags
* Without specifying explicit bundles
* Served dynamically in development
* Compiled by collectstatic for producton
This code is the minimum required for this.
There are two things to take into account:
* list method to find coffeescript files to compile for collectstatic
* find method to find coffeescript equivalent for a js file for django.contrib.staticfiles.views.serve.
The list method will use the list method on all finders that come before it in STATICFILES_FINDERS to find all the files that end with .coffee and will return the equivalent .js path with a storage class that knows how to compile the coffeescript.
The find method will use the find method on all finders that come before it in STATICFILES_FINDERS to locate the coffeescript file that is actually being requested.
It will then compile the coffeescript into a file in settings.CACHE_DIR before serving that file.
This management command is run like this: `./manage.py -a someapp filename.cfg`
it looks in `someapp`'s directory for a file called `/config/filename.cfg` with the format explained in the help text, and creates the model instances described in the config file.
It uses the configobj module.
this would be an example config file:
[project.Profile]
[[fields]]
receive_notifications = False
[[children]]
[[[auth.User]]]
[[[[fields]]]]
username = AnnonymousUser
password = ! # set unusable password. There's no way yet to hash and set a given password
email = [email protected]
This tag is equivalent to {% cycle %} but resets when we exit the
containing loop.
See Django ticket "Cycle tag should reset after it steps out of scope"
[https://code.djangoproject.com/ticket/5908](https://code.djangoproject.com/ticket/5908)
This code is a lightly modified version of Simon Litchfield's
attachment on that ticket.
This snippet loads data from JSON files into a MongoDB database.
The code is related with the other snippet [MongoDB data dump](http://djangosnippets.org/snippets/2872/).
To get it working, just create a ``MONGODB_NAME`` variable in settings, holding the name of your Mongo database. This can be edited to fit more your needs. The snippet requires ``Pymongo``, since it uses its bson module and the ``MongoClient``.
This Django management command just dumps data from a given MongoDB collection into a JSON file.
To get it working, just create a ``MONGODB_NAME`` variable in settings, holding the name of your Mongo database. This can be edited to fit more your needs. The snippet requires Pymongo, since it uses its ``bson`` module and the ``MongoClient``.
Django templatetag wraps everything between ``{% linkif %}`` and ``{% endlinkif %}`` inside a ``link`` if ``link`` is not False.
Sample usage::
{% linkif "http://example.com" %}link{% endlinkif %}
{% linkif object.url %}link only if object has an url{% endlinkif %}
in models, import GeoCoordinateField:
class Place(models.Model):
geocoordn = GeoCoordinateField(verbose_name="geocordfield", null=True, blank = True)
>>>place = Place.objects.geocoordn #gives you a Geocoordinate object
>>>place.geocoordn.latitude, place.geocoordn.longitude #gives latitude and longitude of place
A management command to automatically generate a fully specified admin for the models in a specific app.
It automatically generates raw_id_fields, search_fields, list_filter and more. It bases this on date fields, fields named as "name" or slug.
Usage: ./manage admin_autogen <model>
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.