Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

#22034 skip checks for generic InlineAdmin classes #2351

Closed
wants to merge 6 commits into from

2 participants

Jonas Svensson Russell Keith-Magee
Jonas Svensson

I'm introducing dependency to django.contrib.contenttypes.fields.GenericForeignKey.
Don't know if this is acceptable or not.

Jonas Svensson

The lastest approach did not completely work. The previous commit ​josven@b697147 still works.

Russell Keith-Magee
Owner

This PR is headed in the right direction, but isn't quite correct. I've just committed a full patch in 70ec4d7.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
4 django/contrib/contenttypes/admin.py
View
@@ -6,6 +6,7 @@
from django.contrib.contenttypes.forms import (
BaseGenericInlineFormSet, generic_inlineformset_factory
)
+from django.contrib.contenttypes.checks import GenericInlineModelAdminChecks
from django.forms import ALL_FIELDS
from django.forms.models import modelform_defines_fields
@@ -15,6 +16,9 @@ class GenericInlineModelAdmin(InlineModelAdmin):
ct_fk_field = "object_id"
formset = BaseGenericInlineFormSet
+ # Add new check class? So it;s possible to overide _check_fk_name
+ checks_class = GenericInlineModelAdminChecks
+
def get_formset(self, request, obj=None, **kwargs):
if 'fields' in kwargs:
fields = kwargs.pop('fields')
22 django/contrib/contenttypes/checks.py
View
@@ -1,8 +1,28 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
-from django.utils import six
from django.apps import apps
+from django.contrib.admin.checks import InlineModelAdminChecks
+from django.contrib.contenttypes.fields import GenericForeignKey
+from django.core import checks
+from django.forms.models import _get_foreign_key
+from django.utils import six
+
+
+class GenericInlineModelAdminChecks(InlineModelAdminChecks):
+
+ def _check_fk_name(self, cls, parent_model):
+ try:
+ _get_foreign_key(parent_model, cls.model, fk_name=cls.fk_name)
+ except ValueError as e:
+ # Check if generic, if not add error.
+ is_generic_inline_admin = any(
+ isinstance(vf, GenericForeignKey) for vf in parent_model._meta.virtual_fields)
+ if is_generic_inline_admin:
+ return []
+ return [checks.Error(e.args[0], hint=None, obj=cls, id='admin.E202')]
+ else:
+ return []
def check_generic_foreign_keys(**kwargs):
9 tests/admin_checks/models.py
View
@@ -2,6 +2,8 @@
Tests of ModelAdmin system checks logic.
"""
+from django.contrib.contenttypes.fields import GenericForeignKey
+from django.contrib.contenttypes.models import ContentType
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
@@ -55,3 +57,10 @@ class State(models.Model):
class City(models.Model):
state = models.ForeignKey(State)
+
+
+class EggFKContentType(models.Model):
+ content_type = models.ForeignKey(ContentType)
+ object_id = models.PositiveIntegerField()
+ content_object = GenericForeignKey('content_type', 'object_id')
+
19 tests/admin_checks/tests.py
View
@@ -4,11 +4,13 @@
from django import forms
from django.contrib import admin
+from django.contrib.contenttypes.admin import GenericStackedInline
from django.core import checks
from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
-from .models import Song, Book, Album, TwoAlbumFKAndAnE, City, State
+from .models import Song, Book, Album, TwoAlbumFKAndAnE, City, State, \
+ EggFKContentType
class SongForm(forms.ModelForm):
@@ -157,6 +159,21 @@ class ExcludedFieldsAlbumAdmin(admin.ModelAdmin):
]
self.assertEqual(errors, expected)
+ def test_generic_inline_model_admin(self):
+ """
+ Testing if _check_fk_name skips adding errors for
+ generic InlineAdmin. #22034
+ """
+
+ class SongInline(GenericStackedInline):
+ model = Song
+
+ class EggFKContentTypeAdmin(admin.ModelAdmin):
+ inlines = [SongInline]
+
+ errors = EggFKContentTypeAdmin.check(model=EggFKContentType)
+ self.assertEqual(errors, [])
+
def test_exclude_inline_model_admin(self):
"""
Regression test for #9932 - exclude in InlineModelAdmin should not
Something went wrong with that request. Please try again.