Python Forum
Insert data in a table after a user is created from djando admin
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert data in a table after a user is created from djando admin
#1
I am a fresher in django. I am using django 1.10 with allauth app which takes care of frontend registration, sending user confirmation email,validation, login with email etc. So it all works in the frontend. In the backend I managed to change the user creation form by adding an extra field email.

The allauth app inserts 3 rows in 3 tables when a user is created in the frontend.

1. auth_user
2. account_emailaddress
3. account_emailconfirmation

When adding a user from admin it creates a row in auth_user and general_userprofile(extending django user model) table. I would like to insert a row in account_emailaddress table whenever a user is created via admin.

Fields in account_emailaddress are--

id
email
verified
primary
user_id

models.py --

from __future__ import unicode_literals
from django.db import models
from django.contrib.auth.models import User
from django.utils.translation import ugettext as _
from django.db.models.signals import post_save
from django.dispatch import receiver
from ciasroot.util import HashedPk
from phonenumber_field.modelfields import PhoneNumberField
import math, decimal, datetime, os

User._meta.get_field('email').blank = False
User._meta.get_field('email')._unique = True


class EmailAddress(models.Model):
    verified = models.BooleanField(verbose_name=_('verified'), default=True)
    primary = models.BooleanField(verbose_name=_('primary'), default=True)

class UserProfile(models.Model, HashedPk):
    user = models.OneToOneField(User, unique=True, related_name ='profile')
    job_title = models.CharField(max_length=128, blank=True, null=False, default="")
    website = models.URLField(max_length=255, blank=True, null=True)
    organisation = models.CharField(max_length=50, blank=True, null=True, default="")
    phone_number = PhoneNumberField( blank=True, null=True)

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)


def __str__(self):
    return self.user.get_full_name()

def save(self, *args, **kwargs):
    super(UserProfile, self).save(*args, **kwargs)
How can I fetch userid and email under class EmailAddress and save it to the table.

Any help is highly appreciated.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Scraping data from table into existing dataframe vincer58 1 1,960 Jan-09-2022, 05:15 PM
Last Post: vincer58
  Inserting data from a table to another (in same db) firebird 5 2,425 Oct-05-2020, 06:04 AM
Last Post: buran
  Extract data from a table Bob_M 3 2,627 Aug-14-2020, 03:36 PM
Last Post: Bob_M
  Scraping a dynamic data-table in python through AJAX request filozofo 1 3,823 Aug-14-2020, 10:13 AM
Last Post: kashcode
  Django admin login problem erfanakbari1 0 2,023 Dec-29-2019, 12:29 PM
Last Post: erfanakbari1
  Table data with BeatifulSoup gerry84 11 7,083 Oct-23-2019, 10:09 AM
Last Post: Larz60+
  Want to scrape a table data and export it into CSV format tahir1990 9 5,132 Oct-22-2019, 08:03 AM
Last Post: buran
  Using flask to add data to sqlite3 table with PRIMARY KEY catafest 1 3,703 Sep-09-2019, 07:00 AM
Last Post: buran
  sending email from admin to user in django anjana 0 3,220 Jun-07-2019, 12:01 PM
Last Post: anjana
  sqlalchemy DataTables::"No data available in table" when using self-joined table Asma 0 2,550 Nov-22-2018, 02:46 PM
Last Post: Asma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020