Opened 4 years ago

Closed 4 years ago

#17871 closed Bug (worksforme)

Broken: GenericInlineModelAdmin and two ForeignKey

Reported by: danny.adair@… Owned by: nobody
Component: contrib.admin Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


A scenario that I would not call exotic at all:

    from django.contrib.contenttypes import generic
    from django.contrib.contenttypes.models import ContentType
    from django.db import models
    class Whatever(models.Model):
        something = models.CharField(max_length=254)

    class Person(models.Model):
        name = models.CharField(max_length=254)
    class Action(models.Model):
        content_type = models.ForeignKey(ContentType)
        object_id = models.PositiveIntegerField()
        content_object = generic.GenericForeignKey('content_type', 'object_id')
        task = models.CharField(max_length=254)
        accountable = models.ForeignKey(Person, related_name='actions_accountable_for', null=True, blank=True)
        assigned_to = models.ForeignKey(Person, related_name='actions_assigned_to', null=True, blank=True)
        due_date = models.DateField(null=True, blank=True)
        completed_date = models.DateField(null=True, blank=True)

So I want to attach an "Action" (something that needs to be done) to various other things (generic relation), and it has a Person being accountable and a Person that's assigned to it. They have a different related_name so all good.

    from django.contrib import admin
    from django.contrib.contenttypes.generic import GenericStackedInline
    from models import Action, Whatever
    class ActionInline(GenericStackedInline):
        model = Action
    class WhateverAdmin(admin.ModelAdmin):
        inlines = [ActionInline], WhateverAdmin)

That throws the exception

    <class 'myproject.myapp.models.Action'> has more than 1 ForeignKey to <class 'myproject.myapp.models.Person'>

This happens in admin.validation.validate_inline() triggered by ", WhateverAdmin)".

I don't get it. The related_name are different, and the foreign key for the inline is a GenericForeignKey. I thought maybe I just have to satisfy that (buggy?) validation and set 'fk_name' in ActionInline to one of the two (as the GenericStackedInline won't be using it anyway). But then I get:

    fk_name 'accountable' is not a ForeignKey to <class 'myproject.myapp.models.Whatever'>

Sure it's not. But it's not the relevant foreign key for this inline - the generic foreign key is. Is this a bug? How can I get around this?

The documentation states I can use a GenericInlineModelAdmin
It also says that if an inline has two ForeignKeys to the same model, "fk_name" needs to be specified
That seems to assume that the two ForeignKeys are used in the inline for that foreign model.

What's also freaky is that with settings.DEBUG=True the Exception is thrown but with DEBUG=False the change_form is rendered without complaints (and seemingly working).

Change History (1)

comment:1 in reply to: ↑ description Changed 4 years ago by ramiro

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to worksforme
  • Status changed from new to closed

Replying to danny.adair@…:


That throws the exception

    <class 'myproject.myapp.models.Action'> has more than 1 ForeignKey to <class 'myproject.myapp.models.Person'>

This happens in admin.validation.validate_inline() triggered by ", WhateverAdmin)".

Can't reproduce this. Tested with 1.3 and trunk. I used your models removing the DateField's in Action.

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