Opened 10 years ago

Closed 10 years ago

Last modified 10 years ago

#22982 closed Bug (fixed)

Coercing GenericRelation property to string raises StopIteration exception

Reported by: Ben Davis Owned by: nobody
Component: contrib.contenttypes Version: 1.7-rc-1
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

For example,

class TaggedItem(models.Model):
    tag = models.SlugField()
    content_type = models.ForeignKey(ContentType)
    object_id = models.PositiveIntegerField()
    content_object = GenericForeignKey('content_type', 'object_id')


class Bookmark(models.Model):
    url = models.URLField()
    tags = GenericRelation(TaggedItem)


{{{
#!python
>>> b = Bookmark.objects.create(url='https://www.djangoproject.com/')
>>> str(b.tags)
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "django/db/models/manager.py", line 81, in __str__
    in opts.concrete_managers + opts.abstract_managers
StopIteration
}}}

Change History (6)

comment:1 by Tim Graham, 10 years ago

Easy pickings: unset
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

Do you know if it's a regression in 1.7?

comment:2 by Ben Davis, 10 years ago

I believe so. The bug occurs in stable/1.7.x, and it seems the code for Manager.__str__ was added here: https://github.com/django/django/commit/d818e0c9#diff-bf776a3b8e5dbfac2432015825ef8afeR166

Last edited 10 years ago by Ben Davis (previous) (diff)

comment:3 by Ben Davis, 10 years ago

A little more detail: A GenericRelation attribute is a descriptor that returns a dynamically created Manager. Since Manager.contribute_to_class is not called during this process, it does not get added to the model's concrete_managers or abstract_managers.

I guess the question at this point is whether or not it should be included as an abstract/concrete manager, or if the __str__ method should simply catch StopIteration and return repr(self) as a fallback.

comment:4 by jacobh, 10 years ago

I've put up a pull request here https://github.com/django/django/pull/3030

comment:5 by Tim Graham <timograham@…>, 10 years ago

Resolution: fixed
Status: newclosed

In 29585e9b6a0ce15dc0149c8424cd11c3e27ee295:

[1.7.x] Fixed #22982 -- Added GenericRelatedObjectManager.str. to prevent crash.

Thanks bendavis78 for the report.

comment:6 by Tim Graham <timograham@…>, 10 years ago

In f676305ecede116332884ea682fc953c29813232:

Fixed #22982 -- Added GenericRelatedObjectManager.str. to prevent crash.

Thanks bendavis78 for the report.

Forwardport of 29585e9b6a from stable/1.7.x

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