Il giorno Thu, 10/11/2011 alle 17.09 +0100, Cédric Krier ha scritto:
> 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.
right
> 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.
good strategy.
> 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.
>
right again. Practically it should not happen, we're talking about
classromm lessons, so data about people attending the lesson MUST be
inserted AFTER the lesson has taken place and at that moment the time of
the lesson IS surely known. But there is always the chance of human
error in data entry. And in case of modification of the start/stop time
of the lesson there is always the need to change the exceptions, even in
the store-only-exceptions scenario
I think that the best solution is to substitute the Activity DateTime
fields with a Time field (float_time widget) to store the amount of time
spent by the person at lesson (where NULL = the whole lesson), so any
change in the Lesson start/stop won't require any change into the
activity. There will be a slight loss of information (the exact time
span start/stop) but it was redoundant since the real needed information
is the amount of time.
It just remains the technical issue behind the original problem (i deem
that i will have the need of this knowledge in the near future...). Can
you point me in the right direction about how I can access record data
from within the default_<field name> function?
thanks as usual
leo
--
vuk di Leonardo Venturini
web developement
via Zandonai, 1 - 24129 Bergamo (BG)
Italy
P.IVA 03084840168
cell 339.6132096
http://vuk.bg.it
--
[email protected] mailing list