I've done this with a table with a year as its id:

idb.define_table(
    'next_id',
    Field('year', 'id'),
    Field('next', 'integer'),
)


def get_next_id(year): 
    record = idb.next_id(year)
    if not record:
        # Auto add new years
        idb.next_id.insert(year=year, next=1)
        record = idb.next_id(year)
   
    # Return the id while incrementing the seed for the next call
    id = record.next
    record.update_record(next=id + 1)
    return id

Probably not bullet-proof against collisions if traffic is extremely high, 
but it works for the 10000s of uses per year I use.

On Saturday, 6 January 2018 17:16:31 UTC, Andrea Santini wrote:
>
>  need to create an auto increamet Field that reset to 1 every Year.
>
> db.define_table('protocol',Field('n_protocol'),Field('year_protocol',type='datetime',
>  writable = False, readable = False, default=request.now, 
> requires=(IS_DATETIME(timezone=pytz.timezone("Europe/Gibraltar"),format=('%Y'))))
>
> The field 'n_protocol' must start to 1 auto increment and return to 1 next 
> year.
>
> Why the date is correct when i insert data but is not correct when i see 
> the data stored in db the date is wrong?
>
> I also tried this:
>
>
>
> ultimo_protocollo= db(db.protocollo.data_protocollo).select().last()
> ieri = ultimo_protocollo.year
> data = datetime.datetime
> oggi = data.year
> db.protocollo.n_protocollo = Field.Virtual('n_protocollo', lambda 
> n_protocollo: (n_protocollo ++ 1) if (ieri == oggi)  else 
> (db.protocollo.n_protocollo =="1"))
>
>
> but i recive error.
>
> Can you please help me?
>
> Thank you guys
>
>

-- 
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 the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to web2py+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to