Opened 12 years ago

Closed 12 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

Description

A scenario that I would not call exotic at all:

models.py

    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.

admin.py

    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]
    admin.site.register(Whatever, 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 "admin.site.register(Whatever, 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?

P.S.:
The documentation states I can use a GenericInlineModelAdmin
https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relations-in-forms-and-admin
It also says that if an inline has two ForeignKeys to the same model, "fk_name" needs to be specified
https://docs.djangoproject.com/en/dev/ref/contrib/admin/#working-with-a-model-with-two-or-more-foreign-keys-to-the-same-parent-model
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)

in reply to:  description comment:1 by Ramiro Morales, 12 years ago

Resolution: worksforme
Status: newclosed

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 "admin.site.register(Whatever, 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