3

I am using graphql in my project and want to generate token while registration. Even though django-graphql-auth has all the written mutation, it uses a different user model. But I want to use my custom user model. What should I do? This is my user model

class CustomUser(AbstractUser):
"""
Customized User Model
"""
email = models.EmailField(blank=True, null=True,
                          max_length=256, unique=True)
mobile_no = models.CharField(
    max_length=20, blank=True, null=True, unique=True)
verification_code = models.CharField(max_length=6)
code_valid_till = models.DateTimeField(blank=True, null=True)

timezone = models.CharField(max_length=40, blank=True, null=True)
country = models.CharField(max_length=20, default='BD', choices=COUNTRIES)

pin_verified = models.BooleanField(default=False)
email_verified = models.BooleanField(default=False)

modified_on = models.DateTimeField('date modified', auto_now=True)
created_on = models.DateTimeField(auto_now_add=True)

# For password reset
reset_verification_code = models.CharField(
    max_length=6, blank=True, null=True)
reset_code_valid_till = models.DateTimeField(blank=True, null=True)
reset_request = models.BooleanField(default=False)
reset_valid_till = models.DateTimeField(blank=True, null=True)

class Meta:
    verbose_name_plural = "User"

def __str__(self):
    if not self.is_anonymous:
        return "{} - {}".format(self.mobile_no, self.email)
    else:
        return "Anon"

@property
def get_full_name(self):
    return super().get_full_name()

def get_all_permissions(self, obj=None):
    return super().get_all_permissions(obj=obj)

def send_email(self, subject, message, to_email: list, from_email=None, **kwargs):
    send_mail_to_user(subject, message, from_email, to_email, **kwargs)
Booshra
  • 63
  • 7

1 Answers1

1

I'm the author of the package. Now the documentation site has a custom user model in the quickstart, you can see it here. Currently, it's not documented how to use it with a custom user model, but it is already an open issue, you can see it here. I will paste the same answer that is on the Github.

From Django docs:

Changing to a custom user model mid-project

Changing AUTH_USER_MODEL after you’ve created database tables is significantly more difficult since it affects foreign keys and many-to-many relationships, for example.

So, make sure to create the custom user model when you start your project.

Add the following to your custom user model

Following the Django custom user model.

from django.contrib.auth.models import AbstractUser

class CustomUser(AbstractUser):
    # ...
    USERNAME_FIELD = "<username_filed_name>" # e.g: "username", "email"
    EMAIL_FIELD = "<email_field_name>" # e.g: "email", "primary_email"

Add your custom user model in the settings

See here for more info.

# settings.py

AUTH_USER_MODEL = 'myapp.CustomUser'

Please let me know if you have further questions :)


Edit: Add the mobile_no in the registration

Use the REGISTER_MUTATION_FIELDS or REGISTER_MUTATION_FIELDS_OPTIONAL.

# settings.py


GRAPHQL_AUTH = {
    # ...

    REGISTER_MUTATION_FIELDS = [
        # ...
        'mobile_no'
    ]

    # or
    REGISTER_MUTATION_FIELDS_OPTIONAL = ['mobile_no']

    # You can set the graphene base scalars:
    REGISTER_MUTATION_FIELDS = {
        "email": "String",
        "username": "String",
        "mobile_no": "String",
    }
}

pedrobern
  • 1,134
  • 9
  • 24
  • In my user model I wanted to take mobile_no as input as well as email while registering a new user. I tried to edit the code of your package but wasn't able to achieve the output. I am quite new at graphql, it would be great if you could help me out. I have added my user model in my question. – Booshra Mar 28 '20 at 10:38
  • I don't have "email" field in my custom User Model can I not use graphql auth ? – stackoverflow rohit Feb 27 '21 at 20:25
  • set the username as the email field then. – pedrobern Feb 28 '21 at 14:28