display items only entered by a logged in user in a dropdown menu of a reference table

62 views
Skip to first unread message

mostwanted

unread,
Jul 19, 2020, 4:09:58 AM7/19/20
to web2py-users
In my application I have 4 tables, 3 of them referenced in the fourth table but every user should be able to view only what they have entered into the database. Is there a way that in a dropdown list of a referenced table in an SQLFORM users can only view & select from items they have entered?

MODELS CODE:
db.define_table('lecturer',
               
Field('surname'),
               
Field('name'),
               
Field('title'),
               
Field('posted_by', 'reference auth_user', default=auth.user_id, readable=False, writable=False), #HERE
                format
="%(surname)s %(name)s %(title)s")
def lecturer(details): return '%(surname)s %(name)s' % details

db
.define_table('subject',
               
Field('subject_name'),
               
Field('posted_by', 'reference auth_user', default=auth.user_id, readable=False, writable=False), #HERE
                format
="%(subject_name)s"
               
)
db
.define_table('departments',
               
Field('department_name'),
               
Field('posted_by', 'reference auth_user', default=auth.user_id, readable=False, writable=False), #HERE
                format
="%(department_name)s")

db
.define_table('lecture',
               
Field('subject_name', 'reference subject'),
               
Field('department', 'reference departments'),
#HERE
               
Field('theLevels', label=SPAN('Levels'), requires=IS_IN_SET(['1.1', '1.2', '2.1', '2.2', '3.1', '3.2', '4.1', '4.2'], zero='---Select A Level---')), #HERE
                Field('lecturer', 'reference lecturer'),
               
Field('class_session', requires=IS_IN_SET(['single session', 'double session', 'evening single session', 'evening double session'], zero='----Select A Session Period----')),
               
Field('the_time2', readable=False, writable=False),
               
Field('class_room', requires=IS_IN_SET(['500', '501', '502', '503', '504', '505', '506'], zero='---Select A Class Room---')),
               
Field('controller', readable=False, writable=False),
               
Field('posted_by', 'reference auth_user', default=auth.user_id, readable=False, writable=False))



CONTROLLER CODE:
I tried the highlighted part but it didnt work, I felt it wouldnt work but tried it anyway & it didnt work (shocker!)
Please help me figure this out.
def index():
   
user=db.auth_user(auth.user_id)
   
if not user or not(user.id==auth.user_id): redirect(URL('index'))
    form
=SQLFORM(db.lecture.posted_by==user.id)

   
if form.process(onvalidation=my_form_processing).accepted:
        response
.flash=T('Lecture Entered')
   
return locals()


Regards;

Mostwanted

AGRogers

unread,
Jul 19, 2020, 7:13:06 AM7/19/20
to web...@googlegroups.com
I would try using a common_filter on the drop down list table. Could that work? 

--
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+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/e98d4e6a-69a2-4c1c-9028-3d98579a0f59o%40googlegroups.com.

mostwanted

unread,
Jul 19, 2020, 9:06:28 AM7/19/20
to web2py-users
Hey AGRogers, thanks for that suggestion, I added the highlited lines to my model code & it gave results, thank you very much:


db.define_table('lecturer',
               
Field('surname'),
               
Field('name'),
               
Field('title'),
               
Field('posted_by', 'reference auth_user', default=auth.user_id, readable=False, writable=False),

                common_filter = lambda query: db.lecturer.posted_by == auth.user_id,

                format
="%(surname)s %(name)s %(title)s")
def lecturer(details): return '%(surname)s %(name)s' % details

db
.define_table('subject',
               
Field('subject_name'),
               
Field('posted_by', 'reference auth_user', default=auth.user_id, readable=False, writable=False),

                common_filter = lambda query: db.subject.posted_by == auth.user_id,

                format
="%(subject_name)s"
               
)
db
.define_table('departments',
               
Field('department_name'),
               
Field('posted_by', 'reference auth_user', default=auth.user_id, readable=False, writable=False),

                common_filter = lambda query: db.departments.posted_by == auth.user_id,

                format
="%(department_name)s")

db
.define_table('lecture',
               
Field('subject_name', 'reference subject'),
               
Field('department', 'reference departments'),
#HERE
               
Field('theLevels', label=SPAN('Levels'), requires=IS_IN_SET(['1.1', '1.2', '2.1', '2.2', '3.1', '3.2', '4.1', '4.2'], zero='---Select A Level---')), #HERE
                Field('lecturer', 'reference lecturer'),
               
Field('class_session', requires=IS_IN_SET(['single session', 'double session', 'evening single session', 'evening double session'], zero='----Select A Session Period----')),
               
Field('the_time2', readable=False, writable=False),
               
Field('class_room', requires=IS_IN_SET(['500', '501', '502', '503', '504', '505', '506'], zero='---Select A Class Room---')),
               
Field('controller', readable=False, writable=False),
               
Field('posted_by', 'reference auth_user', default=auth.user_id, readable=False, writable=False))

Regards;

Mostwanted
To unsubscribe from this group and stop receiving emails from it, send an email to web...@googlegroups.com.

Akshata Naduvinahalli

unread,
Jul 19, 2020, 11:50:21 AM7/19/20
to web...@googlegroups.com
Consider that :you have list of names,which you have entered through front end and you have search and filter the entered data on the search box. Please send the video related or code for this.

To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/a7a24094-6fea-465c-9a7d-eeb582c2acaco%40googlegroups.com.

AGRogers

unread,
Jul 19, 2020, 8:07:41 PM7/19/20
to web...@googlegroups.com
Great. It's a cool feature.

You would have read that you can turn it off in code temporarily just by setting common_filter =none.

That has caught me a few times. 

To unsubscribe from this group and stop receiving emails from it, send an email to web2py+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/web2py/a7a24094-6fea-465c-9a7d-eeb582c2acaco%40googlegroups.com.

Sanka kanaka krupakar

unread,
Jul 19, 2020, 10:45:38 PM7/19/20
to web...@googlegroups.com
Dear group members

Please advise how to create multiple rows insert by usung SQL form .

AGRogers

unread,
Jul 19, 2020, 10:57:21 PM7/19/20
to web...@googlegroups.com
I'm not sure that is possible Sanka. At least I have not read it in the book nor seen it referenced by anyone. 

I expect you have to write a custom form and handle it manually. But web2py can still help with those tasks. 

villas

unread,
Jul 20, 2020, 8:21:47 AM7/20/20
to web2py-users
>> Please advise how to create multiple rows insert by usung SQL form .

You have to create a form with multiple rows and then loop through multiple records when the form is accepted.

To give you a better idea,  here is a snippet from some code I used in the distant past. It updates multiple records from a custom form. You could also use this same technique using update_or_insert. I hope you can figure it out.

---- controller ------
myrecords=db(query).select(db.table.ALL)
if form.accepts ...
    for r in myrecords:
        if request.vars.has_key('id%i_price' % r.table.id):
            r.table.update_record(price=request.vars['id%i_price'%
r.table.id])

----- view ------
<table>....
{{for record in myrecords:}}
  <tr>
    <td>{{=record.carprice.id}}</td>
    <td><INPUT type="text", name="id{{=record.table.id}}_price",
value={{=record.table.price}}/></td>
  </tr>
{{pass}}
...</table>
Reply all
Reply to author
Forward
0 new messages