Opened 18 years ago

Closed 17 years ago

#2522 closed defect (duplicate)

Error when 2 foreign keys to same Model w/ edit_inline=True

Reported by: mgraziosi@… Owned by: anonymous
Component: contrib.admin Version: 0.95
Severity: blocker Keywords: graph, graphs, FormWrapper
Cc: Maniac@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

It seems that Django has some serious restrictions/bug when there are two foreign keys pointing to the same Model and one of them has the edit_inline attribute set.

Suppose the following problem: a need to represent the concept of a callgroup: a list of items (either persons or, again, callgroups) to which phone calls should be dispatched in a given sequence. The model is illustrated at the end of this ticket.

When I try to use the Admin interface it seems to get
confused:

  • If I set editable=False in callgroup_owner the Admin interface gets confused because there are two foreign keys from CallgroupItem to Callgroup: callgroup_owner and callgroup_member. In fact the Admin interface shows a TABULAR list of Callgroup items and, also, a STACKED list (however, only one should be shown).
  • If I set editable=True in callgroup_owner when I try to Add a new callgroup I get KeyError exception with the following offending template line:
    {% for related_object in inline_related_objects %} ...
    

I'm using version 0.96-pre of Django. Following is the data model I used (models.py) to isolate and reproduce the bug:

class Person (models.Model):
    name = models.CharField(maxlength=15)

class Callgroup (models.Model):
    name = models.CharField(maxlength=15)
    class Admin:
        pass

class CallgroupItem (models.Model):
    sequence = models.IntegerField(core=True)

    # Owner of this callgroup item
    callgroup_owner = models.ForeignKey(Callgroup,
            related_name="callgroup_owner",
            editable=False, # Error even if True
            edit_inline=models.TABULAR,
            )

    # Callgroup to be called (if not NULL)
    callgroup_member = models.ForeignKey(Callgroup,
            related_name="callgroup_member",
            null=True, blank=True,
            )
    # Person to be called (if not NULL)
    person_member = models.ForeignKey(Person,
            related_name="person_member",
            null=True, blank=True
            )

Change History (4)

comment:1 by Maniac <Maniac@…>, 18 years ago

Cc: Maniac@… added

Looks very similar to #2421 that was recently discussed in my forum. I'm not very deep in the problem myself so I don't know if it's really a dupe or just looks like it.

comment:2 by jose.gessoft@…, 17 years ago

Keywords: graph graphs FormWrapper added
Owner: changed from Adrian Holovaty to jose.gessoft@…
priority: normalhighest
Severity: normalblocker
Version: SVN0.95

¡¡¡THE SADEST THING IS THAT THIS BUG MAKES THE ADMIN APP, UNCAPABLE OF ADMINISTRATING ANY GRAPH-LIKE MODELS!!!
Please pay attention on this matter, i am trying to fix it my self.

I have a simplier example with the same problem:

class Graph_Arc(models.Model):

from_Node = models.ForeignKey('GraphNode',related_name='outputArcs', edit_inline=True)
to_Node = models.ForeignKey('GraphNode',related_name='inputArcs',core=True)
class Admin:

pass

class GraphNode(models.Model):

value = models.CharField(maxlength=100, verbose_name=_('Valor del Nodo'))
def str(self):

return "%s" % (self.value)

class Admin:

pass

TRACEBACK WHEN CALLEN THE admin/add PAGE FOR THE GraphNodes:

Traceback (most recent call last):
File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\template\init.py" in render_node

  1. result = node.render(context)

File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\template\defaulttags.py" in render

  1. nodelist.append(node.render(context))

File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\contrib\admin\templatetags\admin_modify.py" in render

  1. bound_related_object = relation.bind(contextform, original, bound_related_object_class)

File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\db\models\related.py" in bind

  1. return bound_related_object_class(self, field_mapping, original)

File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\contrib\admin\templatetags\admin_modify.py" in init

  1. self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping ,fields, i)

File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\contrib\admin\templatetags\admin_modify.py" in init

  1. self.bound_fields = [AdminBoundField(field, self.field_mapping, field_mappingoriginal)

File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\contrib\admin\views\main.py" in init

  1. self.form_fields = [field_mapping[name] for name in self.field.get_manipulator_field_names()]

File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\forms\init.py" in getitem

  1. return self.formfield_dict[template_key]

KeyError at /admin/appdemo/graphnode/add/
'from_Node'

NOTES:
It seems to be that in this case the manipulator is throwing the related edit_inline field to the FormWrapper, but the FormWrapper hasn't this field as editable so it throws the exception. The FormWrapper is ok to throw the error, the problem is in the other part: Django shouldn't tell the FormWrapper to render this object.

comment:3 by anonymous, 17 years ago

Owner: changed from jose.gessoft@… to anonymous
Status: newassigned

Sorry about the format of the previous post, i have never used this before.
Here is the code again:

class Graph_Arc(models.Model):
    from_Node = models.ForeignKey('GraphNode',related_name='outputArcs', edit_inline=True)
    to_Node = models.ForeignKey('GraphNode',related_name='inputArcs',core=True)
    class Admin:
        pass

class GraphNode(models.Model):
    value = models.CharField(maxlength=100, verbose_name=_('Valor del Nodo'))
    def __str__(self):
        return "%s" % (self.value)
    class Admin:
        pass

TRACEBACK WHEN CALLEN THE admin/add PAGE FOR THE GraphNodes:

Traceback (most recent call last):
File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\template\__init__.py" in render_node
  706. result = node.render(context)
File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\template\defaulttags.py" in render
  118. nodelist.append(node.render(context))
File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\contrib\admin\templatetags\admin_modify.py" in render
  166. bound_related_object = relation.bind(context['form'], original, bound_related_object_class)
File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\db\models\related.py" in bind
  126. return bound_related_object_class(self, field_mapping, original)
File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\contrib\admin\templatetags\admin_modify.py" in __init__
  147. self.form_field_collection_wrappers = [FormFieldCollectionWrapper(field_mapping ,fields, i)
File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\contrib\admin\templatetags\admin_modify.py" in __init__
  123. self.bound_fields = [AdminBoundField(field, self.field_mapping, field_mapping['original'])
File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\contrib\admin\views\main.py" in __init__
  112. self.form_fields = [field_mapping[name] for name in self.field.get_manipulator_field_names('')]
File "C:\Python24\lib\site-packages\django-0.95-py2.4.egg\django\forms\__init__.py" in __getitem__
  195. return self.formfield_dict[template_key]

  KeyError at /admin/appdemo/graphnode/add/
  'from_Node'

comment:4 by Gary Wilson <gary.wilson@…>, 17 years ago

Resolution: duplicate
Status: assignedclosed

duplicate of #1939.

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