Opened 17 years ago
Last modified 13 years ago
#6805 closed
Get exception when initializing a GenericForeignKey using Model.get_or_create() — at Initial Version
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Keywords: | GenericForeignKey | |
Cc: | Triage Stage: | Design decision needed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
This call will generate an exception because the content_object is not found as field
Property.objects.get_or_create(name=property_name, content_object=self)
This would work fine however (if self is in db as owner of Property):
p = Property(name=property_name, content_object=self)
The last line of the exception is interesting:
TypeError: Cannot resolve keyword 'content_object' into field. Choices are: anexampleowner, resource, id, name, content_type, object_id
It seems that, for some reason, here the variable referring to AnExampleOwner class has been named after the class, i.e.
anexampleowner. Is this by design?
Hee is the code:
class Property(models.Model):
name = models.CharField(max_length=200)#, core=True)
content_object = generic.GenericForeignKey()
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
class AnExampleOwner(models.Model, Owner):
res_props = generic.GenericRelation(Property)
name = models.CharField(max_length=200)
def get_or_create_property(self, property_name):
return Property.objects.get_or_create(name=property_name, content_object=self)
p = mr_dummy.get_or_create_property('description')
File "S:\nc\Projects\Inventive\Ticmate\svn\ticmate\trunk\source\python\ticmate\django\apps\resources\tests.py", line 16, in ticmate.django.apps.resources.tests
Failed example:
p = mr_dummy.get_or_create_property('description')
Exception raised:
Traceback (most recent call last):
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\test\_doctest.py", line 1267, in run
compileflags, 1) in test.globs
File "<doctest ticmate.django.apps.resources.tests[4]>", line 1, in <module>
p = mr_dummy.get_or_create_property('description')
File "S:\nc\Projects\Inventive\Ticmate\svn\ticmate\trunk\source\python\ticmate\django\apps\resources\models.py", line 206, in get_or_create_property
return Property.objects.get_or_create(name=property_name, content_object=self)
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\db\models\manager.py", line 72, in get_or_create
return self.get_query_set().get_or_create(kwargs)
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\db\models\query.py", line 286, in get_or_create
return self.get(kwargs), False
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\db\models\query.py", line 261, in get
obj_list = list(clone)
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\db\models\query.py", line 114, in iter
return iter(self._get_data())
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\db\models\query.py", line 483, in _get_data
self._result_cache = list(self.iterator())
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\db\models\query.py", line 180, in iterator
select, sql, params = self._get_sql_clause()
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\db\models\query.py", line 498, in _get_sql_clause
joins2, where2, params2 = self._filters.get_sql(opts)
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\db\models\query.py", line 720, in get_sql
joins2, where2, params2 = val.get_sql(opts)
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\db\models\query.py", line 771, in get_sql
return parse_lookup(self.kwargs.items(), opts)
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\db\models\query.py", line 926, in parse_lookup
joins2, where2, params2 = lookup_inner(path, lookup_type, value, opts, opts.db_table, None)
File "S:\nc\Projects\Inventive\Ticmate\svn\django-trunk\django\db\models\query.py", line 1044, in lookup_inner
raise TypeError, "Cannot resolve keyword '%s' into field. Choices are: %s" % (name, ", ".join(choices))
TypeError: Cannot resolve keyword 'content_object' into field. Choices are: anexampleowner, resource, id, name, content_type, object_id
‣ About this site
Also on http://dpaste.com/39871/