ValueError when deleting row object

21 views
Skip to first unread message

Maurice Waka

unread,
Dec 5, 2018, 9:16:15 AM12/5/18
to web2py-users
I'm trying to delete a certain row among others with this code:
@auth.requires_login()
def Daily_Calorie_Calculator():
    diary_date = None
    result_breakfast = []
    result_lunch = []
    result_dinner = []
    result_snack = []
    calories_sum = 0
    protein_sum = 0
    fat_sum = 0
    carbs_sum = 0
    
    #Deleting a specific row
    if request.vars.delete:
        #row = db(db.diary.author== auth.user.id).delete()#deletes all posted rows data unfortunately. I only want to delete a specific selected row

lunch.png

        row = db(db.diary.id==request.vars.delete).delete()# deletes all except under 'lunch'
        db.commit()
        redirect(URL('default', 'Daily_Calorie_Calculator', vars=dict(current_date=session.date)))
    
    #Working with a diary date
    from datetime import datetime

    if request.vars.current_date == None:
        diary_date = date.today()
    else:
        diary_date = request.vars.current_date
    session.date = date.today()#datetime.strptime('Jun 1 2005  1:33PM', '%b %d %Y %I:%M%p')

    #Getting goal values for current user
    goals = db(db.projected.created_by == auth.user_id).select().first()
    
    #Getting diary for current user
    rows = db(db.diary.created_by == auth.user_id)
    results = rows(db.diary.today == date.today()).select()#diary_date).select()
    for row in results:
        calories_sum += row.calories
        protein_sum += row.protein
        fat_sum += row.fat
        carbs_sum += row.carbs
        if row.meal == 'breakfast':
            result_breakfast.append(row)
        if row.meal == 'lunch':
            result_lunch.append(row)
        if row.meal == 'dinner':
            result_dinner.append(row)
        if row.meal == 'snack':
            result_snack.append(row)
    return dict(diary_date=diary_date, result_breakfast=result_breakfast, result_lunch=result_lunch, result_dinner=result_dinner, result_snack=result_snack, calories_sum=calories_sum, protein_sum=protein_sum, fat_sum=fat_sum, carbs_sum=carbs_sum, goals=goals)


db.define_table('diary',
                Field('author', 'reference auth_user', default=auth.user_id, writable=False, readable=False),
                Field('meal'),
                Field('today', 'date'),
                Field('food_item'),
                Field('calories', 'integer'),
                Field('carbs', 'integer'),
                Field('fat', 'integer'),
                Field('protein', 'integer'),
                Field('created_by', 'reference auth_user', default=auth.user_id, readable=False, writable=False))

When I want to delete any row under 'LUNCH', I get this error:
ValueError: Invalid objectid argument string. Requires an integer or base 16 value

Dave S

unread,
Dec 6, 2018, 3:42:13 AM12/6/18
to web2py-users


On Wednesday, December 5, 2018 at 6:16:15 AM UTC-8, Maurice Waka wrote:
I'm trying to delete a certain row among others with this code:
[...] 
want to delete a specific selected row

lunch.png

        row = db(db.diary.id==request.vars.delete).delete()# deletes all except under 'lunch'
     

 When you say "deletes all except under 'lunch'", does that mean that the delete button works if you're displaying [and punching delete for] Dinner entry, but for a Lunch entry, it gives the error below?
 
[...] 
When I want to delete any row under 'LUNCH', I get this error:
ValueError: Invalid objectid argument string. Requires an integer or base 16 value

What does your view look like?   Are you using javascript to do the buttons?  JQuery? 

Also, note that request.vars are usually strings and you may need to do a string to int conversion (atoi() for C users, but int() for Python).
(for dates, datetime.datetime.strptime() might be needed.)

/dps

 

Maurice Waka

unread,
Dec 6, 2018, 3:46:09 AM12/6/18
to web...@googlegroups.com
Hi Dave. 
Thanks for the interest.
Actually this line was the issue:
<td><form><button type="submit" name="delete" value="{{=item.id}} "style="background-color: #DA2128; 

instead of

<td><form><button type="submit" name="delete" value="{{=item.id}}"  style="background-color: #DA2128; 

Note the difference at the style=

--
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
---
You received this message because you are subscribed to a topic in the Google Groups "web2py-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/web2py/CktRiwZp5ow/unsubscribe.
To unsubscribe from this group and all its topics, send an email to web2py+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages