How to use custom AdminSite class?

The Problem Using a custom class derived from django.contrib.admin.AdminSite for the admin site of a project, without having to write custom registration code to register models with the new class. When I use 3rd party apps with their own models, I’d rather not have to edit custom registration code only because models were added or … Read more

UnicodeEncodeError: ‘ascii’ codec can’t encode character

For anyone encountering this problem when running Django with Supervisor, the solution is to add e.g. the following to the supervisord section of Supervisor’s configuration: environment=LANG=”en_US.utf8″, LC_ALL=”en_US.UTF-8″, LC_LANG=”en_US.UTF-8″ This solved the problem for me in Supervisor 3.0a8 running on Debian Squeeze. Also make sure Supervisor re-reads the configuration by running: supervisorctl reread supervisorctl restart myservice … Read more

Override a form in Django admin

You can override forms for django’s built-in admin by setting form attribute of ModelAdmin to your own form class. See: https://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.form https://docs.djangoproject.com/en/dev/ref/contrib/admin/#adding-custom-validation-to-the-admin It’s also possible to override form template – have a look at https://docs.djangoproject.com/en/dev/ref/contrib/admin/#custom-template-options If you’re looking specifically for autocomplete I can recommend https://github.com/crucialfelix/django-ajax-selects

Django admin – inline inlines (or, three model editing at once)

You need to create a custom form and template for the LinkSectionInline. Something like this should work for the form: LinkFormset = forms.modelformset_factory(Link) class LinkSectionForm(forms.ModelForm): def __init__(self, **kwargs): super(LinkSectionForm, self).__init__(**kwargs) self.link_formset = LinkFormset(instance=self.instance, data=self.data or None, prefix=self.prefix) def is_valid(self): return (super(LinkSectionForm, self).is_valid() and self.link_formset.is_valid()) def save(self, commit=True): # Supporting commit=False is another can of worms. … Read more

Creating Custom Filters for list_filter in Django Admin

You can indeed add custom filters to admin filters by extending SimpleListFilter. For instance, if you want to add a continent filter for ‘Africa’ to the country admin filter used above, you can do the following: In admin.py: from django.contrib.admin import SimpleListFilter class CountryFilter(SimpleListFilter): title=”country” # or use _(‘country’) for translated title parameter_name=”country” def lookups(self, … Read more

How to drop all tables from the database with manage.py CLI in Django?

As far as I know there is no management command to drop all tables. If you don’t mind hacking Python you can write your own custom command to do that. You may find the sqlclear option interesting. Documentation says that ./manage.py sqlclear Prints the DROP TABLE SQL statements for the given app name(s). Update: Shamelessly … Read more

How to limit queryset/the records to view in Django admin site?

In your admin definition, you can define a queryset() method that returns the queryset for that model’s admin. eg: class MyModelAdmin(admin.ModelAdmin): def queryset(self, request): qs = super(MyModelAdmin, self).queryset(request) return qs.filter(user=request.user) Then only objects with user=request.user will be visible in the admin.

Django admin file upload with current model id

I ran into the same problem. Okm’s answer sent me on the right path but it seems to me it is possible to get the same functionality by just overriding the save() method of your Model. def save(self, *args, **kwargs): if self.pk is None: saved_image = self.image self.image = None super(Material, self).save(*args, **kwargs) self.image = … Read more