Code

Opened 8 years ago

Closed 7 years ago

#2828 closed defect (fixed)

TypeError when deleting objects with ManyToMany(self) relationships in the admin

Reported by: anonymous Owned by: adrian
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

When trying to delete an object with a ManyToMany(self) relationship you get a "TypeError getattr(): attribute name must be string". Have played a little around it and it seems to work correctly when adding "filter_interface=models.HORIZONTAL" to the ManyToManyField(self) Field.

Error:
TypeError at /admin/person/person/2/delete/
getattr(): attribute name must be string

Produces an error:

class Person(models.Model):
    name = models.CharField(maxlength=20)
    friends = models.ManyToManyField('self',blank=True)
    idols = models.ManyToManyField('self', symmetrical=False,related_name='stalkers',blank=True)

    def __str__(self):
        return self.name

    class Admin:
        pass 

Works ok:

class Person(models.Model):
    name = models.CharField(maxlength=20)
    friends = models.ManyToManyField('self',blank=True)
    idols = models.ManyToManyField('self', symmetrical=False,related_name='stalkers',blank=True,filter_interface=models.HORIZONTAL)

    def __str__(self):
        return self.name

    class Admin:
        pass 

Attachments (0)

Change History (5)

comment:1 Changed 8 years ago by anonymous

Still doesn't work, also with filter_interface=models.HORIZONTAL.

comment:2 Changed 8 years ago by Gary Wilson <gary.wilson@…>

It would be helpful to include the full traceback.

comment:3 Changed 8 years ago by james.mulholland@…

See:

http://groups.google.com/group/django-developers/browse_thread/thread/b8584e0ea9e4d544/a502482e449e28b0#a502482e449e28b0

AFAICT, the solution posted in that thread by Brian Beck does work, although I haven't thoroughly tested it yet. The changes are:

In file django/contrib/admin/views/main.py at line 463, replace:

463:    has_related_objs = False
464:    rel_objs = getattr(obj, rel_opts_name, None)

with

463:    has_related_objs = False
464:    if rel_opts_name:
465:        rel_objs = getattr(obj, rel_opts_name, None)
466:    else:
467:        rel_objs = None

In file django/db/models/related.py comment lines 135 and 136:

134:    # If this is a symmetrical m2m relation on self, there is no reverse accessor.
135:    if getattr(self.field.rel, 'symmetrical', False) and self.model == self.parent_model:
136:        return None

Change to:

134:    # If this is a symmetrical m2m relation on self, there is no reverse accessor.
135:    #if getattr(self.field.rel, 'symmetrical', False) and self.model == self.parent_model:
136:    #    return None

comment:4 Changed 7 years ago by Simon G. <dev@…>

  • Has patch set
  • Triage Stage changed from Unreviewed to Ready for checkin
  • Version 0.95 deleted

comment:5 Changed 7 years ago by jacob

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

(In [4616]) Fixed #2828, a TypeError when deleting objects with m2m relations to self. Thanks, Brian Beck.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.