delattr

This is a bit more of technical than I usually post here, consider it a post by me, to me, so that if I have to do this again I will be able to remember how to do it.

I made some mods to my data models and wanted to delete an attribute from some of them. The Google App Engine documentation says to cycle through the existing entities in each model and use delattr to remove them, but it doesn’t give an example. Judging by the questions at Stack Overflow and other sites, a number of people had clearly struggled with getting this to work and various experts and gurus with a wide range of ability had attempted to explain how it was done, some with a great deal of complexity and recommendations to watch several hours of video presentations by Google staff at the IO conferences. So FWIW, this is my example of how I used delattr to modify the data models.

When I designed my new gift vendor system for my new range of products in SL and IW, I had this idea that I would write a fancy system that supported multiple merchants in multiple worlds and I  included a merchant name field in my data models. As work progressed I realised that I don’t actually want to get (back) into the business of selling vending machines because there are plenty of people doing that already. I want to make virtual pets and wildlife and countryside things and I just need a tool to record the sales so that I can offer updates and run a news service. I’d had the system running for a couple of weeks and there was already data in it when I decided to do this and my sales model looked like this:

class customer(db.Model):
 customer_name     = db.StringProperty(required=True, default="Jane Doe")
 customer_key      = db.StringProperty(required=True, default="NULL_KEY")
 customer_news     = db.StringProperty(required=True, default="Yes")
 world_name      = db.StringProperty(required=True, default="Second Life")
 joined_date = db.DateTimeProperty(required=True, auto_now_add=True)
 merchant_name = db.StringProperty(required=True, default="Jane Avatard")

I made two mods to the model.

  1. Make it an Expando class instead of a Model.
  2. Remove the merchant name attribute.
class customer(db.Expando):
 customer_name     = db.StringProperty(required=True, default="Jane Doe")
 customer_key      = db.StringProperty(required=True, default="NULL_KEY")
 customer_news     = db.StringProperty(required=True, default="Yes")
 world_name      = db.StringProperty(required=True, default="Second Life")
 joined_date = db.DateTimeProperty(required=True, auto_now_add=True)
 #merchant_name = db.StringProperty(required=True, default="Jane Avatard")

I then ran this code:

query = models.customer.all()
results = query.fetch(200)
for r in results:
 delattr(r, 'merchant_name')
 r.put()

I then set the class back to Model and Robert was one of my parent’s siblings.

If you were looking for a lot of technical explanation and reams of clever code, sorry, it’s just the quick and dirty that I used to get ‘er done.

About these ads
This entry was posted in Python/App Engine. Bookmark the permalink.

3 Responses to delattr

  1. joemar says:

    Thanks for posting this example. I will always keep going back to this when I need to update my models in my app engine app.

  2. John O'Brien says:

    Thank you for posting this, I concur with you, the GAE docs for this are less articulate than they could be.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s