如何枚举实体的反向引用

发布于 2024-10-30 02:09:23 字数 1939 浏览 6 评论 0原文

在这种情况下,company.py包含:

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包含:

from company import Company

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

模块3包含:

from company import Company

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

这是我需要做的事情的简化示例,但应该很好地捕获它......我需要Company能够将自己表达为JSON 字符串,并且它不知道引用它的其他类(Office 和 Region)。这适用于普通属性:

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())

我得到的输出是:

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

因此,properties() 按预期包含实体的属性,但我还想包含来自关联对象的 ReferenceProperty 属性的反向引用。本质上,我正在寻找的只是反向引用(collection_name)名称的列表,因为我知道它们代表查询,但我找不到任何方法来枚举反向引用名称本身。

请记住,此代码实际上并不表示真正的代码,它已被大大简化。现在这有效了:

offices = getattr(company, 'offices')

所以我期望看到的是这样的:

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

我实际上不需要将反向引用“offices”和“regions”包含在properties()枚举中,但它们以某种方式检查唯一性(例如,我不能在名为“offices”的公司中也有一个 StringProperty),因此必须有一种方法来枚举它们。有谁知道有什么办法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁〃客ぐ 2024-11-06 02:09:23

唯一性检查只是针对类字典(即提供类对象的名称和值之间的映射的内部对象)。您可以通过检查Company.__dict__来迭代这些。不过,这包括所有内容 - 内置 Python object 的所有属性、属性和方法,同样适用于 db.Model 和任何其他父类。

最好的选择是迭代类字典,检查每个项目以查看它是否是 _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).

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).
终陌 2024-11-06 02:09:23

假设您没有为 collection_name 赋值,只需将其保留为 Modelname_set。然后您可以dir(model),选择以_set结尾的属性,这些就是反向引用。或者你可以制定一个约定来命名它们,例如。 groups_refs,并搜索以 _refs 结尾的属性。

Let's suppose you don't assign values to collection_name, just leave it as Modelname_set. Then you can dir(model), choose the attributes ending in _set, and those are the backreferences. Or you can make a convention to name them eg. groups_refs, and search for attributes ending in _refs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文