Login

Snippets by DrMeers

Snippet List

Limit queryset to objects related to parent in ManyToMany fields within admin inlines

`formfield_for_manytomany` allows you to limit the choices/queryset for a ManyToManyField, but without direct access to the parent object. This snippet stores a reference to the parent object in `get_formset` and allows limiting of `ManyToManyField`s to objects related to the same parent object. See `ExampleInline` for example usage. If for some reason you have a `ManyToManyField` in a `TabularInline`, just change the `template` in the subclass.

  • limit_choices_to ManyToMany ManyToManyField admin inline formfield_for_manytomany
Read More

Fake SSL Middleware for Tests and Local Development

Add `FakeSSLMiddleware` to the top of your `MIDDLEWARE_CLASSES` stack when running tests or developing locally to allow https:// links to operate correctly. Can be used in conjunction with other SSL middleware to allow critical tests to be performed.

  • middleware
  • ssl
  • testing
  • test
  • https
  • local
  • fake
Read More

DKIM Email Backend

Overrides the `_send` method of the default SMTP `EmailBackend` class to include a [DKIM](http://www.dkim.org/) signature based on settings: 1. `DKIM_SELECTOR` -- e.g. `'selector'` if using `selector._domainkey.example.com` 2. `DKIM_DOMAIN` -- e.g. `'example.com'` 3. `DKIM_PRIVATE_KEY` -- full private key string, including `"""-----BEGIN RSA PRIVATE KEY-----"""`, etc You'll need [pydkim](http://hewgill.com/pydkim/). Just include this code in `project_name.email_backend.py`, for example, and select it in your settings file; e.g. `EMAIL_BACKEND = 'project_name.email_backend.DKIMBackend'`

  • email
  • dkim
Read More

DRY Fieldsets

I've devised a DRY method of declaring django fieldsets: ** Example usage: ** 1. Include the attached code in `fieldsets.py` 2. `models.py`: from django.db import models from fieldsets import Fieldset, ModelWithFieldsets class Person(ModelWithFieldsets): #instead of models.Model # this field will be placed in nameless fieldset example_field = models.IntegerField() # this fieldset will be grouped into one row Fieldset(grouped=True) first_name = models.CharField(max_length=64) surname = models.CharField(max_length=64) Fieldset("Contact Details", classes=('collapse',)) mobile_phone = models.CharField(max_length=10) email_address = models.EmailField() Fieldset("Address") street_address = models.CharField(max_length=255) # the next two fields will be grouped into one row of this fieldset Fieldset.new_group(2) suburb = models.CharField(max_length=64) state = models.CharField(max_length=64) 3. `admin.py`: from django.contrib import admin from models import Person from fieldsets import Fieldset class PersonAdmin(admin.ModelAdmin): fieldsets = Fieldset.get_fieldsets(Person) admin.site.register(Person, PersonAdmin) This example produces the equivalent of manually typing: fieldsets = ( (None, {'fields': ('example_field')}), (None, {'fields': (('first_name', 'surname'),)}), ('Contact Details', { 'fields': ('mobile_phone', 'email_address'), 'classes': ('collapse',)}), ('Address', {'fields': ('street_address', ('suburb', 'state'))}) ) But now if you want to rearrange your fields, rename, delete, insert, etc, you won't need to remember to update the fieldsets in the ModelAdmin. This implementation is a bit of a hack, but I believe a cleaner equivalent should be implemented in django itself.

  • admin
  • dry
  • fieldsets
Read More

DrMeers has posted 5 snippets.