#17871: Broken: GenericInlineModelAdmin and two ForeignKey
-------------------------------+--------------------
Reporter: danny.adair@… | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.3
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------+--------------------
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).
--
Ticket URL: <https://code.djangoproject.com/ticket/17871>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.