djangodjango-modelsdjango-profiles

Accessing objects parent property in django profiles


I am using django profiles with having different types of profiles.

My Profile Model of Company is similar to:

from django.db import models
from django.contrib.auth.models import User
from accounts.models import UserProfile
from jobs.models import City
from proj import settings
from django.core.mail import send_mail

class Company(UserProfile):
    name=models.CharField(max_length=50)
    short_description=models.CharField(max_length=255)
    tags=models.CharField(max_length=50)
    profile_url=models.URLField()
    company_established=models.DateField(null=True,blank=True)
    contact_person=models.CharField(max_length=100)
    phone=models.CharField(max_length=50)
    address=models.CharField(max_length=200)
    city=models.ForeignKey(City,default=True)

    def send_activation_email(self):
        self.set_activation_key(self.username)
        email_subject = 'Your  '+settings.SITE_NAME+' account confirmation'
        email_body = """Hello, %s, and thanks for signing up for an                                                                                            
example.com account!\n\nTo activate your account, click this link within 48                                                                                       
hours:\n\nhttp://"""+settings.SITE_URL+"/accounts/activate/%s""" % (self.username,self.activation_key)
        send_mail(email_subject,
                  email_body,
                  'acccounts@site.com',
                  [self.email])

Here I am inheriting Company from UserProfile and in send activation email, I am trying to use properties and methods of parent classes user and userprofile. Its direct parent is userprofile while user has onetoone relation with userprofile. So I tried to call self.activation_key that is defined in userpofile and called self.username that is property of user class/table and getting error on self.username .

So seems like I am calling self.username in wrong way,so how can I access self.username ? Any detail will be helpful and appreciated.


Solution

  • Company inherits from UserProfile, but as you say UserProfile doesn't inherit from User - it has a OneToOne relationship with it. So there's no such thing as self.username - there's simply a relationship from self to User.username. I'm going to guess that that relationship is via a field called user, in which case you want self.user.username.