Re: Update: Date formatting between model and form output

2008-09-26 Thread Gerard Petersen

Found an error in the model code. It should be this:

if isinstance(self.period_start_date, datetime): 

Regards,

Gerard,

koenb wrote:
> On 26 sep, 16:00, Gerard Petersen <[EMAIL PROTECTED]> wrote:
>> Getting closer. This works: product.period_start_date.strftime('%d-%m-%Y')
>>
>> But I definitely do not want this in all view handlers. It should go in the 
>> model .. or the modelform.
>>
>> Thanx again!
>>
>> Gerard.
>>
>> Gerard Petersen wrote:
>>> Hi all,
>>> I'm trying to have a formfield filled with a correlctly formatted date 
>>> value.
>>> Validation is already in place. It only accepts "dd-mm-" on submitting 
>>> but when it gets the existing value from the model it shows it in the 
>>> formfield like "-mm-dd"
>>> So the sequence is almost complete. But where do I format the value so the 
>>> formfield shows the correct formatted value when editing. In the model, the 
>>> form or the view method?
>>> Thanx a lot.
>>> Gerard.
>> --
>> urls = { 'fun':  'www.zonderbroodje.nl',  'tech':  'www.gp-net.nl'}
> 
> Hi Gerard,
> 
> the DateTimeInput widget already accepts a format parameter.
> What I do is I have a subclass of ModelForm that in its init goes over
> the fields and replaces the widget for Datefields with a datetimeinput
> with the correct format. I also change the input format their (I
> prefer dd/mm/).
> 
> something like:
> 
> for field in self.fields:
> if isinstance(self.fields[field], forms.Datefield):
> self.fields[field].input_formats = settings.DATE_INPUT_FORMATS
> self.fields[field].widget = forms.DateTimeInput(format =
> settings.DATE_OUTPUT_FORMAT)
> 
> as you can see, I put the formats in my settings file, but of course
> you could just plug them right in here too.
> 
> I am not sure this is the best way to do this, but maybe this helps.
> 
> Koen
> > 

-- 
urls = { 'fun':  'www.zonderbroodje.nl',  'tech':  'www.gp-net.nl' }


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Update: Date formatting between model and form output

2008-09-26 Thread Gerard Petersen

Hi Koen,

I'm using modelform, having tried the 'output_format' there does not seems to 
work. In the mean time I figured out a solution within the model like this:

def __init__(self, *args, **kwargs):
super(Product, self).__init__(*args, **kwargs)

if not isinstance(self.period_start_date, unicode): 
self.period_start_date = self.period_start_date.strftime('%d-%m-%Y')

nb: the unicode test is for when a new empty object is invoked.

I'm not sure yet whether formatting like this belongs on the model or the form 
side.

Anyway, thanx for the response.


Regards,

Gerard.

koenb wrote:
> On 26 sep, 16:00, Gerard Petersen <[EMAIL PROTECTED]> wrote:
>> Getting closer. This works: product.period_start_date.strftime('%d-%m-%Y')
>>
>> But I definitely do not want this in all view handlers. It should go in the 
>> model .. or the modelform.
>>
>> Thanx again!
>>
>> Gerard.
>>
>> Gerard Petersen wrote:
>>> Hi all,
>>> I'm trying to have a formfield filled with a correlctly formatted date 
>>> value.
>>> Validation is already in place. It only accepts "dd-mm-" on submitting 
>>> but when it gets the existing value from the model it shows it in the 
>>> formfield like "-mm-dd"
>>> So the sequence is almost complete. But where do I format the value so the 
>>> formfield shows the correct formatted value when editing. In the model, the 
>>> form or the view method?
>>> Thanx a lot.
>>> Gerard.
>> --
>> urls = { 'fun':  'www.zonderbroodje.nl',  'tech':  'www.gp-net.nl'}
> 
> Hi Gerard,
> 
> the DateTimeInput widget already accepts a format parameter.
> What I do is I have a subclass of ModelForm that in its init goes over
> the fields and replaces the widget for Datefields with a datetimeinput
> with the correct format. I also change the input format their (I
> prefer dd/mm/).
> 
> something like:
> 
> for field in self.fields:
> if isinstance(self.fields[field], forms.Datefield):
> self.fields[field].input_formats = settings.DATE_INPUT_FORMATS
> self.fields[field].widget = forms.DateTimeInput(format =
> settings.DATE_OUTPUT_FORMAT)
> 
> as you can see, I put the formats in my settings file, but of course
> you could just plug them right in here too.
> 
> I am not sure this is the best way to do this, but maybe this helps.
> 
> Koen
> > 

-- 
urls = { 'fun':  'www.zonderbroodje.nl',  'tech':  'www.gp-net.nl' }


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: Update: Date formatting between model and form output

2008-09-26 Thread koenb

On 26 sep, 16:00, Gerard Petersen <[EMAIL PROTECTED]> wrote:
> Getting closer. This works: product.period_start_date.strftime('%d-%m-%Y')
>
> But I definitely do not want this in all view handlers. It should go in the 
> model .. or the modelform.
>
> Thanx again!
>
> Gerard.
>
> Gerard Petersen wrote:
> > Hi all,
>
> > I'm trying to have a formfield filled with a correlctly formatted date 
> > value.
>
> > Validation is already in place. It only accepts "dd-mm-" on submitting 
> > but when it gets the existing value from the model it shows it in the 
> > formfield like "-mm-dd"
>
> > So the sequence is almost complete. But where do I format the value so the 
> > formfield shows the correct formatted value when editing. In the model, the 
> > form or the view method?
>
> > Thanx a lot.
>
> > Gerard.
>
> --
> urls = { 'fun':  'www.zonderbroodje.nl',  'tech':  'www.gp-net.nl'}

Hi Gerard,

the DateTimeInput widget already accepts a format parameter.
What I do is I have a subclass of ModelForm that in its init goes over
the fields and replaces the widget for Datefields with a datetimeinput
with the correct format. I also change the input format their (I
prefer dd/mm/).

something like:

for field in self.fields:
if isinstance(self.fields[field], forms.Datefield):
self.fields[field].input_formats = settings.DATE_INPUT_FORMATS
self.fields[field].widget = forms.DateTimeInput(format =
settings.DATE_OUTPUT_FORMAT)

as you can see, I put the formats in my settings file, but of course
you could just plug them right in here too.

I am not sure this is the best way to do this, but maybe this helps.

Koen
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---