what is the best approach to have auto generate document number or codification (e.g. product code)?

28 views
Skip to first unread message

黄祥

unread,
May 28, 2015, 6:46:39 AM5/28/15
to web...@googlegroups.com
hi,

what is the best approach to have auto generate document number or codification (e.g. product code) using web2py?
what i've done right now is something like :
document number
controllers/purchase_transaction.py

if 'purchase_order' in request.function :
    # purchase_order_header
    id_max_purchase_order_header = db.purchase_order_header.id.max()
    maxID_purchase_order_header = db(db.purchase_order_header).select(id_max_purchase_order_header).first()[id_max_purchase_order_header]
    purchase_order_no_id = int(maxID_purchase_order_header) + 1 if maxID_purchase_order_header else 1
    supplier_for_purchase_order_no = str(request.vars.supplier)
    purchase_order_no = str(purchase_order_no_id)+'/PO/'+request.now.strftime('%y%m%d')+'/'+supplier_for_purchase_order_no

    db.purchase_order_header.purchase_order_no.default = purchase_order_no

    # purchase_order_detail
    db.purchase_order_detail.purchase_order_no.default = purchase_order_no_id

codification (e.g. product code)
controllers/master.py

def __onvalidation_product(form):
    row = db(db.model.id == form.vars.model).select().first()
    form.vars.brand = row.brand

    count_product_brand = db(db.product.brand == row.brand).count()
    next_product_count = int(count_product_brand) + 1 if count_product_brand else 1
    product_no = str(row.brand.code)+str(next_product_count)
    db.product.product_no.default = product_no

but anthony mention in another discussion that this kind of approach is not the best approach, any idea or suggestion what is the best approach to handle this kind of situation?

discussion ref:
https://groups.google.com/forum/#!topic/web2py/CGFkel06jcw

thanks and best regards,
stifan

黄祥

unread,
Jun 30, 2015, 10:09:49 PM6/30/15
to web...@googlegroups.com
found it on the book (use unique = True)
http://web2py.com/books/default/chapter/29/07/forms-and-validators#Database-validators

best regards,
stifan
Reply all
Reply to author
Forward
0 new messages