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

  • SEARCH
  • Home
  • 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 4238858
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:57:33+00:00 2026-05-21T02:57:33+00:00

In this scenario, company.py contains: from django.utils import simplejson class Company(db.Model): name=db.StringProperty() address=db.StringProperty() def

  • 0

In this scenario, company.py contains:

from django.utils import simplejson

class Company(db.Model):
    name=db.StringProperty()
    address=db.StringProperty()

    def to_JSON():
        d = dict([(p, unicode(getattr(self, p))) for p in self.properties()])
        return simplejson.dumps(d)

office.py contains:

from company import Company

class Office(db.Model):
    name = db.StringProperty()
    company = db.ReferenceProperty(Company, collection_name='offices')

module 3 contains:

from company import Company

class Region(db.Model):
    name = db.StringProperty()
    company = db.ReferenceProperty(Company, collection_name='regions')

This is a simplified example of what I need to do, but should capture it pretty well… I need Company to be able to express itself as a JSON string, and it does not know about the other classes (Office and Region) that reference it. This works for the normal properties:

c = Company(name='A Company',address='123 Main St, New York, NY 12345')
c.put()

dallas = Office(name='Dallas',company=c)
dallas.put()

ny = Office(name='New York',company=c)
ny.put()

northeast = Region(name='NorthEast',company=c)
northeast.put()

southwest = Region(name='SouthWest',company=c)
southwest.put()

logging.info('Company as JSON: %s' % c.to_JSON())

And what I get for output is:

{"name": "A Company", "address": "123 Main St, New York, NY 12345"}

So properties() includes the properties of the entity as expected, but I also want to include the back-references from the ReferenceProperty attributes of the associated objects. Essentially what I’m looking for is just a list of the back-reference (collection_name) names, since I know they represent queries, but I can’t find any way to enumerate the back-reference names themselves.

Remember this code isn’t actually indicative of the real code, it’s greatly simplified. Now this works:

offices = getattr(company, 'offices')

So what I was expecting to see was this:

{"name": "A Company", "address": "123 Main St, New York, NY 12345", 
 "offices": "<db.Query object at 0x110e992d0>", 
 "regions": "<db.Query object at 0x110e99300>"
}

I don’t actually need the back-references “offices” and “regions” to be included in the properties() enumerations, but they’re checked for uniqueness somehow (I couldn’t, for example, also have a StringProperty in Company named ‘offices’) so there must be a way to enumerate them. Anyone know of a way?

  • 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-21T02:57:34+00:00Added an answer on May 21, 2026 at 2:57 am

    The check for uniqueness is simply against the class dict (that is, the internal object that provides a mapping between names and values for the class object). You can iterate over these by examining Company.__dict__. This includes everything, though – all the properties, attributes and methods of the built in Python object, likewise for db.Model and any other parent classes.

    Your best option would be to iterate over the class dict, checking each item to see if it’s an instance of _ReverseReferenceProperty:

    for propname, prop in Company.__dict__.iteritems():
      if isinstance(prop, db._ReverseReferenceProperty):
        # Do something with prop, such as getting a query with getattr(a_company, propname).
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In this scenario, I have 2 or more models: class Store(models.Model): name = models.CharField(max_length
Is this scenario even possible? class Base { int someBaseMemer; }; template<class T> class
we have this scenario: A server which contains needed data and client component which
...any change? The scenario is this: For our company we develop a standard how
The scenario is this: My company has 2000 customers, and we need to send
I'm wondering what the best branching strategy is for this scenario. My company is
Take this scenario: To view the company counts of all items, I can write:
This scenario comes up often, now that I use LinkToSql and the classes it
Consider this scenario: I've an XML file called person.xml with the following data in
Consider this scenario. I have my own website, that I use as my identifier,

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.