1
votes

I have one-to-many model scheme. All seems correct, data population works but linkitem_set.fetch fails with:

AttributeError: '_ReverseReferenceProperty' object has no attribute 'fetch'

There also one question here on SO with the same error but without solution. My code below:

class Project(db.Model):
   name = db.StringProperty()

class LinkItem(db.Model):
   url = db.StringProperty()
   project = db.ReferenceProperty(Project)

class Show(webapp2.RequestHandler):
   def get(self):
      links = Project.linkitem_set.fetch(100)
      self.response.headers['Content-Type'] = 'text/plain'
      for li in links:
         self.response.out.write(li + '/r/n')

class PopulateDb(webapp2.RequestHandler):
   def get(self):
      prj = Project(name = 'SomeProject 1')
      prj.put()
      for i in range(1000):
         rlink = random.betavariate(1, 2)
         link = LinkItem(url = str(rlink), project = prj)
         link.put()

I'm using Python 2.7 and tested this localy and hosted.

1

1 Answers

1
votes

I think that the problem is that the linkitem_set collection will only exist for an instance of Project, but you are trying to use it on the class itself.

Your code should look something more like this:

class Show(webapp2.RequestHandler):
   def get(self):
      prj_name = "" # Get a valid value, probably from URL params
      prj_to_show = Project.all().filter("name=", prj_name).get()
      if prj_to_show is not None:
          links = prj_to_show.linkitem_set.fetch(100)
          self.response.headers['Content-Type'] = 'text/plain'
          for li in links:
              self.response.out.write(li + '/r/n')