Code

Opened 6 years ago

Closed 6 years ago

Last modified 3 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: master
Severity: Keywords: GenericForeignKey
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description (last modified by mtredinnick)

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/

Attachments (0)

Change History (6)

comment:1 Changed 6 years ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 Changed 6 years ago by mtredinnick

  • Description modified (diff)

Fixed description formatting.

comment:3 Changed 6 years ago by ericholscher

  • milestone set to 1.0
  • Triage Stage changed from Unreviewed to Design decision needed

comment:4 Changed 6 years ago by jacob

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

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

comment:5 Changed 4 years ago by oliland

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

comment:6 Changed 3 years ago by jacob

  • milestone 1.0 deleted

Milestone 1.0 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.