Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8835243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T09:12:56+00:00 2026-06-14T09:12:56+00:00

Our team is relatively new to AppEngine and we’re still learning the ropes. We’re

  • 0

Our team is relatively new to AppEngine and we’re still learning the ropes. We’re using plain ndb.Model models (i.e. nothing fancy) and webapp2 handlers. The app is UI-less since it’s just a restful API.

So we have a model defined as such:

from google.appengine.ext import ndb
class Pasta(ndb.Model):
 type = ndb.StringProperty(indexed=True)
 name = ndb.StringProperty()
 contents = ndb.JsonProperty()
 modified_date = ndb.DateTimeProperty(auto_now=True)
 added_date = ndb.DateTimeProperty(auto_now_add=True)

And the use case is that we would like to add a record if one doesn’t exist and if it does exist, return it. We could query for it in the handler and even create one there and use the pasta.put() instance method to create a new one. However, we think that data-bound code belongs in the model, not the handler, but, if we’re not mistaken, in the model context we need to use the get_or_insert() which requires we explicitly declare a Key, correct?

Any suggestions how to handle this sort of logic in the model? TIA.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T09:12:58+00:00Added an answer on June 14, 2026 at 9:12 am

    I don’t know why you think the decision on whether the code goes in the model or the handler determines the method you use to do the query. You can use get_or_insert in either place, but it does mean that you need to know the key of the entity you want. And, you can do queries in either place.

    If you do want to keep all your data interaction within the model, one possible pattern is to define a classmethod on the model that does the query and returns either the existing instance or a new one. Something like:

    class Pasta(ndb.Model): 
        ...
        @classmethod
        def get_or_create(cls, type=None, name=None):
            query = cls.query(cls.type=type, cls.name=name)
            existing = query.get()
            if existing:
                return existing
            else:
                new_item = cls(type=type, name=name)
                new_item.put()
                return new_item
    

    (Note I haven’t actually used ndb, but this is more or less right.) You’ll want to make sure this method is called within a transaction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our team is pretty new with testing. We are using scrum for the first
Our team is growing, but we're also growing specialized. We've already been using ticketing/bug
Our team is writing its first JSF 2.0 application since using Stripes for many
Our team is willing to unit-test a new code written under a running project
Our team is still in a love / hate relationship with it. I am
Our team is working on a new application that we started with Rails 3.1
I am setting up our (relatively small) programming team to work with source control.
Our relatively small development team is getting a bit sick of Dreamweaver. The only
Our team is currently using some ported code from an old architecture to a
Our team started using mercurial about a month ago and it was a rough

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.