Django

Code

Ticket #2522 (closed: duplicate)

Opened 2 years ago

Last modified 2 years ago

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

Reported by: mgraziosi@fgasoftware.com Assigned to: anonymous
Milestone: Component: django.contrib.admin
Version: 0.95 Keywords: graph, graphs, FormWrapper
Cc: Maniac@SoftwareManiacs.Org Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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
            )

Attachments

Change History

08/11/06 07:33:03 changed by Maniac <Maniac@SoftwareManiacs.Org>

  • cc set to Maniac@SoftwareManiacs.Org.

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.

01/13/07 19:00:13 changed by jose.gessoft@gmail.com

  • keywords set to graph, graphs, FormWrapper.
  • owner changed from adrian to jose.gessoft@gmail.com.
  • version changed from SVN to 0.95.
  • severity changed from normal to blocker.
  • priority changed from normal to highest.

¡¡¡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.

01/13/07 19:03:14 changed by anonymous

  • owner changed from jose.gessoft@gmail.com to anonymous.
  • status changed from new to assigned.

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'

01/15/07 11:43:24 changed by Gary Wilson <gary.wilson@gmail.com>

  • status changed from assigned to closed.
  • resolution set to duplicate.

duplicate of #1939.


Add/Change #2522 (Error when 2 foreign keys to same Model w/ edit_inline=True)




Change Properties
Action