How to force-save an “empty”/unchanged django admin inline?

It took me quite some time to figure out but it is actually really simple. from django.contrib import admin from django.forms.models import BaseInlineFormSet, ModelForm class AlwaysChangedModelForm(ModelForm): def has_changed(self): “”” Should returns True if data differs from initial. By always returning true even unchanged inlines will get validated and saved.””” return True class CheckerInline(admin.StackedInline): “”” Base … Read more

How do I add a link from the Django admin page of one object to the admin page of a related object?

Use readonly_fields: class MyInline(admin.TabularInline): model = MyModel readonly_fields = [‘link’] def link(self, obj): url = reverse(…) return mark_safe(“<a href=”https://stackoverflow.com/questions/9919780/%s”>edit</a>” % url) # the following is necessary if ‘link’ method is also used in list_display link.allow_tags = True

django admin inlines: get object from formfield_for_foreignkey

Another way, that, IMHO, feels cleaner than, but is similar to @erichonkanen’s answer is something like this: class ProjectGroupMembershipInline(admin.StackedInline): # irrelevant bits…. def formfield_for_foreignkey(self, db_field, request, **kwargs): if db_field.name == “group”: try: parent_id = request.resolver_match.args[0] kwargs[“queryset”] = Group.objects.filter(some_column=parent_id) except IndexError: pass return super().formfield_for_foreignkey(db_field, request, **kwargs)

How to register multiple models with the admin?

admin.site.register has this definition in the library: def register(self, model_or_iterable, admin_class=None, **options): so models to be registered can be a single model or iterable object so just use this: myModels = [models.Project, models.Client, models.About] # iterable list admin.site.register(myModels) I tested this in my site and works perfectly fine.

Django InlineModelAdmin: Show partially an inline model and link to the complete model

UPDATE: Since Django 1.8, this is built in. See this answer and the official documentation. OLD ANSWER: At the end I found a simple solution. I create a new template called linked.html that is a copy of tabular.html and I added this code to create the link. {% if inline_admin_form.original.pk %} <td class=”{{ field.field.name }}”> … 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

Redirect on admin Save

To change the redirect destination after save in the admin, you need to override response_add() (for adding new instances) and response_change() (for changing existing ones) in the ModelAdmin class. See the original code in django.contrib.admin.options. Quick examples to make it clearer how to do this (would be within a ModelAdmin class): from django.core.urlresolvers import reverse … Read more

Paginator for inline models in django admin

If anyone requires this, I found this nice (though described as “quite hacky”) implementation of a pagination TabularInline subclass in this comment of a django-suit issue. For Django 1.6 it requires a template change and subclassing this PaginationInline class: from django.contrib import admin from django.contrib.admin.views.main import ChangeList from django.core.paginator import EmptyPage, InvalidPage, Paginator class InlineChangeList(object): … Read more

Is there a way to get custom Django admin actions to appear on the “change” view in addition to the “change list” view?

Here is update and improvement of this answer. It works with django 1.6 and redirects to where you came from. class ActionInChangeFormMixin(object): def response_action(self, request, queryset): “”” Prefer http referer for redirect “”” response = super(ActionInChangeFormMixin, self).response_action(request, queryset) if isinstance(response, HttpResponseRedirect): response[‘Location’] = request.META.get(‘HTTP_REFERER’, response.url) return response def change_view(self, request, object_id, extra_context=None): actions = self.get_actions(request) … Read more

How to add the custom button which executes a Django admin action to change form page?

You could take a look at the change_form_template and set it to a custom template of yours and override the response_change method: class MyModelAdmin(admin.ModelAdmin): # A template for a customized change view: change_form_template=”path/to/your/custom_change_form.html” def response_change(self, request, obj): opts = self.model._meta pk_value = obj._get_pk_val() preserved_filters = self.get_preserved_filters(request) if “_customaction” in request.POST: # handle the action on … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)