Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get a collection_name without having and instance of the referencing object?
    primarykey
    data
    text
    <p>I'm doing a simple program about customers, products and drafts.</p> <p>Since they are referenced to each other in some way, when I delete one entity of a kind, another entity of another kind might give an error.</p> <p>Here's what I have:</p> <p>-customer.py</p> <pre><code>class Customer(db.Model): """Defines the Customer entity or model.""" c_name = db.StringProperty(required=True) c_address = db.StringProperty() c_email = db.StringProperty() ... </code></pre> <p>-draft.py</p> <pre><code>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() ... </code></pre> <p>Ok, now what I want to do is check if a customer has any Draft referencing to him, before deleting him. This is the code I'm using:</p> <pre><code>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 &gt; 0...) customer.delete() else: do_something_else() </code></pre> <p>And now, it comes the problem. If I have a draft previously created with the selected customer on it, there's no problem at all, and it does what has to do. But if I haven't created any draft that references to that customer, when trying to delete him, it will show this error:</p> <pre><code>AttributeError: 'Customer' object has no attribute 'draft_set' </code></pre> <p>What am I doing wrong? Is it needed to always create a Draft including a Customer for him to have the collection_name property "available"?</p> <p><em><strong>EDIT</em></strong>: I found out what the error was. Since I have both classes in different .py files, it seems that GAE loads the entities into the datastore at the same moment as it "goes through" the file that contains that model. Therefore, if I'm executing the program, and never use or import that file, the datastore is not updated until then. Now what I'm doing is:</p> <pre><code>from draft.py import Draft </code></pre> <p>inside de "deleteCustomer()" function and it's finally working fine, but I get a horrible "warning not used" because of so.</p> <p>Is there any other way I can fix this?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload