Django: accessing the model instance from within ModelAdmin?

I think you might need to approach this in a slightly different way – by modifying the ModelForm, rather than the admin class. Something like this: class OrderForm(forms.ModelForm): def __init__(self, *args, **kwargs): super(OrderForm, self).__init__(*args, **kwargs) self.fields[‘parent_order’].queryset = Order.objects.filter( child_orders__ordernumber__exact=self.instance.pk) class OrderAdmin(admin.ModelAdmin): form = OrderForm

Django admin, hide a model

Based on x0nix’s answer I did some experiments. It seems like returning an empty dict from get_model_perms excludes the model from index.html, whilst still allowing you to edit instances directly. class MyModelAdmin(admin.ModelAdmin): def get_model_perms(self, request): “”” Return empty perms dict thus hiding the model from admin index. “”” return {} admin.site.register(MyModel, MyModelAdmin)

How to display uploaded images in “Change List” page in Django Admin?

You can create a model instance method with another name, allow HTML tags for its output and add this method as a list field. Here is an example: First add a new method returning the HTML for the image inclusion: class Article(models.Model): … def admin_image(self): return ‘<img src=”%s”/>’ % self.img admin_image.allow_tags = True Then add … Read more

Nested inlines in the Django admin?

As of now there is no “built-in” way to have nested inlines (inline inside inline) in django.contrib.admin. Pulling something like this off is possible by having your own ModelAdmin and InlineModelAdmin subclasses that would enable this kind of functionality. See the patches on this ticket http://code.djangoproject.com/ticket/9025 for ideas on how to implement this. You’d also … Read more

Django: How to get current user in admin forms?

Here is what i did recently for a Blog: class BlogPostAdmin(admin.ModelAdmin): form = BlogPostForm def get_form(self, request, **kwargs): form = super(BlogPostAdmin, self).get_form(request, **kwargs) form.current_user = request.user return form I can now access the current user in my forms.ModelForm by accessing self.current_user EDIT: This is an old answer, and looking at it recently I realized the … Read more

Using Django auth UserAdmin for a custom user model

After digging around the Django source code for a while, I found a working soultion. I am not totally happy with this solution, but it seems to work. Feel free to suggest better solutions! Django uses UserAdmin to render the nice admin look for User model. By just using this in our admin.py-file, we can … Read more

Overriding admin css in django

It depends a lot of what you want to do. Though first of all: do not overwrite it in the Django admin directly. You got two options I think are reasonable: If you want to change the appearance of the admin in general you should override admin templates. This is covered in details here: Overriding … Read more

Tying in to Django Admin’s Model History

The admin history is just an app like any other Django app, with the exception being special placement on the admin site. The model is in django.contrib.admin.models.LogEntry. When a user makes a change, add to the log like this (stolen shamelessly from contrib/admin/options.py: from django.utils.encoding import force_unicode from django.contrib.contenttypes.models import ContentType from django.contrib.admin.models import LogEntry, … Read more

Django Admin – change header ‘Django administration’ text

As of Django 1.7 you don’t need to override templates. You can now implement site_header, site_title, and index_title attributes on a custom AdminSite in order to easily change the admin site’s page title and header text. Create an AdminSite subclass and hook your instance into your URLconf: admin.py: from django.contrib.admin import AdminSite from django.utils.translation import … Read more