Tuesday, February 21, 2012

Re: I am getting TypeError: coercing to Unicode: need string or buffer, long found. Please help.

On Tue, Feb 21, 2012 at 12:18 PM, Daniel Marquez <daniel.marquez0112@gmail.com> wrote:
Wow, I should've caught that. Thanks guys. However, since I needed a
string, what I did was add "default=x" to the integer field as
follows:

class Phone(models.Model):
       phonenumber = models.IntegerField(default=10)
       pub_date = models.DateTimeField('date published')
       def __unicode__(self):
               return self.phonenumber

and it worked.


As mentioned earlier, your phone number field should not be an integer. Among other reasons, using a string allows you to validate for different countries, have extensions, and more easily manipulate and display the value everywhere you use it (you're never going to use it *as* an integer, after all).

Also, setting an integer field to have a default value other than zero smells really bad.

Shawn 

--
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 django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment