Execute functions in Model
Hi Guys,
first i´ve to say i am n(oob)ew to Python and Database things, so
please be patient.
Is there any general way to execute custom functions in Model, which
not only validate and how to implement them?
I´ll try to explain with a few examples:
First the simple one:
I have a table with 2 fields:
db.define_table("table1",
Field("field1", "string"),
Field("field2", "string"))
field1 is required so there should be two options:
1. the user enters something in field1 and field2
2. the user enters something in field1 and leaves field2 blank
In the second case i want to use a function that takes the value from
field1 splits the first word and then stores it in field2.
something like:
def function():
field1_value="word1 word2 word3"
valuesplit= field1_value.split(" ")[:1]
joinvalue= " ".join(valuesplit)
return joinvalue
Everthing should work from model...
I have already tried the solution from the thread "Populate field
based no other fields":
def _compute():
return request.vars.field1
Field("field2", "string", compute=_compute
It works but field2 wont be visible in appadmin.
Second Example:
I have 2 tables with 2 fields:
db.define_table("table1",
Field("field1", "string"),
Field("field2", "string"))
db.define_table("table2",
Field("field1", "string"),
Field("field2", "string"))
Now user enters value in table1.field1
then funktion should split first part of the value
then looking up if value is present in table2.field1 (.like(%value))
and if true store the table2.field2 value in table1.field2
Can anybuddy help?
--
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en.