Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在没有引用对象的和实例的情况下获取集合名称?_Python_Google App Engine_Collections_Referenceproperty - Fatal编程技术网

Python 如何在没有引用对象的和实例的情况下获取集合名称?

Python 如何在没有引用对象的和实例的情况下获取集合名称?,python,google-app-engine,collections,referenceproperty,Python,Google App Engine,Collections,Referenceproperty,我正在做一个关于客户、产品和草稿的简单程序 因为它们以某种方式相互引用,所以当我删除一种类型的实体时,另一种类型的实体可能会给出错误 以下是我所拥有的: -customer.py class Customer(db.Model): """Defines the Customer entity or model.""" c_name = db.StringProperty(required=True) c_address = db.StringProperty

我正在做一个关于客户、产品和草稿的简单程序

因为它们以某种方式相互引用,所以当我删除一种类型的实体时,另一种类型的实体可能会给出错误

以下是我所拥有的:

-customer.py

class Customer(db.Model):
    """Defines the Customer entity or model."""
    c_name      = db.StringProperty(required=True)
    c_address   = db.StringProperty()
    c_email     = db.StringProperty() ...
-草稿.py

class Draft(db.Model):
    """Defines the draft entity or model."""
    d_customer      = db.ReferenceProperty( customer.Customer,
                                        collection_name='draft_set')
    d_address       = db.StringProperty()
    d_country       = db.StringProperty() ...
好的,现在我要做的是在删除客户之前,检查客户是否有任何草稿引用他。 这是我正在使用的代码:

def deleteCustomer(self, customer_key):
    '''Deletes an existing Customer'''

    # Get the customer by its key
    customer = Customer.get(customer_key)

    if customer.draft_set: # (or customer.draft_set.count > 0...)
        customer.delete()

    else:
        do_something_else()
现在,问题来了。 如果我有一个以前与选定客户一起创建的草稿,那么根本就没有问题,它会做必须做的事情。但是,如果我没有创建任何引用该客户的草稿,在尝试删除该客户时,它将显示以下错误:

AttributeError: 'Customer' object has no attribute 'draft_set'
我做错了什么?是否需要始终为其创建包含客户的草稿,以使collection_name属性“可用”

编辑:我发现了错误所在。 由于这两个类在不同的.py文件中,GAE似乎在“遍历”包含该模型的文件的同时将实体加载到数据存储中。 因此,如果我正在执行该程序,并且从未使用或导入该文件,则数据存储在此之前不会更新。 现在我要做的是:

from draft.py import Draft
在de“deleteCustomer()”函数中,它终于可以正常工作了,但我得到了一个可怕的“警告未使用”,因为这样


是否有其他方法可以解决此问题?

集合名称属性是一个查询,因此它应该始终可用

您可能缺少的是
reference\u class
参数(请检查)

以下方面应起作用:

if customer.draft_set.count():
    customer.delete()

请注意,
customer.draft\u set
将始终返回true,因为它是生成的查询对象,因此必须使用
count()
集合名称属性进行查询,因此它应该始终可用

您可能缺少的是
reference\u class
参数(请检查)

以下方面应起作用:

if customer.draft_set.count():
    customer.delete()

请注意,
customer.draft\u set
将始终返回true,因为它是生成的查询对象,因此必须使用
count()
有两种可能的解决方案:

  • 丑陋,糟糕的一个:正如我编辑的问题中所描述的

  • 最佳做法:将所有模型放在一个文件(例如models.py)中,如下所示:

    class Customer(db.Model):
    
        """Defines the Customer entity or model."""
    
        c_name      = db.StringProperty(required=True)
        c_address   = db.StringProperty()
        c_email     = db.StringProperty() ...
    
    class Draft(db.Model):
    
        """Defines the draft entity or model."""
        d_customer      = db.ReferenceProperty( customer.Customer,
                                    collection_name='draft_set')
        d_address       = db.StringProperty()
        d_country       = db.StringProperty() ...
    

  • 轻松点

    有两种可能的解决方案:

  • 丑陋,糟糕的一个:正如我编辑的问题中所描述的

  • 最佳做法:将所有模型放在一个文件(例如models.py)中,如下所示:

    class Customer(db.Model):
    
        """Defines the Customer entity or model."""
    
        c_name      = db.StringProperty(required=True)
        c_address   = db.StringProperty()
        c_email     = db.StringProperty() ...
    
    class Draft(db.Model):
    
        """Defines the draft entity or model."""
        d_customer      = db.ReferenceProperty( customer.Customer,
                                    collection_name='draft_set')
        d_address       = db.StringProperty()
        d_country       = db.StringProperty() ...
    
  • 轻松点