rewrite the controller function for clarity

59 views
Skip to first unread message

Abhijit Chatterjee

unread,
Feb 24, 2015, 8:49:01 AM2/24/15
to web...@googlegroups.com, Massimo DiPierro
Hello,

Does anyone know how to rewrite this two contoller line? After model and view, I am trying to get familiar with controller and I am finding when a code is summarized, its hard to understand at times. Wondering if someone can break it down for me a little?

first def:

(1) categories = db(db.category).select(orderby=db.category.name)

why two dbs? can we rewrite this without the select? Is select is a web2py key word? I come from a MATLAB world where we define the structure first and then use. I don't recall I defined select to be a struct.

(2) similarly on my second def:

category = db.category(request.args(0))

Now, I only see one db instead of two. Why? I understand the request args means where I click. Which argument is it.

also this line,

news = db(db.news.category == category.id).select(orderby=db.news.vote)

Its hard to understand when lot of arguments are combined into one. Anyone can help break it down for me a little? Any idea about how to rewrite that, may be even using "if"statement without structure if possible?





Anthony

unread,
Feb 24, 2015, 9:48:48 AM2/24/15
to web...@googlegroups.com, massimo....@gmail.com
This is all DAL syntax. I suggest you read the chapter on the DAL, particularly this section and this section (for your question #2).

Leonel Câmara

unread,
Feb 24, 2015, 9:58:25 AM2/24/15
to web...@googlegroups.com, massimo....@gmail.com
1)

db is your instance of the DAL class. A class in python can have a __call__ method. If it does, instances of that class can be called like functions. Which is what your doing here with db(...).

In this case the DAL's __call__ takes a query or nothing and returns a Set (not a python set, it's a DAL class). To make this query is why you have the second db. There's another method in the DAL called __getattr__ which allows you to get the tables from the DAL defined with define_table by using dot notation. So what you're doing here is ask the DAL instance to give you the table category.

So now we know what this means:
my_set = db(db.category)

You're asking for a Set with the table category.

select is a method of Set which pretty much is equivalent to SQL's select and returns Rows. Which is what you then do with:

my_set.select(orderby=db.category.name)

This can be simplified by simply calling select on the returning Set immediately so you don't need a variable for the set if you just want the Rows.

 categories = db(db.category).select(orderby=db.category.name)

In this case, on the orderby, first you get the table from db using getattr and then you get the Field, which you want to use to order, from the table.



That said, you don't need to know any of this, just learn the DAL's syntax like Anthony said.

Massimo DiPierro

unread,
Feb 24, 2015, 10:41:08 AM2/24/15
to Abhijit Chatterjee, web...@googlegroups.com
the DAL has only these API:

db(query).select(….)
db(query).update(…)
db(query).count()
db(query).delete()
table.insert(…)

which map into the corresponding SQL SELECT, UPDATE, COUNT, DELETE and INSERT.

If you define a table with
db.define_table(‘person’,Field(‘name’))
than you refer to it by db.person
If you define it with
Person = db.define_table(‘person’,Field(‘name’))
than you can refer to it by simply Person.

other the query all the other arguments are optional.

Ron Chatterjee

unread,
Feb 24, 2015, 10:58:18 AM2/24/15
to Massimo DiPierro, web...@googlegroups.com

Thanks a lot massimo.  U r the man!

Ron Chatterjee

unread,
Feb 24, 2015, 11:09:29 AM2/24/15
to web...@googlegroups.com, Massimo DiPierro

Thank u leonel n Anthony.  That was very helpful. Nice to have so many experts on this site!

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

Ron Chatterjee

unread,
Feb 24, 2015, 10:56:01 PM2/24/15
to web...@googlegroups.com
You explained very well Leonel, if you don't mind asking. What would be wrong if I do?


  news = dbOBJECT(dbOBJECT.news.category==request.args(0)).select()

Instead of how its written. Same thing. Right? Basically, its like a "find" command in MATLAB. in the "set", if the category is 1, return the selected item. I know the way it was written because its meant for redirect etc. But wouldn't it be same? I get the same results.



--

Leonel Câmara

unread,
Feb 25, 2015, 8:58:42 AM2/25/15
to web...@googlegroups.com, massimo....@gmail.com
There's nothing wrong with that code. You may want to do additional checks on request.args(0), for instance making sure it's an int with request.args(0, cast=int) and even that it's an id of a category that actually exists (which you may want to use to inform the user why he isn't getting any results). Otherwise it's perfectly fine to do it that way.

Ron Chatterjee

unread,
Feb 25, 2015, 9:24:09 AM2/25/15
to web...@googlegroups.com

R u in the states? Looking for work?

On Feb 25, 2015 8:58 AM, "Leonel Câmara" <leonel...@gmail.com> wrote:
There's nothing wrong with that code. You may want to do additional checks on request.args(0), for instance making sure it's an int with request.args(0, cast=int) and even that it's an id of a category that actually exists (which you may want to use to inform the user why he isn't getting any results). Otherwise it's perfectly fine to do it that way.

--

Leonel Câmara

unread,
Feb 25, 2015, 11:03:40 AM2/25/15
to web...@googlegroups.com, massimo....@gmail.com
Portugal :)

Ron Chatterjee

unread,
Feb 25, 2015, 11:15:33 AM2/25/15
to web...@googlegroups.com, Massimo DiPierro

Thats cool. I was telling massimo that I have some ideas. Would b nice to network if something does spin off.

On Feb 25, 2015 11:03 AM, "Leonel Câmara" <leonel...@gmail.com> wrote:
Portugal :)

Leonel Câmara

unread,
Feb 25, 2015, 11:27:53 AM2/25/15
to web...@googlegroups.com, massimo....@gmail.com
Sure, I'm willing to help with what I can do remotely.
Reply all
Reply to author
Forward
0 new messages