Allows for in internal link markup to be saved inside markdown-formatted Textfield entries.
Using the filter, the link is only looked up at display time, so if your view's URL has changed, that should automatically update with the reverse() lookup.
You could tweak the regex pattern to match whatever link markup you prefer. I also use Markdown to process my description fields, so I make the link return a markdown-formatted link instead of HTML, but you could tweak that too. If you use Markdown, you'd want to put this filter first.
So to display a description TextField with internal links, in the template would be something like this:
`{{ entity.description|internal_links|markdown }}`
(See the [Django docs on writing your own custom filters](https://docs.djangoproject.com/en/1.6/howto/custom-template-tags/#writing-custom-template-filters) for more details on writing and registering filters.)
Written for [my own website](http://opticalpodcast.com), and a basic version was shared as the [answer to this Stack Overflow question](http://stackoverflow.com/a/26812762/429070).
I have a master/slave SQL setup and sometimes I need to access the data immediately after write, but the data has not yet propagated to the slave yet.
This forces me to require `.using('default')` every time that relationship is accessed. Ex:
self.books.using('default').all().delete()
Setting `objects = ForceDefaultDBManager()` on the related object removes this requirement throughout your code. This now does the same as the last example:
self.books.all().delete()
Extended Template Tag initially created by Dan Ward (http://d-w.me):
https://djangosnippets.org/snippets/1398/
The default setting has been renamed to normal, output stays the same.
If you have used the old tag with ':short' you have to remove or rename it to ':normal' for the same ouput!
As an example, given the duration 84658:
Short: 23:30:58
Normal (default): 23 hrs 30 mins 58 secs
Long: 23 hours, 30 minutes and 58 seconds
Best regards,
Gregor Volkmann
MultiForm and MultiModelForm
Based on a PrefixDict class I wrote and thus very lean. Lacks a little documentation, though
class MyMultiForm(ModelMultiForm):
class Meta:
localized_fields = '__all__'
form_classes = OrderedDict((
('form1', Form1),
('form2', Form2),
))
Subfields are named `form-name` `prefix_sep` `subfield-name`. `prefix_sep` defaults to `-`. For access in templates, use `form.varfields`, which uses `var_prefix_sep` (default: `_`), instead.
multiform.varfields()['form1_field1']
Custom model field to support two ways encryption.
The key is provided in the app settings.py file. It should be at least 32 chars.
** usage **
Assuming that you placed the new code into fields.py
from app.fields import EncyptedField
class Account(models.Model):
password = EncryptedField()
A very simple way of automatically loading data on model creation.
As I am using South I wanted an automatic way of loading initial data only when a new model is create during a migration.
Django provides almost everything out of the box by providing the *post_syncdb* signal (triggered also by South migrate command) and the *loaddata* command.
The code will simply look for a an existing fixture named `<model>_initial.*` and invoke the *loaddata* command to try to load it
Note: beware *post_syncdb* signal is deprecated since version 1.7: it has been replaced by *post_migrate*.
When you use Virtualfish, the virtualenv wrapper for the fish shell, you can add hooks for whenever the virtualenv is activated. This little snippet sets some important environment variables and adds the manage.py command with autocompletion so that you won't ever need to write "python ./manage.py help" ever again, but only "manage <TAB>".
For this to work you need to source or paste this code in your ~/.config/fish/config.fish
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.