i figured it out..

The issue is the sequence after the result.put() followed by
gParent.xxxx
The childname result needs to be updated.  The original code was
creating a new entry every time for parentperson() instead of reusing
the original .get().

The code below contains some goofy if nests, but it was a result of
testing without care of structure.  It's only a proof (if only to
myself) of how it works.

Here is a working piece of code.

class person(db.Model):
    firstName = db.StringProperty()
    lastName = db.StringProperty()
    rid = db.IntegerProperty()

class parentperson(db.Model):
    childname = db.ReferenceProperty(person)
    persontype = db.StringProperty()
    updateTime = db.DateTimeProperty()


class ShowIssues(webapp.RequestHandler):
    def get(self):
        personLookup = person()
        query = personLookup.gql("WHERE rid = :1", int(1234))
        result = query.get()

        if result:
            tParent = db.Query(parentperson)
            tParent.filter('persontype', 'Daughter')
            tParent.filter('childname',result.key())
            tResult = tParent.get()
            if tResult:
                pOutput = """
                    Child - Type: %s
                    <br />
                    Key Name: %s
                    <br />
                    Parent First Name is: %s
                    <br />
                    Parent Last Name is: %s
                    <br />
                    Last Update Time was: %s
                    <hr />
                """ %(tResult.persontype, str(tResult.childname),
tResult.childname.firstName, tResult.childname.lastName, str
(tResult.updateTime))
                self.response.out.write(pOutput)
        else:
            tResult = None

        if result:
            output = """
                    First Name is %s
                            <br />
                            Last Name is %s
                            <br />
                            Key is %s
                            """ %(result.firstName, result.lastName,
str(result.key()))
            self.response.out.write(output)
            result.lastName = "New Smith"
            result.put()

            if tResult:
                # update the existing child
                # datetime is used for testing a unique data
insertion
                tResult.updateTime = datetime.now()
                tResult.put()
            else:
                # its a new child
                gParent = parentperson()
                gParent.childname = result.key()
                gParent.persontype = "Daughter"
                gParent.updateTime = datetime.now()
                gParent.put()

        else:
            result = person()
            result.firstName = "John"
            result.lastName = "Smith"
            result.rid = int(1234)
            result.put()

        qi = parentperson.gql("LIMIT 1000")
        qresults = qi.fetch(1000)
        self.response.out.write("<hr />")
        for qires in qresults:
            self.response.out.write(str(qires.childname) + " " + str
(qires.persontype) +"<br />")
            #qires.delete()

        qt = person.gql("LIMIT 1000")
        qtresults = qt.fetch(1000)
        self.response.out.write("<hr />")
        for qtires in qtresults:
            self.response.out.write(str(qtires.firstName) + " " + str
(qtires.lastName) +"<br />")
            #qtires.delete()

On Mar 24, 4:00 pm, Steve W <wetmon...@gmail.com> wrote:
> I happened to catch that today while the post was being moderated.
>
> I updated the code to include that (person) and it still doesn't work.
> It seems as though childname=result ( and childname=result.key())
> returns an in memory object, and not really the key id.
>
> doing a str(result.key()) after the put() returns a string similar too
> <__main__.person object at 0x036C7210>
> the memory address changes each time.
>
> I am considering storing the str(result.key()) in a
> StringPropertyType, but I shouldn't have too. =)
>
> Thanks for responding so quick.
>
> On Mar 24, 3:56 pm, Paul Roy <paul.ro...@gmail.com> wrote:
>
> > your ReferenceProperty should be declared as:
>
> > childname = db.ReferenceProperty(person)
>
> > and then set as:
>
> > childname = result
>
> > i think :)
>
> > Sent from my iPhone
>
> > On 09-03-24, at 09:51, Steve W <wetmon...@gmail.com> wrote:
>
> > > I am struggling with understanding how to use keys to enforce
> > > reference between two tables.
>
> > > I need to access the same set of data from table one, and table two.
> > > Then change data within both tables.  The data manipulation works in
> > > the "person" table, but it looses the connection to itself in the
> > > "parentperson" table.
>
> > > The code below demonstrates the issue.  Reload the application four+
> > > times and you can see the 'parentperson' table/model fills up with
> > > entries.  The key I get from 'person' is different every time, but
> > > there is only one row in 'person'.
>
> > > Completely confused as to what I am missing on this one..
>
> > > class person(db.Model):
> > >    firstName = db.StringProperty()
> > >    lastName = db.StringProperty()
> > >    rid = db.IntegerProperty()
>
> > > class parentperson(db.Model):
> > >    childname = db.ReferenceProperty()
> > >    persontype = db.StringProperty()
> > >    updateTime = db.DateTimeProperty()
>
> > > class ShowIssues(webapp.RequestHandler):
> > >    def get(self):
> > >        personLookup = person()
> > >        query = personLookup.gql("WHERE rid = :1", int(1234))
> > >        result = query.get()
>
> > >        if result:
> > >            tParent = db.Query(parentperson)
> > >            tParent.filter('persontype', "Daughter")
> > >            tParent.filter('childname',result.key())
> > >            tResult = tParent.get()
> > >        else:
> > >            tResult = None
>
> > >        if tResult:
> > >            pOutput = """
> > >                Child - Type: %s
> > >                <br />
> > >                Key Name: %s
> > >                <br />
> > >                Parent First Name is: %s
> > >                <br />
> > >                Parent Last Name is: %s
> > >                <br />
> > >                Last Update Time was: %s
> > >                <hr />
> > >            """ %(tResult.persontype, str(tResult.childname),
> > > tResult.childname.firstName, tResult.childname.lastName, str
> > > (tResult.updateTime))
> > >            self.response.out.write(pOutput)
> > >        if result:
> > >            output = """
> > >                    First Name is %s
> > >                            <br />
> > >                            Last Name is %s
> > >                            <br />
> > >                            Key is %s
> > >                            """ %(result.firstName, result.lastName,
> > > str(result.key()))
> > >            self.response.out.write(output)
> > >            result.lastName = "New Smith"
> > >            result.put()
> > >            gParent = parentperson()
> > >            gParent.childname = result.key()
> > >            gParent.persontype = "Daughter"
> > >            gParent.updateTime = datetime.now()
> > >            gParent.put()
>
> > >        else:
> > >            result = person()
> > >            result.firstName = "John"
> > >            result.lastName = "Smith"
> > >            result.rid = int(1234)
> > >            result.put()
>
> > >        qi = parentperson.gql("LIMIT 1000")
> > >        qresults = qi.fetch(1000)
> > >        self.response.out.write("<hr />")
> > >        for qires in qresults:
> > >            self.response.out.write(str(qires.childname) + " " + str
> > > (qires.persontype) +"<br />")
> > >            #qires.delete()
>
> > >        qt = person.gql("LIMIT 1000")
> > >        qtresults = qt.fetch(1000)
> > >        self.response.out.write("<hr />")
> > >        for qtires in qtresults:
> > >            self.response.out.write(str(qtires.firstName) + " " + str
> > > (qtires.lastName) +"<br />")
> > >            #qtires.delete()
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to