Opened 17 years ago

Closed 16 years ago

Last modified 13 years ago

#6805 closed (wontfix)

Get exception when initializing a GenericForeignKey using Model.get_or_create()

Reported by: niklas@… 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 (last modified by Malcolm Tredinnick)

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

Also on http://dpaste.com/39871/

Change History (6)

comment:1 by anonymous, 17 years ago

Sorry for my incredibly bad formatting... I'm new to this. I'm reading the FAQ on how to submit but I don't know how to edit my description. I have registered an account now as well.

comment:2 by Malcolm Tredinnick, 17 years ago

Description: modified (diff)

Fixed description formatting.

comment:3 by Eric Holscher, 16 years ago

milestone: 1.0
Triage Stage: UnreviewedDesign decision needed

comment:4 by Jacob, 16 years ago

Resolution: wontfix
Status: newclosed

As you've no doubt discovered, GFK's aren't "really" fields -- they're just syntactic sugar over a pair of fields.

comment:5 by oliland, 14 years ago

It seems odd that this is inconsistent with Model.object.create(), which accepts a content_object field.

comment:6 by Jacob, 13 years ago

milestone: 1.0

Milestone 1.0 deleted

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