On 10/11/11 01:50 -0800, vuk wrote:
> hello everybody, i have this situation: a model A with a Many2One
> field to a model B and a DateTime field whose default value shuld be
> based on a model B datetime value. Here is a simplified code of the
> situation:
>
> ................................................................................................
> class Lesson(ModelSQL, ModelView):
> .....
> StartDateTime = fields.DateTime(string='Start')
> Activities = fields.One2Many('train.Activity', 'Lesson',
> string='Activities')
> ......
> class Activity(ModelSQL, ModelView):
> ......
> Lesson = fields.Many2One('train.Lesson', string='Lesson')
> StartDateTime = fields.DateTime(string='Start Date Time',
> depends=['Lesson'])
>
> def default_StartDateTime(self):
> #lesson_obj = Pool().get('train.Lesson')
> lesson = Transaction().context.get('Lesson')
> return ''
> ................................................................................................
> The logic is that the activity is created starting from the One2Many
> Activities field of the Lesson form and that the activity start time
> proposed into the form is the Lesson one (that should be te norm,
> unless a student comes late to the lesson)
> I understand that i must use the default_StartDateTime function to set
> the default value but:
>
> - getting the Lesson object through the Pool seems to return only the
> Lesson model definition and not the current value of the Lesson record
> I started from (or at least I'm not able to get those values)
> - the Transaction().context object is only filled with the locale,
> timezone, etc data and nothing about the current records
> - I couldnt figure out how into the default_StartDateTims function to
> get any ids of the current records starting from 'self'It is always bad to make default value depending of some kind of context. Especially when it requires to come from the parent record. The issue is that you duplicate the information. I think you should leave the StartDateTime of Activity empty that will mean the same as the Lesson and fill it only is there is an exception. Perhaps this case will clarify my thought, what will happen if the StartDateTime of the Lesson is changed after the creation of Activity? You need to update all Activities with the new default value. -- Cédric Krier B2CK SPRL Rue de Rotterdam, 4 4000 Liège Belgium Tel: +32 472 54 46 59 Email/Jabber: [email protected] Website: http://www.b2ck.com/
pgpy5sLKqiEfK.pgp
Description: PGP signature
