The problem is solved. I have to insert state_id with each insert of
record, default doesn't work.
Thank you for the help.
Suresh
On Wednesday, June 13, 2012 7:50:40 PM UTC+5:30, Suresh wrote:
>
> Thank you very much for your reply.
>
> I am attaching the file db.py. I have kept field name unique in all
> country, state and city. Actually a city needs to be unique in a state
> only, two cities may have the same name if they are in different states. So
> I need a unique key name+str(state_id) for file city and name need not be
> unique.
> For the same reason the default for city should be "city.name, state.name".
> I would prefer a computed field like fullname in city table, because I am
> using smartgrid in my admin view.
>
> When I used a computed field the state_id worked only for the first record
> of city(state_id =1). For other records state_id was null. I request you to
> modify the code and try admin.
>
> Regards.
>
> Suresh
>
>
> On Wednesday, June 13, 2012 8:22:38 AM UTC+5:30, Anthony wrote:
>>
>> db.define_table('state',
>>> Field('country_id', db.country),
>>> Field('name', unique=True),
>>> # Field('full_name',compute=lambda row: row.country_id, unique=True),
>>> format = '%(name)s')
>>> #db(db.country.id==r['country_id']).select().first().name
>>> #class StateVirtualFields(object):
>>> # def namecountryid(self):
>>> # return str(self.state.country_id)+self.state.name
>>> #db.state.virtualfields.append(StateVirtualFields())
>>>
>>> #db.state.full_name = Field.Virtual(lambda row: row.country_id)
>>> db.state.fullname.compute = lambda row: str(row.country_id)+row.name
>>>
>>> 1. db.state.fullname.compute doesnot work at all.
>>>
>>
>> Is that the exact code you ran? If so, you have not defined a "fullname"
>> field anywhere.
>>
>>
>>> 2. if I use Field('full_name',compute=lambda row: row.country_id,
>>> unique=True), for some reason it does *NOT* access country_id properly.
>>>
>>
>> What do you mean it doesn't access it properly? Show the code you are
>> using to insert into the table.
>>
>> If you are using a form to insert records and want to ensure the
>> combination of two fields is unique, you might also consider using an
>> onvalidation function (see
>> http://web2py.com/books/default/chapter/29/7#onvalidation) or something
>> like this:
>> https://groups.google.com/d/msg/web2py/CRPySzABQTk/8jylUabyFTQJ.
>>
>> Anthony
>>
>