Opened 10 years ago

Closed 10 years ago

Last modified 8 years ago

#259 closed defect (invalid)

Admin errors with edit_inline and foreign keys.

Reported by: andrew@… Owned by: adrian
Component: contrib.admin Version:
Severity: major Keywords: ManyToMany edit_inline
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

The following model breaks the admin interface:

from django.core import meta

# Create your models here.
class Parent(meta.Model):
    fields = (
        meta.CharField('title', maxlength=50),
    )

    admin = meta.Admin()

    def __repr__(self):
        return self.title

class Child(meta.Model):
    fields = (
        meta.CharField('title', maxlength=50),
    )

    admin = meta.Admin()

    def __repr__(self):
        return self.title

class Relate(meta.Model):
    fields = (
        meta.ForeignKey(Parent, edit_inline=True,
                        edit_inline_type=meta.TABULAR),
        meta.ForeignKey(Child),
    )

    def __repr__(self):
        return self.get_parent().title + " -> " + self.get_child().title

If there are no records in the 'relate' table, there appears to be no way to add them. If there are, then pressing 'save' in the parent admin view when one of the records shows a line of hyphens results in an error.

Change History (4)

comment:1 Changed 10 years ago by jacob

  • Resolution set to invalid
  • Status changed from new to closed

You need to specify num_in_admin so that Django knows how many inline-editable fields should be there.

comment:2 Changed 10 years ago by andrew@…

  • Resolution invalid deleted
  • Status changed from closed to reopened

I tried that -- the model above was an attempt to get a minimal model that still exhibited the behavior I was seeing. The big problem is not the display problem anyway, it's that pressing save gives this traceback:

There's been an error:

Traceback (most recent call last):

  File "/home/axa/django/django_src/django/core/handlers/base.py", line 63, in get_response
    return callback(request, **param_dict)

  File "/home/axa/django/django_src/django/views/admin/main.py", line 864, in change_stage
    new_object = manipulator.save(new_data)

  File "/home/axa/django/django_src/django/utils/functional.py", line 3, in _curried
    return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items()))

  File "/home/axa/django/django_src/django/core/meta/__init__.py", line 1470, in manipulator_save
    new_rel_obj.save()

  File "/home/axa/django/django_src/django/utils/functional.py", line 3, in _curried
    return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items()))

  File "/home/axa/django/django_src/django/core/meta/__init__.py", line 734, in method_save
    opts.pk.name), db_values + [getattr(self, opts.pk.name)])

  File "/home/axa/django/django_src/django/core/db/base.py", line 10, in execute
    result = self.cursor.execute(sql, params)

ProgrammingError: ERROR:  invalid input syntax for integer: ""

UPDATE test_admin_relates SET parent_id='4',child_id='' WHERE id='3'

I realise I should probably have raised two separate cases and included the traceback in my report. Sorry.

comment:3 Changed 10 years ago by hugo <gb@…>

This is due to the fact that you use edit_inline but didn't define any field with core=True. This is needed for django to know what lines actually are empty or deleted. If your child table has some name field or something like that that shouldn't be empty, just give that the core=True flag and then django won't try to save empty lines any more. At least that's what I stumbled over myself when I forgot to use core=True with edit_inline=True :-)

comment:4 Changed 10 years ago by jacob

  • Resolution set to invalid
  • Status changed from reopened to closed
Note: See TracTickets for help on using tickets.
Back to Top