Skip to content

Custom User Model Breaking Profile Functionality #34

@zetas

Description

@zetas

With Django 1.5 the profile functionality was deprecated in favor of the new custom user model implementation. In my app, I'm using a custom user model for authentication. Currently I'm using Django 1.6.2

When trying to run the example code for stripe:

class UpgradeView(View):
    form_class = StripePaymentForm
    template = 'account/checkout.html'

    def get(self, request, *args, **kwargs):
        return render(request, self.template, {'form': self.form_class()})

    def post(self, request, *args, **kwargs):
        zebra_form = StripePaymentForm(request.POST)
        if zebra_form.is_valid():
            my_profile = request.user.get_profile()
            stripe_customer = stripe.Customer.retrieve(my_profile.stripe_customer_id)
            stripe_customer.card = zebra_form.cleaned_data['stripe_token']
            stripe_customer.save()

            my_profile.last_4_digits = zebra_form.cleaned_data['last_4_digits']
            my_profile.stripe_customer_id = stripe_customer.id
            my_profile.save()
        return render(request, self.template, {'form': zebra_form})

I get an exception saying my custom user model doesn't have the method "get_profile()". Do I need to add some mixins to my user class or just use a profile model anyway?

Here's a copy of my user model and it's manager just in case, note the inclusion of data that would normally be in the profile:

class WUserManager(BaseUserManager):
    def create_user(self, email, password=None):
        """
        Creates and saves a User with the given email and password.
        """
        if not email:
            raise ValueError('Users must have an email address')

        user = self.model(
            email=self.normalize_email(email),
        )

        user.set_password(password)
        user.save(using=self._db)
        return user

    def create_superuser(self, email, password):
        """
        Creates and saves a superuser with the given email and password.
        """
        user = self.create_user(email,
            password=password,
        )
        user.is_admin = True
        user.save(using=self._db)
        return user


class WUser(AbstractBaseUser):
    email = models.EmailField(
        verbose_name='email address',
        max_length=255,
        unique=True,
        db_index=True,
    )

    first_name = models.CharField(max_length=100, null=True, blank=True)
    last_name = models.CharField(max_length=150, null=True, blank=True)
    address = models.CharField(max_length=200, null=True, blank=True)
    city = models.CharField(max_length=100, null=True, blank=True)
    state = models.CharField(max_length=50, null=True, blank=True)
    zip = models.PositiveIntegerField(max_length=5, null=True, blank=True)
    account_type = models.CharField(max_length=20)
    created = models.DateTimeField(auto_now_add=True)
    is_active = models.BooleanField(default=True)
    is_admin = models.BooleanField(default=False)

    objects = WUserManager()

    USERNAME_FIELD = 'email'
    REQUIRED_FIELDS = []

    def get_full_name(self):
        # The user is identified by their email address
        if self.first_name and self.last_name:
            return self.first_name + ' ' + self.last_name

        return self.email

    def get_short_name(self):
        # The user is identified by their email address
        if self.first_name:
            return self.first_name

        return self.email.split('@')[0]

    def get_license_status(self):
        try:
            license = self.license
        except License.DoesNotExist:
            license = None

        if license is not None and isinstance(license, License):
            return True

        return False

    # On Python 3: def __str__(self):
    def __unicode__(self):
        return self.email

    def has_perm(self, perm, obj=None):
        return True

    def has_module_perms(self, app_label):        
        return True

    @property
    def is_staff(self):
        return self.is_admin

    class Meta:
        verbose_name = 'User'
        verbose_name_plural = 'Users'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions