V
V
Vitaly Ananiev2022-03-19 20:30:48
Django
Vitaly Ananiev, 2022-03-19 20:30:48

Why is the number of users in the DB not the same as the number of User.pk? (Signals)?

I have a Customer model:

class Customer(models.Model):
    user = models.ForeignKey(User, verbose_name='Пользователь', on_delete=models.CASCADE, blank=True, null=True)
    avatar = models.ImageField("Аватар", blank=True, null=True)
    date = models.DateField("Дата создания", auto_now=True, auto_now_add=False, blank=True, null=True)
    id_num = models.PositiveIntegerField(verbose_name='id-номер', null=True, blank=True)

and also there is a function that, when saving a new user in User, adds id_num = User.pk to the attribute
@receiver(post_save, sender=User)
def created_user_profile(sender, instance, created, **kwargs):
    if created:
        Customer.objects.create(user=instance, id_num=instance.pk)


@receiver
def save_user_profile(sender, instance, **kwargs):
    instance.profile.save()

Why do I have 6 users in the database and when adding a new user to id_num = 76... and not 7?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
Alexander Nesterov, 2022-03-19
@Vitalii181190

Read about how auto- increment works in a database (or rather, a whole course / book on database and sql architecture).

D
Dr. Bacon, 2022-03-19
@bacon

If id_num means the client's serial number and it must be without gaps, then you definitely cannot use an id based on autoincrement for this, the order must be guaranteed on your own. A lot of people stepped on it, you're another.

V
Vitaly Ananiev, 2022-03-19
@Vitalii181190

Dr. Bacon , Yes, I'm aware, I wanted to make a binding by slug, but I don't know how, when adding, let's say from the username field to the slug field, the entered data from the user would be automatically translated into English characters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question