How to have 2 different admin sites in a Django project?

You can subclass Django’s AdminSite (put it eg. in admin.py in your project root): from django.contrib.admin.sites import AdminSite class MyAdminSite(AdminSite): pass #or overwrite some methods for different functionality myadmin = MyAdminSite(name=”myadmin”) At least from 1.9 on you need to add the name parameter to make it work properly. This is used to create the revers … Read more

Django: access the parent instance from the Inline model admin

Django < 2.0 Answer: Use Django’s Request object (which you have access to) to retrieve the request.path_info, then retrieve the PK from the args in the resolve match. Example: from django.contrib import admin from django.core.urlresolvers import resolve from app.models import YourParentModel, YourInlineModel class YourInlineModelInline(admin.StackedInline): model = YourInlineModel def get_parent_object_from_request(self, request): “”” Returns the parent object … Read more

Permission to view, but not to change! – Django

Update: Since Django 2.1 this is now built-in. In admin.py # Main reusable Admin class for only viewing class ViewAdmin(admin.ModelAdmin): “”” Custom made change_form template just for viewing purposes You need to copy this from /django/contrib/admin/templates/admin/change_form.html And then put that in your template folder that is specified in the settings.TEMPLATE_DIR “”” change_form_template=”view_form.html” # Remove the … Read more

Extending Django Admin Templates – altering change list

To expand on Yuji’s answer, here are some specifics on overriding change_list_results.html … Override changelist_view as described above in step 1, and also described here at djangoproject. Or auto-override by placing in the appropriate directory as in step 2 above. (Note that the step 2 path shown above is model-specific. App-specific would be /admin/<MyAppName>/change_list.html under … Read more

How to run cloned Django project?

First off, you are getting that error because you are starting a project within the same directory as the cloned project, this directory already contains an app with the name ig_miner_app hence the name conflict. As regards steps to running the project by other users , this should work. clone the project git clone https://github.com/erinallard/instagram_miner.git … Read more

Password field in Django model

As @mlissner suggested the auth.User model is a good place to look. If you check the source code you’ll see that the password field is a CharField. password = models.CharField(_(‘password’), max_length=128, help_text=_(“Use ‘[algo]$[salt]$[hexdigest]’ or use the <a href=\”password/\”>change password form</a>.”)) The User model also has a set_password method. def set_password(self, raw_password): import random algo = … Read more

How to add a sortable count column to the Django admin of a model with a many-to-one relation?

I had the same issue (I cannot change my model’s manager to add slow annotations or joins). A combination of two of the answers here works. @Andre is really close, the Django Admin supports modifying the queryset for just the admin, so apply the same logic here and then user the admin_order_field attribute. You still … Read more

Can I make list_filter in django admin to only show referenced ForeignKeys?

As of Django 1.8, there is a built in RelatedOnlyFieldListFilter, which you can use to show related countries. class MyModelAdmin(admin.ModelAdmin): list_display = (‘name’, ‘country’,) list_filter = ( (‘country’, admin.RelatedOnlyFieldListFilter), ) For Django 1.4-1.7, list_filter allows you to use a subclass of SimpleListFilter. It should be possible to create a simple list filter that lists the … Read more

Django Admin: Using a custom widget for only one model field

Create a custom ModelForm for your ModelAdmin and add ‘widgets’ to its Meta class, like so: class StopAdminForm(forms.ModelForm): class Meta: model = Stop widgets = { ‘field_name’: ApproveStopWidget(), } fields=”__all__” class StopAdmin(admin.ModelAdmin): form = StopAdminForm Done! Documentation for this is sort of non-intuitively placed in the ModelForm docs, without any mention to it given in … 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