V
V
Vitaly Ananiev2022-03-18 19:43:17
Django
Vitaly Ananiev, 2022-03-18 19:43:17

How to bind pk to an attribute in a model?

I have a model how to display the pk of the user attribute in the id_num attribute in this model by default? What would be stored in id_num number id / pk under which user in the database?
Example:

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(default=?, verbose_name='id-номер', null=True, blank=True)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Nesterov, 2022-03-18
@Vitalii181190

Override the save method to update the field. Something like:
self.id_num = self.user.id

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question