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 3358418
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:45:01+00:00 2026-05-18T02:45:01+00:00

I need to check if the item is in datastore before I update it.

  • 0

I need to check if the item is in datastore before I update it.

I have 2 lists: UNIQUES = ["B","K","V"] and COUNTS = [5, 10, 3]

This is the model:

class Rep(db.Model):
    mAUTHOR = db.UserProperty(auto_current_user=True)
    mUNIQUE = db.TextProperty()
    mCOUNT = db.IntegerProperty()
    mDATE = db.DateTimeProperty(auto_now_add=True)

This function updates the database:

def write_to_db(S, C):
    REP = Rep(mUNIQUE=S, mCOUNT=C)
    db.put(REP)

Inspired by this page I try:

for i in range(len(UNIQUES)):            
    C_QUERY = Rep.all()
    C_QUERY.filter("mAUTHOR =", user)
    C_QUERY.filter("mUNIQUE =", UNIQUES[i])
    C_RESULT = C_QUERY.fetch(1)
    if C_RESULT:
        C = C_RESULT.mCOUNT + COUNTS[i]
        S = db.Text(UNIQUES[i])
        write_to_db(S,C)
    else:
        C = COUNTS[i]
        S = UNIQUES[i]
        write_to_db(S, C)

But the result is not what I expect. C_RESULT is always empty; and instead of updating, a new record is created. What am I doing wrong? Thanks!

EDIT3: Problem solved

As per David Underhill’s comment I updated the code and it now works.

        if C_RESULT:
            rep=C_RESULT[0]
            rep.mCOUNT+=COUNTS[i]
            rep.put()

EDIT2: Related question

How do I update this query and put it back with updated information?

C_QUERY = Rep.all()
C_QUERY.filter("mAUTHOR =", user)
C_QUERY.filter("mUNIQUE =", UNIQUES[i])
C_RESULT = C_QUERY.fetch(1)                

I want to change mCOUNT and then write it to datastore. How do I this? This looks like the exact same thing they do in this page but I could not make it work. Thanks for your help.

EDIT

I updated the code per David Underhill’s answer. That solved the problem (but the functionality is not right. I am not sure if that should be a different question).

    for i in range(len(UNIQUES)):                        
        C_QUERY = Rep.all()
        C_QUERY.filter("mAUTHOR =", user)
        C_QUERY.filter("mUNIQUE =", UNIQUES[i])
        C_RESULT = C_QUERY.fetch(1)                
        if C_RESULT:
            C = C_RESULT[0].mCOUNT + COUNTS[i]
            S = UNIQUES[i]
            write_to_db(S, C)
        else:
            C = COUNTS[i]
            S = UNIQUES[i]
            write_to_db(S, C)
  • 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-05-18T02:45:02+00:00Added an answer on May 18, 2026 at 2:45 am

    The problem is that the query is trying to filter mUNIQUE. However, you declared mUNIQUE as a db.TextProperty which is never indexed. As a result, you query never finds any results.

    Solution: Change mUNIQUE to a db.StringProperty (which is indexed by default).

    You should also consider updating Rep in a transaction – the current code may fail to property add COUNTS[i] if two requests try to update the same entity concurrently.

    Also, you can update S = db.Text(UNIQUES[i]) to simply S = UNIQUES[i].

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

Sidebar

Related Questions

I have html like this. <span class=gallery-open-item year-icon-Yes 2010> <a href=/year/none> </a> </span> I
I have a List through which I need to check if an item of
I need to check specific positions in an NSArray to see if they have
I need the way to check an item in ListView with choice mode SINGLE
I need to check, if for each item in @line_items if it is in
I need to check for duplicates. Currently I have items stored in sub folders
I have a list of links and need to check some data in it.
I have a windows application with an images folder. I need to check if
I am using Domain-Driven-Design have an entity called Menu, need to check if the
Need to check if a list contains an item with a property value of

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.