i use this to get the pk of a record before creation,
in my scenario to name an uploaded image:
def UPLOADTO( i,n ):
if not i.id:
id = get_nextautoincrement( i.__class__ )
else:
id = i.id
return str(id)+'.jpg'
Permit to redirect desired domain name to the 'domain' of Site app.
Useful if you have different domains name for the same website.
#1. Add to your settings DOMAINS_ALIAS like this:
DOMAINS_ALIAS = (
'my-second-domain.com',
'www.my-second-domain.com',
'third-domain.com',
'www.third-domain.com',
)
notice: all these domains are redirected to the **domain** db entry of Site ID.
#2. add all these domains to ServerAlias directive in your vhost apache configuration.
#3. enable the middleware by adding to your MIDDLEWARE_CLASSES:
MIDDLEWARE_CLASSES = (
...
'utils.middleware.domainsalias.DomainsAliasMiddleware',
...
)
Trying `./manage.py dumpdata` on a huge database and getting `MemoryError`s? Here's part of your solution.
[Snippet 1400](http://www.djangosnippets.org/snippets/1400/) provides a queryset_foreach utility that we've found very useful. This snippet uses it on a serializer that can output to a stream, such as the XML serializer.
Management command coming momentarily...
The following code takes two related models and creates one user interface to for create and update operations that handles them both. It enables the creation or update of instances from both models in one shot, without having to create very complex forms. As I could not find examples for creation of related objects together, I thought this might be useful for someone.
Does exactly what it says on the tin!
This template tag, when implemented, converts a duration (in seconds) to a more meaningful format. It has a short and long setting, which is easy to manipulate for your needs. Apologies if something already exists like this, however I felt that writing this would be quicker than trying to find it online.
As an example, given the duration 84658:
Short (default): 23 hrs 30 mins 58 secs
Long: 23 hours, 30 minutes and 58 seconds
All the best,
[Dan Ward](http://d-w.me).
This is based on [snippet 501](http://www.djangosnippets.org/snippets/501/), with some corrections:
1. if user doesn't exist and AD.authenticate passes, then create new user - don't store password - prevent default django auth backend authentication
2. if user exists and AD.authenticate passes - django User object is updated
3. various error handling
4. fixes (some mentioned in original snippet)
5. some settings removed from settings to backend module
6. other changes (ADUser class, re-indent, logging etc.)
7. ignores problem with search_ext_s (DSID-0C090627)
8. caching connection - when invalid then re-connect and try again
Note that there is also ldaps:// (SSL version) django auth backend on [snippet 901](http://www.djangosnippets.org/snippets/901/).
Possible improvements:
1. define new settings param - use secured - then LDAPS (snippet 901)
2. define new settings extra ldap options - e.g. protocol version
3. fetch more data from AD - fill in user data - maybe to make this configurable to be able to update user.get_profile() data too (some settings that has mapping AD-data -> User/UserProfile data)
This code demonstrates two simple techniques:
1. so-called "dynamic" forms, in which the form is created at run time by the model
2. using a widget (forms.widgets.HiddenInput) for a field of the form. I feel like this could be highlighted more in the documentation. You need to do something similar to get a textarea (forms.CharField(widget=forms.widgets.Textarea()) and it took me too long to figure this out.
There are, no doubt, good reasons not to do what I'm doing here the way I'm doing it. Peanut gallery?
This little bit of code will let you reference parts of the admin but lets you control where it returns to.
For instance, I have a list of objects in the admin and i want to have a delete link for each object. I don't want them to return to the changelist after a delete however, i want them to return to my list.
cheers
I wanted to use Nose with Django so I came up with this.
`TEST_RUNNER = 'noserun.run_tests'` in settings.py
It does not do setup/teardown implicitly between test
methods, you need to call *nosetest.test.flush()* and
*nosetest.test.loaddata()* manually if you want that.
Enables the method names *setup* and *teardown*
The environment variable *NOSE_COVER* runs coverage
tests and *NO_DROPDB* preserves the test db.
This is a [Paginator Tag](http://www.djangosnippets.org/snippets/73/) for 1.x. Since the context is less overfull, the template, paginator.html, needs more logic.
Put the tag in your templatetags and the template at the root of a template-directory.
The tag will work out of the box in a generic view, other views must provide `is_paginated` set to True, `page_obj`, and `paginator`. You can get the `object_list` from the `page_obj`: `page_obj.object_list`. See [the pagination documentation](http://docs.djangoproject.com/en/1.0/topics/pagination/).
This code creates a widget that we used to generate a list of radiobuttons as follows:
* Radio button 1
--Widget__1
* Radio button 2
--Widget__2
* Radio button 3
--Widget__3
How to use it is:
`ImagenForm class (forms.ModelForm):
widget1 = forms.BooleanField ()
widget2 = forms.TextInput
widget3 = forms.ModelChoiceField (queryset = Modelo.objects.all ())
widget4 = forms.FileInput
optConListas = ChoiceWithOtherField (choices = [(2, 'widget1' widget1), (3, 'widget2' widget3.widget), (4, "widget2" widget2), (5, 'widget4' widget4)])`
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.