Opened 12 months ago

Closed 11 months ago

Last modified 11 months ago

#22982 closed Bug (fixed)

Coercing GenericRelation property to string raises StopIteration exception

Reported by: bendavis78 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 Changed 12 months ago by timo

  • Easy pickings unset
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

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

comment:2 Changed 12 months ago by bendavis78

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 12 months ago by bendavis78 (previous) (diff)

comment:3 Changed 12 months ago by bendavis78

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 Changed 11 months ago by jacobh

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

comment:5 Changed 11 months ago by Tim Graham <timograham@…>

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

In 29585e9b6a0ce15dc0149c8424cd11c3e27ee295:

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

Thanks bendavis78 for the report.

comment:6 Changed 11 months ago by Tim Graham <timograham@…>

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