views:

371

answers:

2

Update 3 (Read This First) :

Yes, this was caused by the object "profile" not having been saved. For those getting the same symptoms, the moral is "If a ForeignKey field seems to be getting set to None when you assign a real object to it, it's probably because that other objects hasn't been saved."

Even if you are 100% sure that it was saved, check again ;-)


Hi,

I'm using content_type / generic foreign keys in a class in Django.

The line to create an instance of the class is roughly this :

tag =  SecurityTag(name='name',agent=an_agent,resource=a_resource,interface=an_interface)

Where both agent and resource are content_type fields.

Most of the time, this works as I expect and creates the appropriate object. But I have one specific case where I call this line to create a SecurityTag but the value of the agent field seems to end up as None.

Now, in this particular case, I test, in the preceding line, that the value of an_agent does contain an existing, saved Django.model object of an agent type. And it does.

Nevertheless, the resulting SecurityTag record comes out with None for this field.

I'm quite baffled by this. I'm guessing that somewhere along the line, something is failing in the ORM's attempt to extract the id of the object in an_agent, but there's no error message nor exception being raised. I've checked that the an_agent object is saved and has a value in its id field.

Anyone seen something like this? Or have any ideas?

====

Update : 10 days later exactly the same bug has come to bite me again in a new context :

Here's some code which describes the "security tag" object, which is basically a mapping between

a) some kind of permission-role (known as "agent" in our system) which is a generic content_type,

b) a resource, which is also a generic content_type, (and in the current problem is being given a Pinax "Profile"),

and c) an "interface" (which is basically a type of access ... eg. "Viewable" or "Editable" that is just a string)

class SecurityTag(models.Model) :
    name = models.CharField(max_length='50')
    agent_content_type = models.ForeignKey(ContentType,related_name='security_tag_agent')
    agent_object_id = models.PositiveIntegerField()
    agent = generic.GenericForeignKey('agent_content_type', 'agent_object_id')

    interface = models.CharField(max_length='50')

    resource_content_type = models.ForeignKey(ContentType,related_name='security_tag_resource')
    resource_object_id = models.PositiveIntegerField()
    resource = generic.GenericForeignKey('resource_content_type', 'resource_object_id')

At a particular moment later, I do this :

print "before %s, %s" % (self.resource,self.agent)
t = SecurityTag(name=self.tag_name,agent=self.agent,resource=self.resource,interface=self.interface_id)
print "after %s, %s, %s, %s" % (t.resource,t.resource_content_type,type(t.resource),t.resource_object_id)

The result of which is that before, the "resource" variable does reference a Profile, but after ...

before phil, TgGroup object
after None, profile, <type 'NoneType'>, None

In other words, while the value of t.resource_content_type has been set to "profile", everything else is None. In my previous encounter with this problem, I "solved" it by reloading the thing I was trying to assign to the generic type. I'm starting to wonder if this is some kind of ORM cache issue ... is the variable "self.resource" holding some kind proxy object rather than the real thing?

One possibility is that the profile hasn't been saved. However, this code is being called as the result of an after_save signal for profile. (It's setting up default permissions), so could it be that the profile save hasn't been committed or something?

Update 2 : following Matthew's suggestion below, I added

print self.resource._get_pk_value() and self.resource.id

which has blown up saying Profile doesn't have _get_pk_value()

+1  A: 

This doesn't really answer my question or satisfy my curiosity but it does seem to work if I pull the an_agent object out of the database immediately before trying to use it in the SecurityTag constructor.

Previously I was passing a copy that had been made earlier with get_or_create. Did this old instance somehow go out of date or scope?

interstar
Did you make sure the old instance was saved between populating its fiends and passing it around?
Ed Brannin
+1  A: 

So here's what I noticed passing through the Django code: when you create a new instance of a model object via a constructor, a pre-init function called (via signals) for any generic object references.

Rather than directly storing the object you pass in, it stores the type and the primary key.

If your object is persisted and has an ID, this works fine, because when you get the field at a later date, it retrieves it from the database.

However -- if your object doesn't have an ID, the fetch code returns nothing, and the getter returns None!

You can see the code in django.contrib.contenttypes.generic.GenericForeignKey, in the instance_pre_init and __get__ functions.

Matthew Christensen