Default filter in Django admin

In order to achieve this and have a usable ‘All’ link in your sidebar (ie one that shows all rather than showing pending), you’d need to create a custom list filter, inheriting from django.contrib.admin.filters.SimpleListFilter and filtering on ‘pending’ by default. Something along these lines should work: from datetime import date from django.utils.translation import ugettext_lazy as … Read more

How to change ForeignKey display text in the Django Admin?

If you want it to take effect only in admin, and not globally, then you could create a custom ModelChoiceField subclass, use that in a custom ModelForm and then set the relevant admin class to use your customized form. Using an example which has a ForeignKey to the Person model used by @Enrique: class Invoice(models.Model): … Read more

Overriding the save method in Django ModelForm

In your save you have to have the argument commit. If anything overrides your form, or wants to modify what it’s saving, it will do save(commit=False), modify the output, and then save it itself. Also, your ModelForm should return the model it’s saving. Usually a ModelForm’s save will look something like: def save(self, commit=True): m … Read more

How can I restrict Django’s GenericForeignKey to a list of models?

For example, your apps are app and app2 and there are A, B models in app and there are C, D models in app2. you want to see only app.A and app.B and app2.C from django.db import models class TaggedItem(models.Model): tag = models.SlugField() limit = models.Q(app_label=”app”, model=”a”) | models.Q(app_label=”app”, model=”b”) | models.Q(app_label=”app2″, model=”c”) content_type = … Read more

Django admin: how to sort by one of the custom list_display fields that has no database field

I loved Greg’s solution to this problem, but I’d like to point that you can do the same thing directly in the admin: from django.db import models class CustomerAdmin(admin.ModelAdmin): list_display = (‘number_of_orders’,) def get_queryset(self, request): # def queryset(self, request): # For Django <1.6 qs = super(CustomerAdmin, self).get_queryset(request) # qs = super(CustomerAdmin, self).queryset(request) # For Django … Read more

Django Admin Show Image from Imagefield

Sure. In your model class add a method like: def image_tag(self): from django.utils.html import escape return u'<img src=”https://stackoverflow.com/questions/16307307/%s” />’ % escape(<URL to the image>) image_tag.short_description = ‘Image’ image_tag.allow_tags = True and in your admin.py add: fields = ( ‘image_tag’, ) readonly_fields = (‘image_tag’,) to your ModelAdmin. If you want to restrict the ability to edit … Read more

Custom Filter in Django Admin on Django 1.3 or below

Thanks to gpilotino for giving me the push into the right direction for implementing this. I noticed the question’s code is using a datetime to figure out when its live . So I used the DateFieldFilterSpec and subclassed it. from django.db import models from django.contrib.admin.filterspecs import FilterSpec, ChoicesFilterSpec,DateFieldFilterSpec from django.utils.encoding import smart_unicode from django.utils.translation import … Read more