Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 240123
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T20:38:26+00:00 2026-05-11T20:38:26+00:00

Update 3 (Read This First) : Yes, this was caused by the object profile

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T20:38:26+00:00Added an answer on May 11, 2026 at 8:38 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 111k
  • Answers 111k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The "Effects of Upgrading to Version 8.1" topic in the… May 11, 2026 at 9:48 pm
  • Editorial Team
    Editorial Team added an answer Are you using the same binding on both? If so,… May 11, 2026 at 9:48 pm
  • Editorial Team
    Editorial Team added an answer A logical thread is a concept that dates back to… May 11, 2026 at 9:48 pm

Related Questions

I have two (UNIX) programs A and B that read and write from stdin/stdout.
I am having our company websites (x8 total 500,000per month) on a 1/3 share
I'm new to python programming. I have this problem: I have a list of
I have a shell script that saves the output of a command that is
What tool would you recommend to detect Java package cyclic dependencies , knowing that

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.