Reputation: 1191
Hi Djangonauts,
I am new to Django so please forgive any errors in logic or code
I have a accounts app that has a Profile model with a field is_verified
Now I have another app called verification. That has a model Verification and a field called verify
I want to create a logic such that when you verify the user on the verification app. The is_verified
on profile app is also marked as True
models.py for Profile
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
city = models.CharField(max_length=100)
country = models.CharField(max_length=100)
is_verified = models.BooleanField(default=False)
models.py for Verification
class Verification(models.Model):
user = models.ForeignKey(User, related_name='verified')
applied_on = models.DateTimeField(auto_now_add=True)
verify = models.BooleanField(default=False)
deny = models.BooleanField(default=False)
verified_on = models.DateTimeField()
denied_on = models.DateTimeField()
def verify_a_user(self, user):
self.verify = True
user.profile.is_verified = True
return user.profile.is_verified.save()
Is this correct? Is there a better way to execute this code
Upvotes: 0
Views: 29
Reputation: 166
Take a look at https://docs.djangoproject.com/en/2.0/topics/signals/
Either send a pre_save or post_save signal from your Verification model. https://docs.djangoproject.com/en/2.0/ref/signals/#django.db.models.signals.pre_save https://docs.djangoproject.com/en/2.0/ref/signals/#django.db.models.signals.post_save
Then register the listener function in your Profile app.
For details and example
https://docs.djangoproject.com/en/2.0/topics/signals/#connecting-to-signals-sent-by-specific-senders
Upvotes: 1