Created
September 21, 2011 16:55
-
-
Save rlr/1232640 to your computer and use it in GitHub Desktop.
GenericRelation test case
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [1]: from testapp.models import ModelA | |
In [2]: a = ModelA.objects.all()[0] | |
In [3]: a.my_related_cs | |
Out[3]: <django.db.models.fields.related.RelatedManager object at 0x101e1ca10> | |
In [4]: a.my_related_bs | |
--------------------------------------------------------------------------- | |
AttributeError Traceback (most recent call last) | |
/Users/rlr/dev/testproj/<ipython console> in <module>() | |
AttributeError: 'ModelA' object has no attribute 'my_related_bs' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.contrib.contenttypes.models import ContentType | |
from django.contrib.contenttypes import generic | |
from django.db import models | |
class ModelA(models.Model): | |
name = models.CharField(max_length=42) | |
content_type = models.ForeignKey(ContentType) | |
object_id = models.PositiveIntegerField() | |
content_object = generic.GenericForeignKey('content_type', 'object_id') | |
class ModelB(models.Model): | |
name = models.CharField(max_length=42) | |
model_a = generic.GenericRelation(ModelA, related_name='my_related_bs') | |
class ModelC(models.Model): | |
name = models.CharField(max_length=42) | |
model_a = models.ForeignKey(ModelA, related_name='my_related_cs') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In [9]: from testapp.models import ModelB | |
In [10]: b = ModelB.objects.all()[0] | |
# This is code from django.contrib.admin.utils.get_deleted_objects which is called | |
# from the admin's delete confirmation view: | |
In [11]: from django.db import router | |
In [12]: using = router.db_for_write(ModelB) | |
In [13]: from django.contrib.admin.util import NestedObjects | |
In [14]: collector = NestedObjects(using=using) | |
In [15]: collector.collect([b]) | |
ERROR: An unexpected error occurred while tokenizing input | |
The following traceback may be corrupted or invalid | |
The error message is: ('EOF in multi-line statement', (11, 0)) | |
--------------------------------------------------------------------------- | |
AttributeError Traceback (most recent call last) | |
/Users/rlr/dev/testproj/<ipython console> in <module>() | |
/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/contrib/admin/util.pyc in collect(self, objs, source_attr, **kwargs) | |
125 self.add_edge(None, obj) | |
126 try: | |
--> 127 return super(NestedObjects, self).collect(objs, source_attr=source_attr, **kwargs) | |
128 except models.ProtectedError, e: | |
129 self.protected.update(e.protected_objects) | |
/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/db/models/deletion.pyc in collect(self, objs, source, nullable, collect_related, source_attr, reverse_dependency) | |
179 source=model, | |
180 source_attr=relation.rel.related_name, | |
--> 181 nullable=True) | |
182 | |
183 def related_objects(self, related, objs): | |
/Users/rlr/.virtualenvs/django/lib/python2.6/site-packages/django/contrib/admin/util.pyc in collect(self, objs, source_attr, **kwargs) | |
121 for obj in objs: | |
122 if source_attr: | |
--> 123 self.add_edge(getattr(obj, source_attr), obj) | |
124 else: | |
125 self.add_edge(None, obj) | |
AttributeError: 'ModelA' object has no attribute 'my_related_bs' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In
ModelB
doesGenericRelation
have arelated_name
param? I thought that was only ForeignKeys