Re: Error when I use datetime.now()

2007-10-20 Thread Dirk Eschler

On Samstag, 20. Oktober 2007, Greg wrote:
> Hello,
> I have a class called Orders that tries to add the current date to
>
> it's comments field when it's saved.  Here is my code:
> >from datetime import datetime
>
> class Order(models.Model):
> comments = models.TextField("Comments", maxlength=1000)
> etc...
>
> def save(self):
> self.comments += "This is a string - " + datetime.now() + "
> This is a string "
>
> 
>
> Whenever, I save my order class I get the following error:
>
> TypeError at /admin/plush/order/1/
> cannot concatenate 'str' and 'datetime.datetime' objects
>
> Any Suggestions?

Hi Greg,

you can also concatenate the strings like that:
self.comments = "%s abc %s abc" % (self.comments, datetime.now())

It does a typecast on the datetime object for you and is in general faster 
than using the plus operator.

Best Regards,
Dirk Eschler

-- 
Dirk Eschler 
http://www.krusader.org

--~--~-~--~~~---~--~~
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: Error when I use datetime.now()

2007-10-19 Thread Michael Newman

try datetime.now().__str__() or if you want to make it pretty try
datetime.now().strftime(YourCustomFormatHere). Look at
http://docs.python.org/lib/strftime-behavior.html#strftime-behavior

Your problem is a datetime object is a not a string. Both those above
examples turn it into a string.

On Oct 19, 11:22 pm, Greg <[EMAIL PROTECTED]> wrote:
> Hello,
> I have a class called Orders that tries to add the current date to
> it's comments field when it's saved.  Here is my code:
>
> from datetime import datetime
>
> class Order(models.Model):
> comments = models.TextField("Comments", maxlength=1000)
> etc...
>
> def save(self):
> self.comments += "This is a string - " + datetime.now() + "
> This is a string "
>
> 
>
> Whenever, I save my order class I get the following error:
>
> TypeError at /admin/plush/order/1/
> cannot concatenate 'str' and 'datetime.datetime' objects
>
> Any Suggestions?


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



Error when I use datetime.now()

2007-10-19 Thread Greg

Hello,
I have a class called Orders that tries to add the current date to
it's comments field when it's saved.  Here is my code:

from datetime import datetime

class Order(models.Model):
comments = models.TextField("Comments", maxlength=1000)
etc...

def save(self):
self.comments += "This is a string - " + datetime.now() + "
This is a string "



Whenever, I save my order class I get the following error:

TypeError at /admin/plush/order/1/
cannot concatenate 'str' and 'datetime.datetime' objects

Any Suggestions?


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---