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
This template tag was inspired by http://djangosnippets.org/snippets/592/, but with improvements in the syntax it is used with to be more function-like, and avoiding the problem of conditional recursion as noted in http://djangosnippets.org/comments/cr/15/592/#c2472.
The syntax for using it can be seen in the docstring of the defrecurse() function. Additionally, a magic "level" variable is used to indicate the level of recursion, starting with 0 for the outermost level.
This should theoretically allow for nested recursion, but the inner {% recurse %} call cannot call the outer {% defrecurse %} block.
I thought it would be useful to have a `get_addr()` method available on request objects, similar to the `get_host()` provided by Django. This middleware will add a `get_addr()` method to requests which uses the `X-Forwarded-For` header (useful if you're behind a proxy) if it's present and you have the `USE_X_FORWARDED_FOR` header set to `True` (default is `False`) and otherwise will use the `REMOTE_ADDR` environment variable. Note that if you are *not* behind a proxy and have `USE_X_FORWARDED_FOR` set to `True`, then clients can spoof their IP by simply setting the `X-Forwarded-For header`.
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>
I wanted to sort a CharField which consists of digits in a different way. This field is a matricle number field (some kind of registration number for students. They have matricle numbers in the format YYxxxxxx - which means "YY" are the last two digits of the year they started studying.)
So I wanted to sort them in a way that they appear like this:
5000000, 5000001, ... , 9999998, 9999999, 0000000, 0000001, ... , 1200000, ... , 4999999
Took me some time to find out how to do this efficiently in PostgreSQL, and so I thought I'd share it here.
The important stuff is in the model "Candidate" to use a special "objects" object manager which uses a special QuerySet as well. Here lies the "magic": If there is a ordering required that contains "mnr", then a special on-the-fly calculated field will be added to the queryset called "mnr_specialsorted".
Now it is possible to do things like
`Candidate.objects.filter( firstname__contains="Pony" ).exclude( lastname__contains="Java" ).order_by("lastname", "-mnr")`
For other database engines you might want to change the MNR_SORTER variable to fit your needs.
This snippet allows you to use the CommaSeparatedIntegerField to store a set of integers that correspond to a set of choices. There are a couple other snippets that proclaim to do this, but they either don't work with the django 1.4, or are more complex.
Having spent ages trying out various admin inline ordering packages and examples I found on here and elsewhere I failed to find a single one that did what I was after in the way I wanted or that worked, so I wrote one!
The general idea for this version was to be done purely in javascript, no additional methods or parameters required on your models, it's designed to be stuck in a js file and included in your admin class Media js parameter:
class Media:
js = ['js/admin/widget_ordering.js', ]
Your model should have an integer column for sorting on, the name of this column should go in the 'sort_column' parameter at line 3 and your model should also obviously specify this in it's Meta 'ordering' class:
class Meta:
ordering = ('rank',)
That's it! This is a pretty basic implementation that adds simple up and down buttons next to the sort order field, if you want to adapt this to use drag and drop or something, please feel free!
I had to build unique strings for a payment system and i wanted to make them kindof friendly so i generated them with usernames and datetimes(safe enough uniqueness in combo), some usernames are long and they break the limit of this payment system so i thought i should cut the center of the string so it stills has a part of the username and a part of the datetime, the most changing part of the datetime is of course the last part, as microseconds vary rapidly.
So i wrote this little function to cut the center of a string i thought it cute so i leave it here.
Pay attention to the comment so you can see what is going on.
1. Create an app and place this in `admin.py`.
2. Add `url(r'^login/$', 'social_auth.views.auth', {'backend': 'google'}, name='login')` to your `urls.py`.
3. Add the app to your `INSTALLED_APPS` after `django.contrib.admin`.
4. Set `USE_SOCIAL_AUTH_AS_ADMIN_LOGIN = True` in your `settings.py`.
5. ...
6. Profit.
The code is Django 1.4 version of code based on the [Django 1.3 snippet](http://djangosnippets.org/snippets/2593/) that speeds up Django's admin pages with postgres back-end for big tables (> few hundred thousands of records).
I had a hell of a time getting most QR code stuffs out there working with Django. Many projects, but problems here and problems there. The only additional code you'll need for this is to "pip install qrcode". After that, you're free to <img src="{% qrcode_datauri "http://localhost:8000/" %}" />.
This is a small function for those time when you want a list of all your urls, expanding included urls, so in the end is like all your urls are in one module. This function recursively does it, so it doesnt matter how nested the includes are in the end you get one flat list.