Ensure that a GenericReferenceField properly dereferences items.
(self)
| 1588 | brother.save() |
| 1589 | |
| 1590 | def test_generic_reference(self): |
| 1591 | """Ensure that a GenericReferenceField properly dereferences items.""" |
| 1592 | |
| 1593 | class Link(Document): |
| 1594 | title = StringField() |
| 1595 | meta = {"allow_inheritance": False} |
| 1596 | |
| 1597 | class Post(Document): |
| 1598 | title = StringField() |
| 1599 | |
| 1600 | class Bookmark(Document): |
| 1601 | bookmark_object = GenericReferenceField() |
| 1602 | |
| 1603 | Link.drop_collection() |
| 1604 | Post.drop_collection() |
| 1605 | Bookmark.drop_collection() |
| 1606 | |
| 1607 | link_1 = Link(title="Pitchfork") |
| 1608 | link_1.save() |
| 1609 | |
| 1610 | post_1 = Post(title="Behind the Scenes of the Pavement Reunion") |
| 1611 | post_1.save() |
| 1612 | |
| 1613 | bm = Bookmark(bookmark_object=post_1) |
| 1614 | bm.save() |
| 1615 | |
| 1616 | bm = Bookmark.objects(bookmark_object=post_1).first() |
| 1617 | |
| 1618 | assert bm.bookmark_object == post_1 |
| 1619 | assert isinstance(bm.bookmark_object, Post) |
| 1620 | |
| 1621 | bm.bookmark_object = link_1 |
| 1622 | bm.save() |
| 1623 | |
| 1624 | bm = Bookmark.objects(bookmark_object=link_1).first() |
| 1625 | |
| 1626 | assert bm.bookmark_object == link_1 |
| 1627 | assert isinstance(bm.bookmark_object, Link) |
| 1628 | |
| 1629 | def test_generic_reference_list(self): |
| 1630 | """Ensure that a ListField properly dereferences generic references.""" |