sqlallall.py
A simple script to run 'manage.py sqlall' on every app in a project.
- sql
- manage.py
- sqlall
A simple script to run 'manage.py sqlall' on every app in a project.
Deprecated. I don't use this any more. Hi, I want decimal input which uses a comma as decimal seperator. It was quite complicated, but it works. It can be used as an example how to create an own subclass of an existing db.Field class and how to pass the dbfield to the widget, and use it in its render() method. I think my snippet is too complicated, but couldn't find a better solution. If you do, please tell me.
For those who find the Django comments syntax tedious, this snippet enables the use of (almost) Cheetah-style comments.
I wanted to make the objects of a particular model approvable and store the timestamp of when that happened. In other frameworks/languages, I used to combined those in one "approved_at" field, which would be NULL if an object was currently unapproved. I tried different approaches to implement this in django, and this is the best I came up with so far. Basically, the code in __setattr__ makes sure that the field, once set, will not be updated again. Overriding setattr__() could also be a solution to determining if a field value has changed in save(), a question that seems come up from time to time in #django.
l = (1, 23, 60, 75, 3600, 36000) for n in l: print friendly_seconds(n) 00:00:01 00:00:23 00:01:00 00:01:15 01:00:00 10:00:00
Django does not currently allow one to pull ID values from arbitrarily named sequences. For example, if you did not create your ID column using the serial data type in PostgreSQL, you likely will not be able to use your sequences. This is quite a problem for those integrating with legacy databases. While ultimately the best place to fix this is django proper, this decorator will help people get by for now. Note that in this case, all of my sequences are named "pk_TABLENAME". You'll likely have a different convention and should update the decorator appropriately. While I could have made the pattern a parameter, it didn't seem like that would gain much here.
For use in templates: {% if object|classname:"entry" %} ... class="{{ object|classname }}" ... I couldn't find simpler solution for checking weather specific object belongs to specific class name, or just to output object's class name.
This template filter will split a string of text on newlines and return a string of <li></li>s with a newline before every line. This is handy for taking a paragraph of text and making an <ol> or <ul> from its lines. Don't forget to register your filter with the template library first or the filter won't work.
Assume a model called 'Child' with a field called 'schoolstd' whic h is of integer type and not null. You need to change it to char type and not null and the same time preserve the data. The above snippet does this in postgresql.
Ever since django.contrib.markup appeared I've added a `markup_lang` field to my models where I want to support multiple input formats. This filter lets you pass the filter name as a string (from your model field, for example) and it will call the appropriate filter. I use None when the text is HTML, in which case it will return as-is. Example: class Article(models.Model): content = models.TextField(null=False, default="") markup_lang = models.CharField(maxlength=20, blank=True) a = Article(content="**Test!**", markup_lang='textile') b = Article(content="<h1>Hello!</h1>") And in a template: {% for article in article_list %} {{ article.content|render_markup:article.markup_lang }} {% endfor %}
Usage: `{{ price|dollarize }}`
Based upon (i.e., mostly lifted from) the built-in "linebreaks" and "linebreaksbr" filters, this one works as they do: `{{ object.text|paragraphs }}`
If you want to run multiple versions of a site from the same project IE a staging version and the live one, you need two settings and urlconf files. 1. make separate copies of settings_staging.py and urls_staging.py in the project dir. 2. Change SITE_ID and ROOT_URLCONF in settings_staging.py 3. Make extra include lines in the projects urls_staging.py like the example. 4. Add urls_staging.py to applications where you need extra urls. Make them just like you would normally do urls.py Thanks to ronny for suggesting the double entries in urlconf.
Handy for things such as: `{{ some_string|slice:","|join:";" }}`
This is a reasonably straight forward port of functionality provided by the `django.utils.dateformat` module into a method extending JavaScript's Date object. Its intended use is to allow client-side dynamic content to share the same date & time string formatting as Django template markup. By using this in conjunction with a context processor (to pass a format string to all templates) you can switch formats for both server-generated & client-generated dates across a complete site with a single setting. The function supports *almost* the entire format -- as listed by the Django documentation for the [now template tag](http://www.djangoproject.com/documentation/templates/#now) -- with the exception of "I" & "T". As a 'dumb' illustration, the following template tag usage: It is {% now "jS F Y H:i" %} ...could equate to the following: It is <script type="text/javascript">var now = new Date(); document.write(now.strfdate('jS F Y H:i'));</script> It's not extensively tested (I only wrote it over the weekend), but seems to be working okay. Feel free to leave any corrections or suggestions in the comments, and I'll try to keep this entry updated if I make any fixes or changes.
3110 snippets posted so far.