This management command discovers media files from all `INSTALLED_APPS` (or the apps you specify) and copies or links them to `MEDIA_ROOT`. Put this code in a file like so:
yourapp/management/commands/collectmedia.py
...and don't forget the necessary `__init__.py` files.
This command includes an interactive mode (`-i` or `--interactive`) and a dry run mode (`-n` or `--dry-run`) for previewing what will happen. See `manage.py help collectmedia` for more options.
This subclass of django.models.Field stores a python datetime.timedelta object
as an integer column in the database.
It includes a TimedeltaFormField for editing it through djangos admin interface.
Feedback welcome.
2011-04-20: TimedeltaField can now be (de)serialized. Tested with JSON.
2009-11-23: `_has_changed()` added. Before form.changed_data contained timedelta FormFields,
even if nothing changed.
See also [this thread](http://groups.google.com/group/django-developers/browse_thread/thread/ead13c9424b279a2). I'm an idiot; this snippet has now been updated to show how you do this without re-inventing the wheel.
Usage:
class MyModel(ModelWithHistory):
class History:
model = True # save model changes into admin's LogEntry table
fields = ('f1', 'f2') # save these fields history to AttributeLogEntry table
f1 = CharField(max_length=100)
f2 = IntegerField()
for threadlocals, see http://code.djangoproject.com/wiki/CookBookThreadlocalsAndUser
Aware! Not thoroughly tested yet. May cause problems with loading fixtures.
Sometimes you need to test some model features without a complete django app installation. Just play only with the model object. With these small script you have a complete in memory django installation.
Some Links:
http://www.djangosnippets.org/snippets/1044/ (en)
http://www.jensdiemer.de/permalink/150/mein-blog/99/django-db-model-test/ (de)
http://www.python-forum.de/viewtopic.php?f=3&t=15649 (de)
See also:
https://github.com/readevalprint/mini-django/
Create a copy of a model instance.
Works in model inheritance case where ``instance.pk = None`` is
not good enough, since the subclass instance refers to the
parent_link's primary key during save.
M2M relationships are currently not handled, i.e. they are not
copied.
See also Django #4027.
This is a simple fixture that is useful for many tests.
It contains the following users:
* admin
* staff
* user0
* user1
* user2
* user3
* inactive0
* inactive1
The password of every user is the same as his username, e.g.: admin/admin
Instructions: Set your environment variables, install graphviz, and run.
Finds all models in your installed apps and makes a handsome graph of your their dependencies based on foreignkey relationships. Django models have green outlines, yours have purple. The edge styling could be changed based on edge type.
Here's an example of writing generic views in an object-oriented style, which allows for very fine-grained customization via subclassing. The snippet includes generic create and update views which are backwards compatible with Django's versions.
To use one of these generic views, it should be wrapped in a function that creates a new instance of the view object and calls it:
def create_object(request, *args, **kwargs):
return CreateObjectView()(request, *args, **kwargs)
If an instance of one of these views is placed directly in the URLconf without such a wrapper, it will not be thread-safe.
PayPal's [https://www.paypal.com/ipn](IPN mechanism) is ridiculously easy to consume. You can tell PayPal to POST every single transaction on your account to a URL somewhere, then set up a handler at that URL which processes those transactions in some way. Security is ensured by POSTing the incoming data back to PayPal for confirmation that the transaction is legitimate.
These classes are probably over-engineered, but they were a fun experiment in creating class-based generic views.
A filter to resize a ImageField on demand, a use case could be:
<img src="{{ object.image.url }}" alt="original image">
<img src="{{ object.image|thumbnail }}" alt="image resized to default 104x104 format">
<img src="{{ object.image|thumbnail:200x300 }}" alt="image resized to 200x300">
Original http://www.djangosnippets.org/snippets/192/
Special thanks to all the authors of djangosnippets. It's my first contribution.
Maybe the method **online_users** could be better, but for now it's working for me.
Best Regards from Bolivia.
Best practice based on [YSlow recommendations](http://developer.yahoo.com/yslow/), add the following to your Apache config for your media directory.
<Directory /home/.../site_media/>
...
FileETag None
ExpiresActive on
ExpiresDefault "access plus 10 years"
AddOutputFilterByType DEFLATE text/css application/x-javascript
</Directory`
Make sure to enable mod_deflate and mod_expires.
Alternative version of newform code for handling credit cards. Unlike the other two credit-card snippets (http://www.djangosnippets.org/snippets/764/ and http://www.djangosnippets.org/snippets/830/), this uses two drop-down boxes instead of text fields for the expiration date, which is a bit friendlier. It doesn't do as much checking as snippet #764 since we rely on the payment gateway for doing that. We only accept Mastercard, Visa, and American Express, so the validation code checks for that.
You're looking at the top-rated snippets currently on the site; if you'd like to contribute, sign up for an account and you'll be able to rate any snippet you see.