I have to add a progress_level field to my User model that shows where does this user stand at the moment so I added a field to the user model like this:
progress_level = models.CharField(max_length=25, null=True, choices=USER_PROGRESS_LEVELS)
I want this field to automatically update itself whenever an action gets done by the user. for instance, if the user completes their contact info and document info and submit their forms, the progress level should change to [1][0]. I really dont know how to do this but I created a signal like this:
@receiver(pre_save, sender=User, dispatch_uid='progress_change')
def progress(sender, instance, **kwargs):
user = instance
if DocumentInfo.objects.filter(user=user).exists() and ContactInfo.objects.filter(user=user).exists():
user.progress_level = USER_PROGRESS_LEVELS[1][0]
it works fine but it activates only if I save the User model. how can I prevent that? how can I activate this signal whenever that statement is true? if is there a better way to do this please help me. I really dont know if signals are the right way to do this. these are my models:
class User(AbstractUser):
id = models.AutoField(primary_key=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
isPreRegistered = models.BooleanField(default=False)
username = models.CharField(unique=True, max_length=13)
first_name = models.CharField(max_length=32, null=True, default=None)
last_name = models.CharField(max_length=64, null=True, default=None)
gender = models.CharField(max_length=10, null=True, choices=GENDER)
progress_level = models.CharField(max_length=25, null=True, choices=USER_PROGRESS_LEVELS)
isDetailViewed = models.BooleanField(default=False)
class DocumentInfo(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
certificate = models.FileField(upload_to="documents")
id_card = models.FileField(upload_to="documents")
service_certificate = models.FileField(upload_to="documents")
educational_certificate = models.FileField(upload_to="documents")
CodePudding user response:
How about updating the model save method?
models.py
class User(...):
...
document_info = models.OneToOneField(DocumentInfo, ...)
contact_info = models.OneToOneField(ContactInfo, ...)
def save(self, *args, **kwargs):
if self.document_info and self.contact_info:
self.progress_level = USER_PROGRESS_LEVELS[1][0]
return super().save(*args, **kwargs)
CodePudding user response:
You want change the progress_level value where you add some document or complete contact ... how about you do this in save or update method on document model or contact model ... contact or document model that related with specific user have save or update method can effect "progress_level" value on user model trough user instance. you can fetch user instance from document model on save method (cause you have foreign key) and check the process has been don ... increase "progress_level" by user instance. if you don't know how in code let me know to explain that with sample code
