D
D
Dmitry2020-12-15 13:33:21
Django
Dmitry, 2020-12-15 13:33:21

How to override save() method in Django model?

class ValueRatingPost(models.Model):
  """ Модель оценки постов """
  user = models.OneToOneField(User, on_delete=models.CASCADE, verbose_name='Пользователь')
  rating = models.ForeignKey(Rating, on_delete=models.CASCADE, verbose_name='Оценка')
  post = models.ForeignKey(Post, on_delete=models.CASCADE, verbose_name='Запись')

class Meta:
...

def save(self, *args, **kwargs):
...
if User in self.user:
...

How to override save() with update so that the user can rearrange the rating?

I know that in the view you can do update_or_create, but I want to override save

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dooMoob, 2020-12-15
@Dmitry_97

overriding a method is easy:

def save(self, *args, **kwargs):
    # some logic
   super().save(*args, **kwargs)

But you have a problem in the logic - now the user can put only one rating, the rearrangement of the rating has nothing to do with the redefinition of save

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question