Reputation: 141
Well, I have a VagasUsuarios
model and a Questionario
model. I would like that when I updated the Questionario.pontuacao_questionario
field via django admin, my other VagaUsuarios.pontuacao_vaga
field would be updated as well. Is there a way to do this?
thanks for listening =)
My Models:
class Questionario(models.Model):
usuario = models.ForeignKey(Contas, on_delete=models.CASCADE)
[...]
pontuacao_questionario = models.DecimalField(max_digits=5, decimal_places=2, null=True, blank=True,verbose_name="Pontuacao do Questionário")
class VagasUsuarios(models.Model):
usuario = models.ForeignKey(Contas, on_delete=models.CASCADE)
[...]
pontuacao_vaga = models.DecimalField(max_digits=5, decimal_places=2, verbose_name="Pontuacao da Vaga")
Upvotes: 0
Views: 1530
Reputation: 2225
You could do this with signals.
Example:
from django.db.models.signals import post_save
from django.dispatch import receiver
from myapp.models import Questionario, VagasUsuarios
@receiver(post_save, sender=Questionario)
def my_handler(sender, instance, **kwargs):
obj = VagasUsuarios.objects.get(...)
obj.pontuacao_vaga = instance.pontuacao_questionario
obj.save()
Another is to override the save()
(or rather clean()
) method of your model and when it gets updated to fetch all the relevant VagasUsuarios
-objects you want to update and update them.
Note on clean()
: You got to call the clean-method yourself unless you are using the Django admin.
Upvotes: 3