[google-appengine] Re: How does The Python datastore select the class for an entity (or: swapping classes through the datastore)?

2009-10-23 Thread Rafe

  Yes, you can rename the kind of a class.  Just make sure that any
non-class bound GQL you have written are adjusted.  In other words,
where you have used db.gql vs. db.Model.gql.

On Oct 22, 10:52 am, gae123 pa...@gae123.com wrote:
 NickandRafe,

 this is great to know. Am I correct to assume that this gives us an
 easy way to rename classes? Let's say I had this:

 class Foo(db.Model):
 

 and want to rename it to Bar in my code but I have an app already
 running with lots of data. I can just say:

 class Bar(db.Model):
     �...@classmethod
      def kind(cls):
           return 'Foo'

      ...

 Thanks
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Google App Engine group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How does The Python datastore select the class for an entity (or: swapping classes through the datastore)?

2009-10-22 Thread Tonny

Thank you or the reply. This is great news - if I understood it
correctly it means moving model classes to new modules is possible
(redesign/cleanups).

What i don't understand is how GAE searches for the actual class,
given that it only has an unqualified name. Does it iterate all
modules in the app or something else?

Cheers
Tonny

On 21 Okt., 16:37, Nick Johnson (Google) nick.john...@google.com
wrote:
 Hi Tonny,
 The SDK maintains a mapping between kind name and class. The kind name is
 determined by calling kind() on your class, and defaults to the
 (unqualified) name of the class. Because you have two classes with the same
 name in different modules, they both have the same kind name.

 The solution is to override kind() on one or both classes:

 class MyModel(db.Model):
   @classmethod
   def kind(self):
     return 'some_other_value'

 -Nick Johnson





 On Wed, Oct 21, 2009 at 11:00 AM, Tonny mezz...@gmail.com wrote:

  I ran into a fuzzy little thing today. I have to classes with the same
  name in different modules and both inherits from the Model class
  (let's call them x.A and y.A). I've created an instance of x.A (let's
  call it a1) and put it to the datastore, i then referenced the
  instance from an a.B entity (let's say b1). Now later fetching b1 and
  accessing the a1 through the reference property on b1 i get an object
  of the class y.A. How did this happen?

  Maybe i should put this to code:

  module x
  class A(db.Model):
   name = db.StringProperty()
  class B(db.Model):
   name = db.StringProperty
   other = db.ReferenceProperty(reference_class=A)

  module y
  class A(db.Model):
   name = db.StringProperty()

  # now let's create som entities
  a1 = x.A(name='Test')
  a1.put()
  b1 = x.B(name = 'Some Name', other=a1)
  b1.put()
  b2 = x.B.all().filter('name =', 'Some Name').get()
  a2 = b2.other # now this will be an instance of y.A, even though we've
  created no such instance or worked with module y in module a or the
  end code snippet.

  Magic, a bug, a feature?

  Regards
  Tonny

 --
 Nick Johnson, Developer Programs Engineer, App Engine
 Google Ireland Ltd. :: Registered in Dublin, Ireland, Registration Number:
 368047
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Google App Engine group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How does The Python datastore select the class for an entity (or: swapping classes through the datastore)?

2009-10-22 Thread OvermindDL1

On Thu, Oct 22, 2009 at 12:43 AM, Tonny mezz...@gmail.com wrote:

 Thank you or the reply. This is great news - if I understood it
 correctly it means moving model classes to new modules is possible
 (redesign/cleanups).

 What i don't understand is how GAE searches for the actual class,
 given that it only has an unqualified name. Does it iterate all
 modules in the app or something else?

I doubt it searches for anything.  Most likely it has a meta-class or
something in the base class that inits everything registers the based
class (view __class__.__name__ or whatever it was) at initial start-up
runtime.

Remember, Python has almost LISP-like power, it can do a lot of things
at initial run-time.  :)

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Google App Engine group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How does The Python datastore select the class for an entity (or: swapping classes through the datastore)?

2009-10-22 Thread Rafe

 So, what's happening here is that when a class gets registered there
is a mapping from kind to class just as Nick said.  This mapping will
be the last class that you created with that unqualified name.  In
this case, your last mapping is y.A.  From that point on, every time
you load an entity from datastore of kind A, it will create an
instance of y.A even if it was originally created as x.A.

  App Engine deliberately doe not try to prevent developers from
defining more than one class with the same name more than once.
Developers need to be careful about keeping that space unambiguous.

  You can use the kind method to store fully qualified instance names
which will allow you to use classes with the same name in different
modules.

On Oct 21, 11:43 pm, Tonny mezz...@gmail.com wrote:
 Thank you or the reply. This is great news - if I understood it
 correctly it means moving model classes to new modules is possible
 (redesign/cleanups).

 What i don't understand is how GAE searches for the actual class,
 given that it only has an unqualified name. Does it iterate all
 modules in the app or something else?

 Cheers
 Tonny

 On 21 Okt., 16:37, Nick Johnson (Google) nick.john...@google.com
 wrote:



  Hi Tonny,
  The SDK maintains a mapping between kind name and class. The kind name is
  determined by calling kind() on your class, and defaults to the
  (unqualified) name of the class. Because you have two classes with the same
  name in different modules, they both have the same kind name.

  The solution is to override kind() on one or both classes:

  class MyModel(db.Model):
    @classmethod
    def kind(self):
      return 'some_other_value'

  -Nick Johnson

  On Wed, Oct 21, 2009 at 11:00 AM, Tonny mezz...@gmail.com wrote:

   I ran into a fuzzy little thing today. I have to classes with the same
   name in different modules and both inherits from the Model class
   (let's call them x.A and y.A). I've created an instance of x.A (let's
   call it a1) and put it to the datastore, i then referenced the
   instance from an a.B entity (let's say b1). Now later fetching b1 and
   accessing the a1 through the reference property on b1 i get an object
   of the class y.A. How did this happen?

   Maybe i should put this to code:

   module x
   class A(db.Model):
    name = db.StringProperty()
   class B(db.Model):
    name = db.StringProperty
    other = db.ReferenceProperty(reference_class=A)

   module y
   class A(db.Model):
    name = db.StringProperty()

   # now let's create som entities
   a1 = x.A(name='Test')
   a1.put()
   b1 = x.B(name = 'Some Name', other=a1)
   b1.put()
   b2 = x.B.all().filter('name =', 'Some Name').get()
   a2 = b2.other # now this will be an instance of y.A, even though we've
   created no such instance or worked with module y in module a or the
   end code snippet.

   Magic, a bug, a feature?

   Regards
   Tonny

  --
  Nick Johnson, Developer Programs Engineer, App Engine
  Google Ireland Ltd. :: Registered in Dublin, Ireland, Registration Number:
  368047
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Google App Engine group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How does The Python datastore select the class for an entity (or: swapping classes through the datastore)?

2009-10-22 Thread gae123


Nick and Rafe,

this is great to know. Am I correct to assume that this gives us an
easy way to rename classes? Let's say I had this:

class Foo(db.Model):


and want to rename it to Bar in my code but I have an app already
running with lots of data. I can just say:

class Bar(db.Model):
 @classmethod
 def kind(cls):
  return 'Foo'

 ...

Thanks


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Google App Engine group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---



[google-appengine] Re: How does The Python datastore select the class for an entity (or: swapping classes through the datastore)?

2009-10-21 Thread Nick Johnson (Google)
Hi Tonny,
The SDK maintains a mapping between kind name and class. The kind name is
determined by calling kind() on your class, and defaults to the
(unqualified) name of the class. Because you have two classes with the same
name in different modules, they both have the same kind name.

The solution is to override kind() on one or both classes:

class MyModel(db.Model):
  @classmethod
  def kind(self):
return 'some_other_value'


-Nick Johnson

On Wed, Oct 21, 2009 at 11:00 AM, Tonny mezz...@gmail.com wrote:


 I ran into a fuzzy little thing today. I have to classes with the same
 name in different modules and both inherits from the Model class
 (let's call them x.A and y.A). I've created an instance of x.A (let's
 call it a1) and put it to the datastore, i then referenced the
 instance from an a.B entity (let's say b1). Now later fetching b1 and
 accessing the a1 through the reference property on b1 i get an object
 of the class y.A. How did this happen?

 Maybe i should put this to code:

 module x
 class A(db.Model):
  name = db.StringProperty()
 class B(db.Model):
  name = db.StringProperty
  other = db.ReferenceProperty(reference_class=A)

 module y
 class A(db.Model):
  name = db.StringProperty()

 # now let's create som entities
 a1 = x.A(name='Test')
 a1.put()
 b1 = x.B(name = 'Some Name', other=a1)
 b1.put()
 b2 = x.B.all().filter('name =', 'Some Name').get()
 a2 = b2.other # now this will be an instance of y.A, even though we've
 created no such instance or worked with module y in module a or the
 end code snippet.

 Magic, a bug, a feature?

 Regards
 Tonny
 



-- 
Nick Johnson, Developer Programs Engineer, App Engine
Google Ireland Ltd. :: Registered in Dublin, Ireland, Registration Number:
368047

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Google App Engine group.
To post to this group, send email to google-appengine@googlegroups.com
To unsubscribe from this group, send email to 
google-appengine+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en
-~--~~~~--~~--~--~---