This custom model field is a variant of NullBooleanField, that stores only True and None (NULL) values. False is stored as NULL.
It's usefull for special purposes like unique/unique_together.
One small problem is here, that False is not lookuped as None.
This snippets is a response to [1830](http://www.djangosnippets.org/snippets/1830/)
Based on Snippet [766](http://www.djangosnippets.org/snippets/766/) & [799](http://www.djangosnippets.org/snippets/799/), edited for django 1.0 and higher.
By default, a "Show all" link will appear in the changelist pager only if fewer than 200 records are in the result. Since it is rare that there will be more than one page of records yet fewer than 200, the "Show all" link almost never shows up. Pasting this code somewhere in your app will allow you to increase the 200-record maximum.
"Show all" is very handy when used in combination with batch actions and filters, and this change will enable it for most situations. Note that this allows a changelist with up to 10,000 results, which results in a lot of HTML that can tax slower browsers and older machines. For me, it has been worth the tradeoff, since my users have fast enough computers and need to be able to make batch changes efficiently.
This is a replacement for Django's built-in ImageField. It uses the Google AppEngine image APIs in order to validate.
Notes:
1. Validation of the field counts against your App Engine transformations quota.
2. This code assumes you're only using the in-memory file upload handler. None of the other stock handlers work well on App Engine; you should probably disable them.
This snipped removes a specific fields from the fieldsets. This is very useful to leave a field 'out' in the admin, likewise:
def get_fieldsets(self, request, obj=None):
fieldsets = super(BlaModelAdmin, self).get_fieldsets(request, obj)
if not request.user.has_perm('change_blah'):
remove_from_fieldsets(fieldsets, ('blah',))
This is a hack to get HTML 5 to work for Firefox 2. It Sends the xhtml content-type to all Gecko based browsers where version is less than 1.9, or "rv:1.9pre" or "rv:1.9a" or "rv:1.9bx" where x is less than 5. I created this hack based on the information I found on this site, http://html5doctor.com/how-to-get-html5-working-in-ie-and-firefox-2/.
Just put this code in one of your middleware files (i.e. mysite/middleware.py) and then add it in your MIDDLEWARE_CLASSES in your settings.py.
Example:
MIDDLEWARE_CLASSES = (
...
'mysite.middleware.HTML5Firefox2Hack',
...
)
Calls a view by request.method value.
To use this dispatcher write your urls.py like this:
urlpatterns = pattern('',
url(r'^foo/$', dispatch(head=callable1,
get=callable2,
delete=callable3)),
)
If `request.method` is equal to head, `callable1` will be called as your usual view function;
if it is `get`, `callable2` will be called; et cetera.
If the method specified in request.method is not one handled by `dispatch(..)`,
`HttpResponseNotAllowed` is returned.
Have you always been annoyed by how you set up this elaborate big database schema and weren't able to have **ON DELETE CASCADE ON UPDATE CASCADE** in dbshell?
This solves the problem; create the two files and and empty *__init__.py* and put them somewhere in your path.
Then say DATABASE_ENGINE='postgresql_psycopg2_cascade' in settings.
Really I'd like this to be in the ForeignKey object, were it upstream Django or an own version of it, but it doesn't seem possible.
Ideas on how to make this configurable are more than welcome!
Props go out to Ari Flinkman for the inspiration to do this!
This is a really simple snippet , but is not documented well ...
Whit this , you can limit the output of one field in 25 characters before you test that is equal or over 30 characters!
Note that the snippet cut the text and add three points at the end (...)
Enjoy!
I don't understand why the cache is accumulated between the tests. I thought one of the axioms of unit testing is that the tests should not affect each other.
Couldn't find anything that explains why it's done this way but it seems a bit strange. Anybody know if there's a reason or is this a reason for me to upload a patch to Django code?
A bit cleaner syntax for method attribute assignment; used in models for 'short_description'.
>from mysite.polls.models import Poll
>poll = Poll.objects.get(pk=1)
>poll.was_published_today.short_description
>>>'Published today?'
UPDATE: 'ORDER BY' in de regex is now optional
If enabled while coding just keep track of your console output for:
<<< WARNING >>> Query execution without ownership clause, called from "process_response"
Happy coding
Regards,
Gerard.
Return a datetime corresponding to date_string, parsed according to format.
I had the need for such a thing while working with an API that returned JSON that I fed, via simplejson, directly to a template, and didn't want to change the data structure just for this one piece.
You're looking at the most-bookmarked snippets on the site; if you'd like to help useful snippets show up here, sign up for an account and you'll get your own bookmarks list.