Are you trying to auto-complete a reference field, and you want to be able 
to add new records to the referenced table? The built-in autocomplete 
widget doesn't handle that out of the box, but upon submission, you could 
check the request variables, and if the submitted value doesn't belong to 
an existing record in the referenced table, you could do an insert:

Model:

db.define_table('category', Field(name))

db.define_table('article',
    Field('title'),
    Field('body', 'text'),
    Field('category', db.category,
          widget=SQLFORM.widgets.autocomplete(request, db.category.name,id_field
=db.category.id)))

Controller:

def myfunction():
    if request.post_vars._autocomplete_name_aux and not request.post_vars.
category:
        request.post_vars.category = db.category.insert(name=request.
post_vars._autocomplete_name_aux)
    return dict(form=SQLFORM(db.article).process())

When you use the autocomplete widget on a reference field, it submits the 
display value in a special field named 
_autocomplete_[display_field_name]_aux, and the actual field value is the 
record ID of the referenced record associated with the display value. 
However, if you enter and submit a display value that does not have an 
associated record in the referenced table, the reference field value will 
simply be empty (though the _autocomplete_..._aux field will contain the 
submitted display value).

For example, when submitting a new article, suppose you enter "programming" 
in the category input field, but "programming" does not yet exist in the 
db.category table. In that case, request.post_vars.category will be empty, 
and request.post_vars._autocomplete_name_aux will contain the word 
"programming". In that case, the above code inserts "programming" into the 
db.category table and stores the new record ID in 
request.post_vars.category. When the form is then processed, the new 
db.article record will be created, with the new db.category record ID 
stored in the db.article.category field.

Perhaps the autocomplete widget should handle this automatically (with an 
optional setting).

Anthony

On Thursday, May 10, 2012 12:44:51 PM UTC-4, gfdgdgfdg wrote:
>
> Hello everyone,
>
> I'm looking for a way to get an auto-complete field combined with adding 
> non-existing item to the DB. I've seen a website which
> uses this functionality (demo: http://www.tellmehow.nl/video.html )
>
> I have already tried SELECT_OR_ADD_OPTION but i don't like drop-downs...
>
> Thanks in advance!
> Remco

Reply via email to