How to combine user management with a model

I am doing a Django project. It is supposed to serve as a platform to connect blood donors and seekers where both the donors and seekers will register themselves and search each other.

Part-I: I started it with just 1-app, and a model named Profile. I used these fields; Name, Blood_Type (choices: A+, A- …O-), Contact_Number, and User_Type(choices: Donor, Seeker). Using appropriate URLs, views, and templates, I managed to create registration and search functionalities.

But I also needed sign up, login and logout functions (user management)

Part-II: For user management, I found allauth pretty handy with much work already done. So, in a separate project, I managed to implement user management through allauth.

Challenge: The challenge is to combine Parts I and II. Thus visitors should be able to sign up (Part-I), and complete their profiles (Part-II).
Sign up takes username, email, and password only. Whereas Profile models takes Names, Blood_Type and Contact_Numbers. I should do something so that as soon as a visitor signs up, his username is connected to Name field of Profile model. Then an authenticated user is offered to complete their profile. Or is there a better design option? Please explain this to me as if I am only a beginner in Django.

Best Regards.

Yes, implement the AllAuth in your primary project and add it to Part-I

Thank you KenWhitesell. Elaborate a bit further please. I cannot figure out how to make the Name field of Profile model correspond to the username field of the allauth, so that whenever a new user signs up through allauth, it is also added to Name field of the Profile model?

You don’t. There’s no need to duplicate that information between the two.

Now, I don’t use and don’t know anything specifically about Django allauth, so I can’t address the details. But in general, your Profile model would be created with a OneToOneField relationship with the User model. When the User model is created, you can create the Profile model at that same time, referring to the User model that has just been created.

  1. [quote=“KenWhitesell, post:4, topic:29533”]
    Profile model would be created with a OneToOneField relationship with the User model.
    [/quote] Will my Profile model automatically be linked with a OneToOneField relationship with the User model? How does Django know to link my custom Profile model with User model?
  2. [quote=“KenWhitesell, post:4, topic:29533”]
    When the User model is created, you can create the Profile model at that same time, referring to the User model that has just been created.
    [/quote]. I cannot figure out exactly how to carry out this.

Not “automatically”, no. That’s why I wrote:

and also:

Superficially, I would think you could use either the new_user adapter function (Advanced Usage — django-allauth 0.61.1 documentation) or catch the user_signed_up signal to have your code run when a user is created.

However, someone else who knows more about allauth than I do would have to answer that question definitively.

Ok. Thank you KenWhitesell. Let me give it a try and I see what I can do.

I did this, and here is how;
Here is models.py

from django.db import models
from django.contrib.auth.models import User

# Create your models here.
class Profile(models.Model):
    USER_TYPE_CHOICES = (
        ('Donor', 'Donor'),
        # first is actual value, second is human-readable placeholder
        ('Seeker', 'Seeker'),
    )
    BLOOD_GROUP_CHOICES = (
        ('A+','A+'),
        ('A-','A-'),
        ('AB+','AB+'),
        ('AB-','AB-'),
        ('B+','B+'),
        ('B-','B-'),
        ('O+','O+'),
        ('O-','O-'),
    )
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    name = models.CharField( max_length=15)
    blood_type = models.CharField(max_length=3, choices=BLOOD_GROUP_CHOICES, blank = True, null=True)
    user_type = models.CharField(max_length=10, choices=USER_TYPE_CHOICES, null=True, blank=True)
    contact_number = models.IntegerField(null=True, blank=True)

    def __str__(self):
        return str(self.name)

Then I created signals.py in the same app.

from django.dispatch import receiver
from allauth.account.signals import user_signed_up
from .models import Profile

@receiver(user_signed_up)
def create_profile(user, **kwargs):
    Profile.objects.create(user=user, name=user.username)

And here is my apps.py;

from django.apps import AppConfig


class DashboardConfig(AppConfig):
    default_auto_field = 'django.db.models.BigAutoField'
    name = 'dashboard'

    def ready(self):
        import dashboard.signals  # Import signals module

And it is doing as expected.
Thank you KenWhitesell for guiding me again.