Skip to content

Instantly share code, notes, and snippets.

@douglasmiranda
Created April 24, 2012 19:23
Show Gist options
  • Save douglasmiranda/2482894 to your computer and use it in GitHub Desktop.
Save douglasmiranda/2482894 to your computer and use it in GitHub Desktop.
Customizing the Django Admin to work with multiple databases.
"""
Snippet from Django Docs
"""
class MultiDBModelAdmin(admin.ModelAdmin):
# A handy constant for the name of the alternate database.
using = 'other'
def save_model(self, request, obj, form, change):
# Tell Django to save objects to the 'other' database.
obj.save(using=self.using)
def delete_model(self, request, obj):
# Tell Django to delete objects from the 'other' database
obj.delete(using=self.using)
def queryset(self, request):
# Tell Django to look for objects on the 'other' database.
return super(MultiDBModelAdmin, self).queryset(request).using(self.using)
def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
# Tell Django to populate ForeignKey widgets using a query
# on the 'other' database.
return super(MultiDBModelAdmin, self).formfield_for_foreignkey(db_field, request=request, using=self.using, **kwargs)
def formfield_for_manytomany(self, db_field, request=None, **kwargs):
# Tell Django to populate ManyToMany widgets using a query
# on the 'other' database.
return super(MultiDBModelAdmin, self).formfield_for_manytomany(db_field, request=request, using=self.using, **kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment