0

(sorry my bad english)

i have a integer field my models.py.i use this for phone numbers.and phone numbers start zero(0) or plus(+).and when i write this field:05555555 this will be display:5555555.how i do this i write 05555555 and display 0555555?i tried phone number field but working with problem.here my models.py:

from django.db import models
from django.contrib.auth.models import User

class elan(models.Model):
    cixis_yeri = models.CharField(max_length=150)
    catma_yeri = models.CharField(max_length=150)
    cixis_vaxti = models.DateField()
    catma_vaxti = models.DateField()
    elaqe_nomresi = models.IntegerField()
    sirket = models.CharField(max_length=100,blank=True,null=True)
    elave_melumatlar = models.TextField(null=True,blank=True)
    silinme_vaxti = models.DateTimeField()
    user = models.ForeignKey('auth.User',verbose_name='paylasan',on_delete=models.CASCADE)
    favorit = models.ManyToManyField(User,blank=True,related_name="favorit")

    def __str__(self):
        return self.cixis_yeri


    class Meta:
        ordering = ['-id']

forms.py:

from django import forms
from .models import elan

class elanform(forms.ModelForm):

    class Meta:
        model = elan
        fields = [
            'cixis_yeri',
            'catma_yeri',
            'cixis_vaxti',
            'catma_vaxti',
            'elaqe_nomresi',
            'elave_melumatlar',
            'sirket',
        ]

        labels = {
            'cixis_yeri':"Çıxış Yeri",
            'catma_yeri':"Çatma Yeri",
            'cixis_vaxti':"Çıxış Vaxtı",
            'catma_vaxti':"Çatma Vaxtı",
            'elaqe_nomresi':"Əlaqə Nömrəsi",
            'elave_melumatlar':"Əlavə Məlumatlar",
            'sirket':"Şirkət",
        }

        widgets = {
            'cixis_yeri': forms.TextInput(attrs = {'class':'form-control kendi','placeholder':'Olke-Seher','id':"exampleFormControlInput1"}),
            'catma_yeri': forms.TextInput(attrs = {'class':'form-control kendi','placeholder':'Olke-Seher'}),
            'cixis_vaxti': forms.DateInput(attrs = {'class':'cixis_vaxti','type':'date'}),
            'catma_vaxti': forms.DateInput(attrs = {'class':'catma_vaxti','type':'date'}),
            'elave_melumatlar': forms.Textarea(attrs = {'class':'form-control kendi','placeholder':'elave_melumatlar(Vacib Deyil)','type':'text','id':'exampleFormControlTextarea1','rows':'6','columns':'2'}),
            'sirket': forms.TextInput(attrs = {'class':'form-control kendi','placeholder':'(Vacib Deyil)','type':'text','id':'exampleFormControlTextarea1','rows':'6','columns':'2'}),
            'elaqe_nomresi': forms.NumberInput(attrs = {'class':'form-control kendi','placeholder':'Elaqe','id':"exampleFormControlInput1"}),
        }

please help me.thanks now.

Abdullah-V
  • 21
  • 1
  • 8

2 Answers2

0

Eventhough you say you have issues with the phone number field it is way better than an integer field for a phone number. With a plain integer field you won't be able to store any regional code, prefix such as (+ or #).

Here is another question that will give you the best practice on phone numbers in django. There you have the github related to the solution.

If you really need the number to stay an integer you could store 2 fields, one being the phone number as an integer and one being the prefix as character field and then concatenate them (both as strings) to get the full number.

This solution doesn't seem too pretty to me and it should defenitely not be used for commercial or sensitive coding.

Jao
  • 558
  • 2
  • 12
  • Actually, no.It doesn't need to be complete. I actually looked at the question in the link you gave, but it worked a little problematically (I think it originated from me). In addition, I found 2 solutions: For example the user entered +123456789 and django displayed it as 123456789. I add + to it in my templatem. If I want to add it to the database, I add it with concat. Or I put my own charfileld and write my own validator. (ex.if blabla in "abcdefht ..." ). I will try the source you have given once again. I hope it will be useful. – Abdullah-V Sep 07 '20 at 09:18
  • An integer will clear the "0" or "+" put in front of it. When I said concatenate I should have precised you should do it as strings to print both fields. – Jao Sep 07 '20 at 09:22
  • why @hedgie?i dont know.if this is important i change now – Abdullah-V Sep 07 '20 at 14:39
0

You shouldn't use integerField... instead use CharField and set a validator function

Phone = Models.CharField(max_length=15 , null=False,validators=[phone_validate])


def phone_validate(value):
    max_length = 12
    min_length = 8
    if value.isdigit==True and len(value)>min_length and len(value)<max_length :
       return value
    else:
       raise ValidationError("phone number must be 9-11 digit")