my model field is not accepting "+" symbol into data. Using CharField field for model

Viewed 28

models.py

class Profile(models.Model):
    user = models.ForeignKey(User,on_delete=models.CASCADE,null=True)
    mobile = models.CharField(max_length=14,null=True)
    add = models.CharField(max_length=10,null=True)
    image = models.FileField(null=True)

    def __str__(self):
        return self.user.username 

when i give mobile = +12345

its showing in admin as mobile = 12345

after i edit and save its showing +12345

why? and which field to use for including + symbol

1 Answers

when i give mobile = +12345.

You pass an int, Django will convert this to a str, but the number has as value 12345, not +12345.

What you need to do is store it as a string, so:

Profile.objects.create(
    user=…,
    mobile='+12345',
    add=…,
    image=…
)
Related