S
S
sazhyk2016-11-29 12:25:24
Django
sazhyk, 2016-11-29 12:25:24

How to store and display a list of numbers in the database?

There is a model, let it be Person . This model has a phone_number field . In the model it looks like this

class Person(models.Model):
    ...
    phone_number = models.CharField(
        ...
        validators=[
             RegexValidator(
                 regex=u'^[0-9\(\)\-\+,\'\s]+$',
                 message=u'Это поле может содержать только номер телефона в формате "+7(999)999-99-99"',
                 code='invalid_symbols'
             )
         ],
    )
    ...

There is also a model form that is displayed on the page.
On the user side, javascript adds several phones to the . And as a result, the entry in the database looks like this:
'+7(231)242-34-12', '+7(123)412-34-12',
Accordingly, this is a string in the database. If you get it, then there are
"'+7(231)242-34-12', '+7(123)412-34-12',"
actually several questions:
1. Is it correct to save data in this way (there can be several phones)?
2. And if the answer to the first question is "yes", then how to get a list from the string so that you can make a drop-down list from it in the template in a cycle (the form is rather complicated and cumbersome, and I decided to display all the fields separately in the template)?
3. Maybe I'm doing everything wrong and there are much simpler ways to store such data.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sim3x, 2016-11-29
@sazhyk

#
class Person(Model):
  name = ...
  ....

class Phone(Model):
  person = ForeignKey(Person)
  phone = PhoneNumber()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question