Opened 6 years ago

Last modified 5 years ago

#14408 new Bug

Admin Error when subclassing ContentType for generic Relations

Reported by: MS Owned by: nobody
Component: contrib.admin Version: 1.2
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


If you subclass the ContentType with a proxy model, for example to use other ordering or change some other Meta options, you will get an error in the Admin interface, when using GenericTabularInline.

The problem is line 357 in, where the test is with equal but should be subcalss instead.

Example in models:

from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType as CT

class ContentType(CT):

    class Meta:
        proxy = True
        ordering = ('app_label', 'model')

class ModelWithGeneric(models.Model):
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    contentObject = generic.GenericForeignKey('content_type', 'object_id')

class ObjectWithGeneric(models.Model):
    media = generic.GenericRelation(ModelWithGeneric, content_type_field='content_type', object_id_field='object_id')

Example in Admin:

from django.contrib.contenttypes import generic
from django.contrib import admin

from apps.models import *

class GenericInlineAdmin(generic.GenericTabularInline):
    model = ModelWithGeneric

class ObjectAdmin(admin.ModelAdmin):
    inlines = (
    ), ObjectAdmin)

Error is: fk_name '<django.db.models.fields.related.ForeignKey object at 0x0319A1F0>' is not a ForeignKey to ContentType

Change History (8)

comment:1 Changed 6 years ago by matiasb

  • Needs documentation unset
  • Needs tests unset
  • Owner changed from nobody to matiasb
  • Patch needs improvement unset

comment:2 Changed 6 years ago by matiasb

  • Owner matiasb deleted

comment:3 Changed 6 years ago by matiasb

  • Owner set to nobody

comment:4 Changed 6 years ago by russellm

  • Triage Stage changed from Unreviewed to Accepted

The line is now 374, inside generic_inlineformset_factory.

comment:5 Changed 5 years ago by julien

  • Component changed from Contrib apps to django.contrib.admin

comment:6 Changed 5 years ago by julien

  • Severity set to Normal
  • Type set to Bug

comment:7 Changed 4 years ago by aaugustin

  • UI/UX unset

Change UI/UX from NULL to False.

comment:8 Changed 4 years ago by aaugustin

  • Easy pickings unset

Change Easy pickings from NULL to False.

Note: See TracTickets for help on using tickets.
Back to Top