Automatically repeat field text in another before record insertion

65 views
Skip to first unread message

dirman

unread,
Apr 18, 2018, 6:58:47 AM4/18/18
to web2py-users
How can i automatically repeat a field text into another field with hyphens to replace spaces before record insertion

I want to insert 'High Temperature In City' in the article_title and to be repeated in the article_link with hyphens

db.define_table('articles',
Field('article_title', 'string'),
Field('article_link', 'string'))


Example:

黄祥

unread,
Apr 18, 2018, 7:16:35 AM4/18/18
to web2py-users
perhaps you can use before_insert callback or onvalidate, then assign article_link with the article_title that already converted with function .replace('', '-')

best regards,
stifan

Anthony

unread,
Apr 18, 2018, 9:28:51 AM4/18/18
to web...@googlegroups.com
You probably want a computed field.

Field('article_link', compute=lambda r: r.article_title.replace(' ', '-'))

Alternatively, to avoid storing redundant data in the database, you can instead use a virtual field (that will add a little overhead at runtime, as the links will be created from the titles when you select rows from the database).

Anthony

dirman

unread,
Apr 18, 2018, 10:24:21 AM4/18/18
to web2py-users
I tried virtual field but can not use it as request args to select the records
They do not appear in the list table fields.

I got a ticket using the computed field example 

Field('article_link', lambda r: r.article_title.replace(' ', '-'))

or also

Field('article_link', lambda r: r['article_title'].replace(' ', '-'))


On Wednesday, April 18, 2018 at 1:28:51 PM UTC, Anthony wrote:
You probably want a computed field.

Field('article_link', lambda r: r.article_title.replace(' ', '-'))

dirman

unread,
Apr 18, 2018, 10:27:44 AM4/18/18
to web2py-users
Any example?.

what i want to do is to use article_link as request args to select the records instead of record.id

Anthony

unread,
Apr 18, 2018, 10:28:41 AM4/18/18
to web...@googlegroups.com
On Wednesday, April 18, 2018 at 10:24:21 AM UTC-4, dirman wrote:
I tried virtual field but can not use it as request args to select the records

Not sure what you mean by that. What does request args have to do with it? Can you show your code?
 
They do not appear in the list table fields.

I got a ticket using the computed field example 

Field('article_link', lambda r: r.article_title.replace(' ', '-'))

Please show your code and the full traceback.

Anthony

dirman

unread,
Apr 18, 2018, 10:57:11 AM4/18/18
to web2py-users
http://......./articles/high-temperature-in-city

def articles():
     news = db(db.articles.article_link==request.args[0]).select()[0]
     return(news=news)

Anthony

unread,
Apr 18, 2018, 2:12:40 PM4/18/18
to web2py-users
On Wednesday, April 18, 2018 at 10:57:11 AM UTC-4, dirman wrote:
http://......./articles/high-temperature-in-city

def articles():
     news = db(db.articles.article_link==request.args[0]).select()[0]
     return(news=news)

I see. Yes, if you need to use the values to make a database query, then it is probably best to store them in a database field, so use a computed field.

Please show the code that is generating an error along with the full traceback.

Anthony

dirman

unread,
Apr 18, 2018, 6:21:12 PM4/18/18
to web2py-users
Working after adding compute=lambda

Field('link', compute=lambda r: r.article_link.replace(' ', '-'))

However the table fields can not be updated using grid

Anthony

unread,
Apr 18, 2018, 8:33:46 PM4/18/18
to web2py-users
On Wednesday, April 18, 2018 at 6:21:12 PM UTC-4, dirman wrote:
Working after adding compute=lambda

Field('link', compute=lambda r: r.article_link.replace(' ', '-'))

Sorry, forgot the "compute=". I've updated my original response.
 
However the table fields can not be updated using grid

You can force the grid to include compute fields in forms by using the "formargs" argument to pass the "fields" argument to the SQLFORM generated by the grid:

grid = SQLFORM.grid(..., formargs=dict(fields=['article_title', 'article_link']))

When the grid generates the edit SQLFORM, it will pass the above "fields" argument to SQLFORM, which will override the default field selection (which excludes compute fields) and allow article_link to be edited. Note, if you leave 'article_link' blank in a create or edit form, it will be entered as an empty string in the database rather than computed. To avoid that with create forms, you can instead limit 'article_link' to appear only in edit forms by replacing "formargs" above with "editargs" (which only applies to edit forms).

Anthony

dirman

unread,
Apr 19, 2018, 3:43:02 PM4/19/18
to web2py-users
The page reloads the same SQLFORM edit form page with edited field not submited 

Anthony

unread,
Apr 19, 2018, 8:27:07 PM4/19/18
to web2py-users
On Thursday, April 19, 2018 at 3:43:02 PM UTC-4, dirman wrote:
The page reloads the same SQLFORM edit form page with edited field not submited

Works for me. Please show all your code and describe the exact workflow.

Anthony

dirman

unread,
Apr 20, 2018, 9:03:50 AM4/20/18
to web2py-users
db.define_table('articles',
Field('article_date', 'date'),
Field('article_title'),
Field('article_link', compute=lambda r: r.article_title.replace(' ', '-')),               
Field('image', 'upload'),      
Field('body', 'text'))

def index():
    return dict()

def articles()
     article = db(db.articles.article_link==request.args[0]).select()[0]
     return dict(article=article)

I want to use manage and grid functions to edited the articles table
so records should to editable after insertion.

@auth.requires_membership('admin')
def manage():
     grid=SQLFORM.grid(db.articles, editargs=dict(fields=['article_date','article_title','article_link','image','body']))
     return grid

@auth.requires_membership('admin')
def grid():
    response.view = 'generic.html' 
    tablename = request.args(0)
    if not tablename in db.tables: raise HTTP(403)
    grid = SQLFORM.smartgrid(db[tablename], args=[tablename], deletable=False, editable=True)
    return dict(grid=grid)

With the compute function on the table the SQLFORM.grid or smartgrid records can not be updated.

Anthony

unread,
Apr 20, 2018, 11:29:22 AM4/20/18
to web2py-users
Sorry, using your exact code, I cannot reproduce the problem. I am able to edit records (including the article_link field) using the grid.

Anthony
Reply all
Reply to author
Forward
0 new messages