You can use this view to have a possibility to use ?paginate_by=x on your generic lists.
Example usage in urls.py:
`url('^list/', object_list_with_paginate_by,
{'queryset': Invoice.objects.all(), 'template_name': 'invoices/list.html', 'paginate_by': 10},
'invoices.list')`
10 is the default objects count per page.
For the first time parameter paginate_by is set in URL, we have to get it straight from there.
If the parameter is set, then also set the cookie for later requests without the parameter
If the parameter is not set, then we try the cookie or default value
Unlimited-length character fields in Postgres perform the same as limited-length fields, and the Postgres manual suggests not arbitrarily limiting these fields. Unfortunately, Django does not provide a way to access unlimited-length character fields except using TextField, which is rendered differently in forms and in the admin, and has different connotations.
LongCharField is a way to avoid putting arbitrary max_length values where they aren't required. It will only work with databases that allow VARCHAR with no numeric parameters, such as Postgres. MySQL won't work.
Feet and Inches Widget/Field allows users to enter a value in feet, inches and fractional inches and have that value stored as a decimal in your database of choice.
I have this code broken out into separate files, but you can always combine to suit your needs. I have designated file delineation with a single line comment and the name of the file as referenced in the import statements.
The INCH_DECIMALS and INCH_FRACTION constants may be modified to allow smaller increments. My needs for this implementation only required accuracy to 1/16.
Hope this helps someone learn how to use the MultiWidget and MultiValueFields!
Happy Coding.
I needed to overwrite files on update (not create new ones) but also a validation which would prevent to upload 2 files with the same name.
The CustomCheckFiled triggers a validation passing the filename and model instance. If the validation returns false, the validation error_message will be displayed in admin.
The OverwriteStorage is needed because the default storage alters the name if such name already exists. Enjoy.
I often insert `pdb.set_trace()` in my test cases to debug and examine behavior. When tests fail with assertions like `assertContains(response, 'Some text')`, it would be useful to see the response's contents in a browser window. This snippet does just that. Simply put this code in a python script on your `PYTHONPATH` and import/call the function when the debugger starts.
Only tested on Ubuntu and you might want to change `URL_OPENER` to whatever you want to open the URLs. Simple, but hopefully useful.
Django 1.3 has an assertNumQueries method which will allows you to simply specify the number of queries you expect. Sometimes, however, specifying the exact number of queries is overkill, and makes the test too brittle. This code provides a way to make more forgiving tests.
See http://lukeplant.me.uk/blog/posts/fuzzy-testing-with-assertnumqueries/
I basically mixed both BRCPFField and BRCNPJField to create a widget that validates either an CPF or an CNPJ. The doc strings are not localized. So you probably have to hardcode it yourself.
Code for template tag which generate [**LinkShare**](http://www.linkshare.com/) Pixel Tracking code for [**Satchmo**](http://www.satchmoproject.com/)-based stores.
Paste code into *linkshare_tracking.py* file in your project's template tags folder. Set *LINKSHARE_MERCHANT_ID* in your *settings.py*
**Usage:**
Put at top of *success.html* template: `{% load linkshare_tracking %}`
Somewhere in *success.html* template body: `{% linkshare_pixeltracker request order %}`
I found that South doesn't execute initial sql file after a migration, like what Django can do after syncdb. So here's the workaround by using post_migrate signal.
Usage:
Put your SQL files the same way as you would if you were to use Django to load your initial SQL file (follow Django doc). The only difference is that, the SQL filename needs to in this format: <app_label>.sql OR <app_label>.<backend>.sql
This is done this way since the migration is run per app.
If you need to customize many default templates from installed apps, this management command will help you to find those templates and to copy them to desired location.
Place this code at:
management/commands/templates.py
To see a list of installed templates, run:
python manage.py templates
To copy all templates to specified location:
python manage.py templates --copy-to ./templates
To copy templates from specified applications only:
python manage.py templates admin auth --copy-to ./templates
I don't know if you noticed but GenericForeignKey behaves badly in some situations. Particularly if you assign a not yet saved object (without pk) to this field then you save this object the fk_field does not get updated (even upon saving the model)- it's updated only upon assigning object to the field. So you have to save the related object prior to even **assigning** it to this field. It's get even more apparent when you have null=True on the fk/ct_fields, because then the null constrains won't stop you from saving an invalid object. By invalid I mean an object (based on a model with GFK field) which has ct_field set but fk_field=None.
Maybe this problem is irrelevant in most use case scenarios but this behaviour certainly isn't logical and can introduce silent bugs to your code.
This module contains classes that add new behavior to Django's ORM. Classes include:
**Session**
* Forces QuerySet objects to return identical instances when objects with the same primary key are queried.
* Similar to [SQLAlchemy Session](http://www.sqlalchemy.org/docs/orm/session.html)
**GraphSaver**
* Save entire object graphs at once.
* Automatically detects object dependencies and saves them in the correct order.
**Collection**
* Easier one-to-many relationships.
Instructions and more information on [limscoder.com](http://www.limscoder.com/2011/01/django-orm-tools.html).
This snipped provides a subclass of the syndication Feed class that supports HTTP authentication (basic auth). Feeds that should support authentication can inherit from this class instead from the Feed class. It is basically the implementation of [Snippet #243](http://djangosnippets.org/snippets/243/) ported to the new-style syndication framework feeds (for which decorators don't work).
Usage:
class ArticleFeed(HTTPAuthFeed):
def items(self, obj):
return Article.objects.all().order_by('-created')[:10]
def item_title(self, item):
return item.title
def item_description(self, item):
return item.description
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.