database - How to synchronize tables in Django? -
in database want synchronize 2 tables. use auth_user(default table provided django) table registration , there table user-profile contain entities username, email, age etc. how autometically upadate columns username , email in user-profile according updation in auth_user table.
from django.contrib.auth.models import user class profile(models.model): username = models.charfield(max_length = 30) email = models.emailfield() age = models.positiveintegerfield() auth_user_id = models.foreignkey(user)
you can either modify django registration code , include code save profile model on every new registration.
or
you can set signal on every save of user model. see documentation.
def create_profile(sender, **kwargs): if kwargs["created"]: p = profile(user=kwargs["instance"], ...) p.save() django.db.models.signals.post_save.connect(create_profile, sender=user)
create_profile() called every time user object saved. in example, create profile object if new user instance has been created. can modify change existing profile objects on every change in user instance, also.
Comments
Post a Comment