How to get the ID of the last created object added by newforms
How to get the ID of the last created object added by newforms
- newforms
How to get the ID of the last created object added by newforms
A patch (against django svn trunk [4649](http://code.djangoproject.com/browser/django/trunk/?rev=4649)) that allows users to log in with Basic HTTP Authentication i.s.o. login forms using some simple middleware (entire patch is ~50 lines). I was unaware of http://code.djangoproject.com/wiki/GenericAuthorization so I'm not sure about its usefulness in the long run. You can enable it by including 'django.contrib.auth.middleware.BasicAuthenticationMiddleware' in your MIDDLEWARE_CLASSES and then adding the following lines in your settings.py: BASIC_WWW_AUTHENTICATION = True WWW_AUTHENTICATION_REALM = "djangolures.com" Updated: See also http://code.djangoproject.com/ticket/3609 (patch now availble here as well).
`foo = dynamic_import ( 'rawr.i.am.a.lion' )` Will import `lion` from `rawr.i.am.a` and return it. (This isn't really Django specific) Props to Crast for the original.
Put inside `mysite/templatetags/getattr.py` Add `mysite` to your `INSTALLED_APPS` In your template: {% load getattr %} {{ myobject|getattr:"theattr,default value" }} Thanks to pterk for optimizations! \\o/
How to use it {% pycall os.path.abspath(".") %} {% pycall os.path.abspath(".") as path %} This is the {{ path }}. Syntax {% pycall module.function(...) [as variable_name] %} If there is no as variable_name, the result will be output directly.
How to use it {% pyif i == 1 %} <p>i=1</p> {% elif i == 3 %} <p>i=3</p> {% else %} <p>other</p> {% endif %} Warning: For now, django don't support elif, so you can use it. And I'v submit a patch about to fix it ( #3090 ). If the patch is accepted, you can use elif tag.
#### Allows to fetch a row or array of rows of data, linked to parent object, in a single query. Data is fetched as JSON and is not serialized into Django objects. ##### Example: from django.db import Models class Book(models.Model): authors = models.ManyToMany('Author', through='BookToAuthor', blank=True) title = models.CharField(max_length=512, default='') class Author(models.Model): name = models.CharField(max_length=512, default='') class BookToAuthor(models.Model): author = models.ForeignKey(Author, on_delete=models.CASCADE) book = models.ForeignKey(Book, on_delete=models.CASCADE) ##### Download author with all his/her books in a single query from django.db.models import OuterRef books_by_author_subquery = Book.objects.filter( id__in=BookToAuthor.objects.filter(author_id=OuterRef(OuterRef('id'))) ).values('title') author = Author.objects\ .annotate(books=SubqueryJsonAgg(books_by_author_subquery))\ .get(id=1)
Django Management Command to print a "Magic Link" for one-click log-in. This is nice for people who project switch or don't want to remember passwords.
FileField delete file on delete or update
Place the code any admin.py file in your registered apps
Fields that support HTML optgroups. Adapted from [this snippet](https://djangosnippets.org/snippets/1968/) and updated to work with latest version of Django (1.9) and additional ModelMultipleChoiceField support added. Example Usage: tag = GroupedModelChoiceField(queryset=Tag.objects.all(), group_by_field='parent') positions = GroupedModelMultiChoiceField(queryset=Position.objects.all(), group_by_field='agency')
If you require lots of forms in your project and do not want to be creating an extended template for each one I propose this solution. Classes in the html correspond to bootstrap, you can work without them if you do not use bootstrap.
I've reimplemented the code I found somewhere on the web within my models file. The earlier version was incapable of converting all formats to JPG while this code converts all formats and compresses all of them successfully. You need to have PILLOW installed for this to work.
## required * `{% load trans%}`before using this snippets * Add this [template filter](https://djangosnippets.org/snippets/2253/) to your custom templatetags and load it before using this snippets * Bootstrap framework
Based on [#2369](https://djangosnippets.org/snippets/2369/) Save the snippet as actions.py within your django app, and then add an action on any model you want in it's ModelAdmin definition. Example usage: from actions import export_as_csv_action class YourModelAdmin(admin.ModelAdmin): list_display = (...) list_filter = [...] actions = [export_as_csv_action("CSV Export", fields=[...])]
3110 snippets posted so far.