Every now and then you need to have items in your database which have a specific order. As SQL does not save rows in any order, you need to take care about this for yourself. No - actually, you don't need to anymore. You can just use this file - it is designed as kind-of plug-in for the Django ORM.
Usage is (due to use of meta-classes) quite simple. It is recommended to save this snippet into a separate file called `positional.py`. To use it, you only have to import `PositionalSortMixIn` from the `positional` module and inherit from it in your own, custom model (but *before* you inherit from `models.Model`, the order counts).
Usage example: Add this to your `models.py`
from positional import PositionalSortMixIn
class MyModel(PositionalSortMixIn, models.Model):
name = models.CharField(maxlength=200, unique=True)
Now you need to create the database tables: `PositionalSortMixIn` will automatically add a `postition` field to your model. In your views you can use it simply with `MyModel.objects.all().order_by('position')` and you get the objects sorted by their position. Of course you can move the objects down and up, by using `move_up()`, `move_down()` etc.
In case you feel you have seen this code somewhere - right, this snippet is a modified version of [snippet #245](/snippets/245/) which I made earlier. It is basically the same code but uses another approach to display the data in an ordered way. Instead of overriding the `Manager` it adds the `position` field to `Meta.ordering`. Of course, all of this is done automatically, you only need to use `YourItem.objects.all()` to get the items in an ordered way.
Update: Now you can call your custom managers `object` as long as the default manager (the one that is defined first) still returns all objects. This Mix-in absolutely needs to be able to access all elements saved.
In case you find any errors just write a comment, updated versions are published here from time to time as new bugs are found and fixed.
- db
- orm
- database
- plugin
- mixin
I'm a big fan of Markdown, and often set up models to automatically apply it to certain fields before saving. But that's not really flexible, because if I then distribute the code someone else might want to use reStructuredText or Textile or whatever, and then they have to hack my code.
So here's a function which looks for a setting called `MARKUP_FILTER` and, based on what it finds there (see the docstring for what it looks at), chooses a text-to-HTML conversion function and applies it to a piece of text. Since Textile, Markdown and reStructuredText all support various useful options, it has the ability to pick up arbitrary keyword arguments and pass them through.
- markup
- markdown
- textile
- restructuredtext
Adds filtering by ranges of values in the admin filter sidebar. This allows rows in numerical fields to be grouped together (in this case, group by price):
By store price
All
< 100
100 - 200
200 - 500
500 - 2000
>= 200
**To use:**
1. save the code as rangevaluesfilterspec.py in your app's directory
2. add `import rangevaluesfilterspec` to your models.py
3. add `myfield.list_filter_range = [value1, value2, ...]` to your filter field
**Example:**
from django.db import models
import rangevaluesfilterspec
class Product(models.Model):
store_price = models.DecimalField(max_digits=10, decimal_places=2)
store_price.list_filter_range = [100, 200, 500, 2000]
class Admin:
list_filter = ['store_price']
Note that two extra groups are added: less-than the lowest value, and greater-than-or-equal-to the highest value.
This will save your del.icio.us bookmarks in your own database with a Bookmarks model. It depends on several things:
1. `DELICIOUS_USER` and `DELICIOUS_PASS` defined in settings.py
2. [pydelicious](http://code.google.com/p/pydelicious/) installed
3. Any view that uses del.icio.us should call the delicious() function, to update the database.
4. The [cache_function](http://www.djangosnippets.org/snippets/109/) decorator must be available. (I have it in a decorators.py file in my app; if you move it, you need to update the import)
5. The [django-tagging](http://code.google.com/p/django-tagging/) app, although it could be modified to not need this rather easily.
Other than all those dependencies, it's actually rather simple. It will call del.icio.us for the 5 most recent posts, and create them if they're not already in the database. Otherwise, it'll check for updates, and change as appropriate. It won't return anything, as it updates the models themselves.
- cache
- delicious
- pydelicious
- api
- bookmarks