Re: [web2py] Re: one to many relationship

2015-08-11 Thread Yebach
To create one to many form i tried to follow the post from this 
guy 
http://blog.jotbe-fx.de/articles/2522/web2py-Normalized-many-to-many-model-with-multiselect-drop-down

Also to create the dropdown etc.

The thing worked kind of but the problem was that my grid was not getting 
populated once you wanted to edit the new record etc.

I got stuck here with these code.
If anybody has some extra time to go trough and help me optimize it I would 
appreciate it otherwise I will go with normal list: reference field

Thank you

@auth.requires_login() 
def workers():
#za nekatere polja (w_user in W_organizacija) rabmo default vrednosti, ki 
jih ne more nastavljat uporabnik
user = auth.user_id
org = db(db.auth_user.id == 
user).select(db.auth_user.organization)[0][organization]
db.workers.w_user.default = user
db.workers.w_organisation.default = org
#Load workers
#workers = db((db.workers.w_organisation == 10)  (db.workers.w_status== 
db.status.id)).select(db.workers.id,db.workers.w_status, 
db.workers.w_organisation, db.workers.w_first_name, db.workers.w_last_name,\
# 
db.workers.w_nick_name,db.workers.w_email,db.status.s_code,db.workers.w_note)
#print workers
#NAredimo grrid za šifrant delavcev
#Define the query object. Here we are pulling all contacts having date of 
birth less than 18 Nov 1990
query = ((db.workers.w_organisation == org)  ((db.workers.w_status == 1) 
or (db.workers.w_status == 90)))
query_inactive = db((db.workers.w_organisation == org)  
(db.workers.w_status == 100)).select().as_list()
#print query_inactive
 
#Define the fields to show on grid. Note: (you need to specify id field in 
fields section in 1.99.2
fields = (#db.workers.id,
db.workers.w_first_name,
db.workers.w_last_name,
#db.status.s_code,
db.workers.w_nick_name,
db.workers.w_email,
db.workers_skills.skill,
db.workers.w_note)
#Let's specify a default sort order on date_of_birth column in grid
default_sort_order=[db.workers.w_last_name]
db.workers.w_organisation.readable = db.workers.w_user.readable = False
db.workers.w_organisation.writable = db.workers.w_user.writable = False
db.workers.w_organisation.editable = db.workers.w_user.editable = False
#Nardiš polje bl text like :) WIU WIU
db.workers.w_first_name.widget = SQLFORM.widgets.string.widget
db.workers.w_last_name.widget = SQLFORM.widgets.string.widget
db.workers.w_nick_name.widget = SQLFORM.widgets.string.widget
db.workers.w_email.widget = SQLFORM.widgets.string.widget
#VAlidatorji
#db.workers.w_status.requires = IS_IN_DB(db,db.status.s_code) #tega sm rešu 
v db.py
db.workers.w_nick_name.requires = [IS_NOT_EMPTY(error_message=T('Missing 
nick name'))]
db.workers.w_first_name.requires = [IS_NOT_EMPTY(error_message=T('Missing 
first name'))]
db.workers.w_email.requires = IS_EMAIL(error_message=T('Incorrect e-mail 
address'))
#form = SQLFORM.smartgrid(db.workers,linked_tables=['status'])
#Creating the grid object
if (request.args) and (request.args[0] in ['viev', 'edit', 'new']):
skills = [(r.id, r.sk_name) for r in db(db.skills).select()]
grid_workers = SQLFORM.factory(
db.workers,
   
Field('w_status', type='integer', label= T('Status'), widget = 
SQLFORM.widgets.options.widget, default = 1),
Field('w_first_name',type='text', label= T('First 
name'),represent=repr),
  Field('w_last_name',type='text', label= T('Last 
name'),represent=repr),
  Field('w_nick_name',type='text', label= T('Nick 
name'),represent=repr),
Field('w_email',type='text', label= T('e-mail'),represent=repr),
#Field('w_skills','list:reference skills',requires = 
IS_IN_DB(db,db.skills.id,'%(sk_name)s',multiple=True),label= T('Skills')),
#Field('w_groups','list:reference groups',requires = 
IS_IN_DB(db,db.groups.id,'%(gr_name)s',multiple=True),label= T('Groups')),
Field('skills',requires=IS_IN_SET(skills, multiple=True)),
Field('w_note',type='text', label= T('Comment'),represent=repr))
# (3) Validate form data
   if grid_workers.process().accepted:
# (4) Insert package
   
worker_insert = db.workers.insert(
   **db.workers._filter_fields(grid_workers.vars))
if worker_insert and grid_workers.vars.skills:
# (5) Insert component package associations

worker = db(db.workers)
for skills in grid_workers.vars.skills:
existing_skill = db.skills(id)
db.workers_skills.insert(
skill=skills,
worker=worker_insert
)
response.flash = New record created

else:
grid_workers= SQLFORM.grid(query=query, 
left=db.status.on(db.workers.w_status == db.status.id),
fields=fields,  searchable=False, 
orderby=[db.workers.w_nick_name],create=True,
deletable=False, editable=True, paginate=50, buttons_placement = 'right',
showbuttontext = False,
#oncreate=myfunction,
ui = dict(widget='',
 header='',
 content='',
 default='',
 cornerall='',
 cornertop='',
 cornerbottom='',
 button='button btn btn-default',
 

Re: [web2py] Re: one to many relationship

2015-08-10 Thread Dave S


On Monday, August 10, 2015 at 7:32:31 PM UTC-7, Yebach wrote:

 Thank you for detailed answer

 I am trying to implement this in SQLFORM? Looks like multiple option is 
 not avaliable, the field is presented as text field in the form

 BTW - why web2py does not add new field to a table. If I set migrate to 
 True I get an error table already exists if it is set to false new fields 
 were not created so I had to create them manually ??

 Thank you


Which database are you interfacing to?

/dps
 


 2015-08-07 19:10 GMT+08:00 Lisandro rostagno...@gmail.com javascript::

 Hi there.
 Web2py has some cool field types called list:string, list:integer and 
 list:reference table. Check them here:

 http://www.web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Field-types

 In your case, you could avoid the creation of the third table with 
 *list:reference 
 table*, for example, in your db.py:

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

 db.define_table('worker',\
 Field('name'),\
 Field('skills','list:reference skill'))


 Then you can do things like:

 skill_1 = db.skill.insert(name='coding in python')
 skill_2 = db.skill.insert(name='making a cake')
 skill_3 = db.skill.insert(name='writing a song')

 db.worker.insert(name='John', skills=[skill_1, skill_2])
 db.worker.insert(name='Paul', skills=[skill_3])
 db.worker.insert(name='Richard', skills=[skill_2, skill_3])

 # search which workers have skill_2:
 have_skill_3 = db(db.worker.skills.contains(skill_2)).select()


 The list:reference field will be rendered as a select with multiple=True.
 You can always write your own widget to use other stuff, like checkboxes.

 Look for list:reference in this group, you will lots of posts about it. 
 Hope it helps!



 El jueves, 6 de agosto de 2015, 22:51:53 (UTC-3), Yebach escribió:

 Hello

 How to solve the problem of one to many relationship:

 Lets say I have a worker that has multiple skills. How to set up SQLFORM 
 to add as many skills to the worker as possible.

 So skills in one table workers in another.I guess a third table will be 
 needed

 Thank you

 best regards

 -- 
 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/HjhQdAZWFWY/unsubscribe.
 To unsubscribe from this group and all its topics, send an email to 
 web2py+un...@googlegroups.com javascript:.
 For more options, visit https://groups.google.com/d/optout.




 -- 
 Lep pozdrav 

 Vid Ogris




-- 
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.


Re: [web2py] Re: one to many relationship

2015-08-10 Thread Vid Ogris
Postgres. The thimg is i have no table files. They somehow got deleted
On Aug 11, 2015 12:30 PM, Dave S snidely@gmail.com wrote:



 On Monday, August 10, 2015 at 7:32:31 PM UTC-7, Yebach wrote:

 Thank you for detailed answer

 I am trying to implement this in SQLFORM? Looks like multiple option is
 not avaliable, the field is presented as text field in the form

 BTW - why web2py does not add new field to a table. If I set migrate to
 True I get an error table already exists if it is set to false new fields
 were not created so I had to create them manually ??

 Thank you


 Which database are you interfacing to?

 /dps



 2015-08-07 19:10 GMT+08:00 Lisandro rostagno...@gmail.com:

 Hi there.
 Web2py has some cool field types called list:string, list:integer
 and list:reference table. Check them here:

 http://www.web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Field-types

 In your case, you could avoid the creation of the third table with 
 *list:reference
 table*, for example, in your db.py:

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

 db.define_table('worker',\
 Field('name'),\
 Field('skills','list:reference skill'))


 Then you can do things like:

 skill_1 = db.skill.insert(name='coding in python')
 skill_2 = db.skill.insert(name='making a cake')
 skill_3 = db.skill.insert(name='writing a song')

 db.worker.insert(name='John', skills=[skill_1, skill_2])
 db.worker.insert(name='Paul', skills=[skill_3])
 db.worker.insert(name='Richard', skills=[skill_2, skill_3])

 # search which workers have skill_2:
 have_skill_3 = db(db.worker.skills.contains(skill_2)).select()


 The list:reference field will be rendered as a select with multiple=True.
 You can always write your own widget to use other stuff, like checkboxes.

 Look for list:reference in this group, you will lots of posts about
 it.
 Hope it helps!



 El jueves, 6 de agosto de 2015, 22:51:53 (UTC-3), Yebach escribió:

 Hello

 How to solve the problem of one to many relationship:

 Lets say I have a worker that has multiple skills. How to set up
 SQLFORM to add as many skills to the worker as possible.

 So skills in one table workers in another.I guess a third table will be
 needed

 Thank you

 best regards

 --
 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/HjhQdAZWFWY/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.




 --
 Lep pozdrav

 Vid Ogris


 --
 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/HjhQdAZWFWY/unsubscribe.
 To unsubscribe from this group and all its topics, send an email to
 web2py+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/d/optout.


-- 
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.


Re: [web2py] Re: one to many relationship

2015-08-10 Thread Vid Ogris
Thank you for detailed answer

I am trying to implement this in SQLFORM? Looks like multiple option is not
avaliable, the field is presented as text field in the form

BTW - why web2py does not add new field to a table. If I set migrate to
True I get an error table already exists if it is set to false new fields
were not created so I had to create them manually ??

Thank you

2015-08-07 19:10 GMT+08:00 Lisandro rostagnolisan...@gmail.com:

 Hi there.
 Web2py has some cool field types called list:string, list:integer and
 list:reference table. Check them here:

 http://www.web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Field-types

 In your case, you could avoid the creation of the third table with 
 *list:reference
 table*, for example, in your db.py:

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

 db.define_table('worker',\
 Field('name'),\
 Field('skills','list:reference skill'))


 Then you can do things like:

 skill_1 = db.skill.insert(name='coding in python')
 skill_2 = db.skill.insert(name='making a cake')
 skill_3 = db.skill.insert(name='writing a song')

 db.worker.insert(name='John', skills=[skill_1, skill_2])
 db.worker.insert(name='Paul', skills=[skill_3])
 db.worker.insert(name='Richard', skills=[skill_2, skill_3])

 # search which workers have skill_2:
 have_skill_3 = db(db.worker.skills.contains(skill_2)).select()


 The list:reference field will be rendered as a select with multiple=True.
 You can always write your own widget to use other stuff, like checkboxes.

 Look for list:reference in this group, you will lots of posts about it.
 Hope it helps!



 El jueves, 6 de agosto de 2015, 22:51:53 (UTC-3), Yebach escribió:

 Hello

 How to solve the problem of one to many relationship:

 Lets say I have a worker that has multiple skills. How to set up SQLFORM
 to add as many skills to the worker as possible.

 So skills in one table workers in another.I guess a third table will be
 needed

 Thank you

 best regards

 --
 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/HjhQdAZWFWY/unsubscribe.
 To unsubscribe from this group and all its topics, send an email to
 web2py+unsubscr...@googlegroups.com.
 For more options, visit https://groups.google.com/d/optout.




-- 
Lep pozdrav

Vid Ogris

-- 
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.


[web2py] Re: one to many relationship

2015-08-07 Thread Lisandro
Hi there.
Web2py has some cool field types called list:string, list:integer and 
list:reference table. Check them here:
http://www.web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Field-types

In your case, you could avoid the creation of the third table with 
*list:reference 
table*, for example, in your db.py:

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

db.define_table('worker',\
Field('name'),\
Field('skills','list:reference skill'))


Then you can do things like:

skill_1 = db.skill.insert(name='coding in python')
skill_2 = db.skill.insert(name='making a cake')
skill_3 = db.skill.insert(name='writing a song')

db.worker.insert(name='John', skills=[skill_1, skill_2])
db.worker.insert(name='Paul', skills=[skill_3])
db.worker.insert(name='Richard', skills=[skill_2, skill_3])

# search which workers have skill_2:
have_skill_3 = db(db.worker.skills.contains(skill_2)).select()


The list:reference field will be rendered as a select with multiple=True.
You can always write your own widget to use other stuff, like checkboxes.

Look for list:reference in this group, you will lots of posts about it. 
Hope it helps!



El jueves, 6 de agosto de 2015, 22:51:53 (UTC-3), Yebach escribió:

 Hello

 How to solve the problem of one to many relationship:

 Lets say I have a worker that has multiple skills. How to set up SQLFORM 
 to add as many skills to the worker as possible.

 So skills in one table workers in another.I guess a third table will be 
 needed

 Thank you

 best regards


-- 
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.


[web2py] Re: one to many relationship DB structure

2013-10-16 Thread 黄祥
yeah, my pov is because your product never save the sale id. imho, i think 
your code should be :
db.define_table(
'sale',
Field('product_id', 'reference product'),
Field('user_id', db.auth_user, default=auth.user_id),
Field('start_date', 'date', requires=IS_DATE()),
Field('end_date', 'date', requires=IS_DATE()),
Field('start_time', 'time', requires = IS_TIME()),
Field('end_time', 'time', requires = IS_TIME()))

db.define_table(
'product',
Field('name', requires = IS_NOT_EMPTY()),
Field('price', 'double', default=0.00),
Field('description','text'),
Field('image', 'upload', default=''),
format = '%(name)s %(price)s')

because 1 sale has many product and each product belongs to sale, isn't it?
1 more thing please don't use id as your field name, because by default 
web2py use it as a primary key.
taken from book :

   - Each table must have a unique auto-increment integer field called id
   - Records must be referenced exclusively using the id field.

ref:
http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Legacy-databases-and-keyed-tables

best regards,
stifan

-- 
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/groups/opt_out.


[web2py] Re: one to many relationship DB structure

2013-10-16 Thread raferbop
Thanks Stifan, 

But that doesn't work either, because the product_id now displays in the 
sale form. I need to generate the sale_id first, and then have sow in the 
product table/form. Right now, it just shows up blank. 

On Wednesday, October 16, 2013 11:08:58 AM UTC-5, 黄祥 wrote:

 yeah, my pov is because your product never save the sale id. imho, i think 
 your code should be :
 db.define_table(
 'sale',
 Field('product_id', 'reference product'),
 Field('user_id', db.auth_user, default=auth.user_id),
 Field('start_date', 'date', requires=IS_DATE()),
 Field('end_date', 'date', requires=IS_DATE()),
 Field('start_time', 'time', requires = IS_TIME()),
 Field('end_time', 'time', requires = IS_TIME()))

 db.define_table(
 'product',
 Field('name', requires = IS_NOT_EMPTY()),
 Field('price', 'double', default=0.00),
 Field('description','text'),
 Field('image', 'upload', default=''),
 format = '%(name)s %(price)s')

 because 1 sale has many product and each product belongs to sale, isn't it?
 1 more thing please don't use id as your field name, because by default 
 web2py use it as a primary key.
 taken from book :

- Each table must have a unique auto-increment integer field called 
id
- Records must be referenced exclusively using the id field.

 ref:

 http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Legacy-databases-and-keyed-tables

 best regards,
 stifan


-- 
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/groups/opt_out.


[web2py] Re: one to many relationship DB structure

2013-10-16 Thread Cliff Kachinske
This is a problem:

db.define_table(
'sale',
Field('id', db.auth_user, default=auth.user_id),

How about this instead:
db.define_table(
'sale',
Field('user_id', db.auth_user, requires=IS_IN_DB( # See Forms and 
Validators in the Fine Manual

Also do this:
db.define_table(
'product',
Field('sale_id', db.sale, requires=IS_IN_DB( # See Forms and 
Validators in the Fine Manual

Also I would not use the double data type for currency. Use instead Python 
decimal which works as you expect decimal numbers to work. Or you could 
store the, for example, US currency as pennies and put in the decimal 
marker when you display the value and strip it out when you work with it on 
the back end.

Whatever comes in on request.post_vars and ends up in form.vars is a 
string.  Your database adapter takes care of conversions.




On Wednesday, October 16, 2013 1:36:53 PM UTC-4, raferbop wrote:

 Thanks Stifan, 

 But that doesn't work either, because the product_id now displays in the 
 sale form. I need to generate the sale_id first, and then have sow in the 
 product table/form. Right now, it just shows up blank. 

 On Wednesday, October 16, 2013 11:08:58 AM UTC-5, 黄祥 wrote:

 yeah, my pov is because your product never save the sale id. imho, i 
 think your code should be :
 db.define_table(
 'sale',
 Field('product_id', 'reference product'),
 Field('user_id', db.auth_user, default=auth.user_id),
 Field('start_date', 'date', requires=IS_DATE()),
 Field('end_date', 'date', requires=IS_DATE()),
 Field('start_time', 'time', requires = IS_TIME()),
 Field('end_time', 'time', requires = IS_TIME()))

 db.define_table(
 'product',
 Field('name', requires = IS_NOT_EMPTY()),
 Field('price', 'double', default=0.00),
 Field('description','text'),
 Field('image', 'upload', default=''),
 format = '%(name)s %(price)s')

 because 1 sale has many product and each product belongs to sale, isn't 
 it?
 1 more thing please don't use id as your field name, because by default 
 web2py use it as a primary key.
 taken from book :

- Each table must have a unique auto-increment integer field called 
id
- Records must be referenced exclusively using the id field.

 ref:

 http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Legacy-databases-and-keyed-tables

 best regards,
 stifan



-- 
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/groups/opt_out.


[web2py] Re: one to many relationship DB structure

2013-10-16 Thread raferbop
db.define_table(
'sale',
Field('user_id', db.auth_user),
Field('start_date', 'date', requires=IS_DATE()),
Field('end_date', 'date', requires=IS_DATE()),
Field('start_time', 'time', requires = IS_TIME()),
Field('end_time', 'time', requires = IS_TIME()))

db.sale.user_id.requires = IS_IN_DB(db, 'auth_user.id')


db.define_table(
'product',
Field('sale_id', db.sale),
Field('name', requires = IS_NOT_EMPTY()),
Field('price'),
Field('description','text'),
Field('image', 'upload', default=''),
format = '%(name)s %(price)s')

db.product.sale_id.requires = IS_IN_DB(db, 'sale.id')


Thanks Cliff,

This is an improvement on what I had. But I am not sure if I applied the 
code correctly because the 'user_id' and 'sale_id' have been converted into 
a drop-down list, as opposed to being automatically populated.


On Wednesday, October 16, 2013 4:18:52 PM UTC-5, Cliff Kachinske wrote:

 This is a problem:

 db.define_table(
 'sale',
 Field('id', db.auth_user, default=auth.user_id),

 How about this instead:
 db.define_table(
 'sale',
 Field('user_id', db.auth_user, requires=IS_IN_DB( # See Forms and 
 Validators in the Fine Manual

 Also do this:
 db.define_table(
 'product',
 Field('sale_id', db.sale, requires=IS_IN_DB( # See Forms and 
 Validators in the Fine Manual

 Also I would not use the double data type for currency. Use instead Python 
 decimal which works as you expect decimal numbers to work. Or you could 
 store the, for example, US currency as pennies and put in the decimal 
 marker when you display the value and strip it out when you work with it on 
 the back end.

 Whatever comes in on request.post_vars and ends up in form.vars is a 
 string.  Your database adapter takes care of conversions.




 On Wednesday, October 16, 2013 1:36:53 PM UTC-4, raferbop wrote:

 Thanks Stifan, 

 But that doesn't work either, because the product_id now displays in the 
 sale form. I need to generate the sale_id first, and then have sow in the 
 product table/form. Right now, it just shows up blank. 

 On Wednesday, October 16, 2013 11:08:58 AM UTC-5, 黄祥 wrote:

 yeah, my pov is because your product never save the sale id. imho, i 
 think your code should be :
 db.define_table(
 'sale',
 Field('product_id', 'reference product'),
 Field('user_id', db.auth_user, default=auth.user_id),
 Field('start_date', 'date', requires=IS_DATE()),
 Field('end_date', 'date', requires=IS_DATE()),
 Field('start_time', 'time', requires = IS_TIME()),
 Field('end_time', 'time', requires = IS_TIME()))

 db.define_table(
 'product',
 Field('name', requires = IS_NOT_EMPTY()),
 Field('price', 'double', default=0.00),
 Field('description','text'),
 Field('image', 'upload', default=''),
 format = '%(name)s %(price)s')

 because 1 sale has many product and each product belongs to sale, isn't 
 it?
 1 more thing please don't use id as your field name, because by default 
 web2py use it as a primary key.
 taken from book :

- Each table must have a unique auto-increment integer field called 
id
- Records must be referenced exclusively using the id field.

 ref:

 http://web2py.com/books/default/chapter/29/06/the-database-abstraction-layer#Legacy-databases-and-keyed-tables

 best regards,
 stifan



-- 
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/groups/opt_out.


[web2py] Re: one to many relationship DB structure

2013-10-16 Thread Anthony


 db.sale.user_id.requires = IS_IN_DB(db, 'auth_user.id')


The above line is unnecessary, as you get the IS_IN_DB validator 
automatically by default.
 

 This is an improvement on what I had. But I am not sure if I applied the 
 code correctly because the 'user_id' and 'sale_id' have been converted into 
 a drop-down list, as opposed to being automatically populated.


The values won't be populated automatically unless you specify an actual 
value (you can do so via the default argument).

Anthony

-- 
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/groups/opt_out.


[web2py] Re: one to many relationship DB structure

2013-10-16 Thread raferbop
Thanks Anthony, the default argument, which I am assuming is 
default=auth.user_id, 
inserts the user id into the sales table. But what argument should I use to 
insert the sale_id into the products table, very much in the same way the  
default=auth.user_id inserst the user id in the db.sale. 

On Wednesday, October 16, 2013 6:22:12 PM UTC-5, Anthony wrote:

 db.sale.user_id.requires = IS_IN_DB(db, 'auth_user.id')


 The above line is unnecessary, as you get the IS_IN_DB validator 
 automatically by default.
  

 This is an improvement on what I had. But I am not sure if I applied the 
 code correctly because the 'user_id' and 'sale_id' have been converted into 
 a drop-down list, as opposed to being automatically populated.


 The values won't be populated automatically unless you specify an actual 
 value (you can do so via the default argument).

 Anthony


-- 
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/groups/opt_out.


[web2py] Re: one to many relationship DB structure

2013-10-16 Thread Anthony
Actually, I'm not sure this is the right data model. Presumably the product 
table should include just one record per product, so you don't want to 
store a sale id in the product table. If each sale can include only one 
product, then you want a product reference field in the sale table. If each 
sale can include multiple products, then you probably want a separate 
items table where each record references a sale and a product (i.e., for 
a many-to-many relationship between products and sales). Another option 
would be to include a list:reference field in the sale table to store a 
list of references to multiple products (this is a simpler model but can 
make it less efficient to do various types of aggregate analyses, such as 
calculating number of sales per product).

Anthony

On Wednesday, October 16, 2013 8:03:14 PM UTC-4, raferbop wrote:

 Thanks Anthony, the default argument, which I am assuming is default=auth
 .user_id, inserts the user id into the sales table. But what argument 
 should I use to insert the sale_id into the products table, very much in 
 the same way the  default=auth.user_id inserst the user id in the 
 db.sale. 

 On Wednesday, October 16, 2013 6:22:12 PM UTC-5, Anthony wrote:

 db.sale.user_id.requires = IS_IN_DB(db, 'auth_user.id')


 The above line is unnecessary, as you get the IS_IN_DB validator 
 automatically by default.
  

 This is an improvement on what I had. But I am not sure if I applied the 
 code correctly because the 'user_id' and 'sale_id' have been converted into 
 a drop-down list, as opposed to being automatically populated.


 The values won't be populated automatically unless you specify an actual 
 value (you can do so via the default argument).

 Anthony



-- 
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/groups/opt_out.


[web2py] Re: one to many relationship DB structure

2013-10-16 Thread raferbop
I am going to give it a go right now.

On Wednesday, October 16, 2013 7:18:33 PM UTC-5, Anthony wrote:

 Actually, I'm not sure this is the right data model. Presumably the 
 product table should include just one record per product, so you don't want 
 to store a sale id in the product table. If each sale can include only one 
 product, then you want a product reference field in the sale table. If each 
 sale can include multiple products, then you probably want a separate 
 items table where each record references a sale and a product (i.e., for 
 a many-to-many relationship between products and sales). Another option 
 would be to include a list:reference field in the sale table to store a 
 list of references to multiple products (this is a simpler model but can 
 make it less efficient to do various types of aggregate analyses, such as 
 calculating number of sales per product).

 Anthony

 On Wednesday, October 16, 2013 8:03:14 PM UTC-4, raferbop wrote:

 Thanks Anthony, the default argument, which I am assuming is default=
 auth.user_id, inserts the user id into the sales table. But what 
 argument should I use to insert the sale_id into the products table, very 
 much in the same way the  default=auth.user_id inserst the user id in 
 the db.sale. 

 On Wednesday, October 16, 2013 6:22:12 PM UTC-5, Anthony wrote:

 db.sale.user_id.requires = IS_IN_DB(db, 'auth_user.id')


 The above line is unnecessary, as you get the IS_IN_DB validator 
 automatically by default.
  

 This is an improvement on what I had. But I am not sure if I applied 
 the code correctly because the 'user_id' and 'sale_id' have been converted 
 into a drop-down list, as opposed to being automatically populated.


 The values won't be populated automatically unless you specify an actual 
 value (you can do so via the default argument).

 Anthony



-- 
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/groups/opt_out.


[web2py] Re: one to many relationship but Foreign key as non-id,unique field.

2010-08-08 Thread qqsaqq
as I understand this, the DAL always uses the id field for referencing
in a one to many relationship. And: you don't link to a certain field
(EmailID) but to a record of your emails table.

see http://web2py.com/book/default/chapter/06#Legacy-Databases-and-Keyed-Tables

On Aug 8, 8:00 am, Phyo Arkar phyo.arkarl...@gmail.com wrote:
 I am Thinking to link two tables ,
 emails and attachments

 emails have a unique key , EmailID , which is a md5sum of the path to email
 file

 I am trying to link with another table which is attachment table , it will
 have a field  'attachment_of'   which is link to EmailID

 To Short

 How can i link between primary and foreign keys which is  a
 non-id_but_unique field?


Re: [web2py] Re: one to many relationship but Foreign key as non-id,unique field.

2010-08-08 Thread Phyo Arkar
Yes i understand that 1 to Many do not link to Field but Record but i need
to Reference to a Field which is not id  .
so with that i can set two primary keys but i want to reference to EmailID
(which is not auto increment Integer but a md5sum , string , Unique) .

Is that possible?

On Sun, Aug 8, 2010 at 1:36 PM, qqsaqq sla...@gmx.net wrote:

 as I understand this, the DAL always uses the id field for referencing
 in a one to many relationship. And: you don't link to a certain field
 (EmailID) but to a record of your emails table.

 see
 http://web2py.com/book/default/chapter/06#Legacy-Databases-and-Keyed-Tables

 On Aug 8, 8:00 am, Phyo Arkar phyo.arkarl...@gmail.com wrote:
  I am Thinking to link two tables ,
  emails and attachments
 
  emails have a unique key , EmailID , which is a md5sum of the path to
 email
  file
 
  I am trying to link with another table which is attachment table , it
 will
  have a field  'attachment_of'   which is link to EmailID
 
  To Short
 
  How can i link between primary and foreign keys which is  a
  non-id_but_unique field?



[web2py] Re: one to many relationship but Foreign key as non-id,unique field.

2010-08-08 Thread mdipierro
No. But you can enforce the reference at the web2py level with the
IS_IN_DB validator.

On Aug 8, 2:36 am, Phyo Arkar phyo.arkarl...@gmail.com wrote:
 Yes i understand that 1 to Many do not link to Field but Record but i need
 to Reference to a Field which is not id  .
 so with that i can set two primary keys but i want to reference to EmailID
 (which is not auto increment Integer but a md5sum , string , Unique) .

 Is that possible?

 On Sun, Aug 8, 2010 at 1:36 PM, qqsaqq sla...@gmx.net wrote:
  as I understand this, the DAL always uses the id field for referencing
  in a one to many relationship. And: you don't link to a certain field
  (EmailID) but to a record of your emails table.

  see
 http://web2py.com/book/default/chapter/06#Legacy-Databases-and-Keyed-...

  On Aug 8, 8:00 am, Phyo Arkar phyo.arkarl...@gmail.com wrote:
   I am Thinking to link two tables ,
   emails and attachments

   emails have a unique key , EmailID , which is a md5sum of the path to
  email
   file

   I am trying to link with another table which is attachment table , it
  will
   have a field  'attachment_of'   which is link to EmailID

   To Short

   How can i link between primary and foreign keys which is  a
   non-id_but_unique field?