String Concatenate

35 views
Skip to first unread message

Ankit Chaurasia

unread,
Apr 20, 2022, 9:33:20 AM4/20/22
to Django users
Hello Team,
I need some help,

Please see below my Model.

from django.db import models
class Customer(models.Model):
    name = models.CharField(max_length=20)
    phone = models.IntegerField()
   
I want to concatenate name + last digit of phone number.


   
    

Kasper Laudrup

unread,
Apr 20, 2022, 10:05:33 AM4/20/22
to django...@googlegroups.com
On 20/04/2022 11.33, Ankit Chaurasia wrote:
> from django.db import models
> class Customer(models.Model):
>     name = models.CharField(max_length=20)
>     phone = models.IntegerField()
> I want to concatenate name + last digit of phone number.
>

It makes no sense to store a phone number as an integer. A phone number
is not something you can increment, decrement or do similar arithmetic
operations with.

Just use a CharField or probably even better, a field dedicated to phone
numbers:

https://pypi.org/project/django-phonenumber-field/

Using a more proper field type will make it simple to do the
concatenation using standard Python.

Kind regards,

Kasper Laudrup
OpenPGP_0xE5D9CAC64AAA55EB.asc
OpenPGP_signature

Shaheed Haque

unread,
Apr 20, 2022, 5:44:38 PM4/20/22
to django...@googlegroups.com


On Wed, 20 Apr 2022, 11:05 Kasper Laudrup, <lau...@stacktrace.dk> wrote:
On 20/04/2022 11.33, Ankit Chaurasia wrote:
> from django.db import models
> class Customer(models.Model):
>      name = models.CharField(max_length=20)
>      phone = models.IntegerField()
> I want to concatenate name + last digit of phone number.
>

It makes no sense to store a phone number as an integer. A phone number
is not something you can increment, decrement or do similar arithmetic
operations with.

Not only that, but it is all too easy to lose the leading zeroes that are often part of a phone number. Kasper is right: don't go there. (I won't even mention about leading plus signs...) 


Just use a CharField or probably even better, a field dedicated to phone
numbers:

https://pypi.org/project/django-phonenumber-field/

Using a more proper field type will make it simple to do the
concatenation using standard Python.

Kind regards,

Kasper Laudrup

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/789404f7-e665-d102-1dee-406cbfcfe4cd%40stacktrace.dk.

Jason

unread,
Apr 20, 2022, 7:12:41 PM4/20/22
to Django users
Reply all
Reply to author
Forward
0 new messages