我使用的django配置文件具有不同類型的配置文件。訪問django配置文件中的對象父項屬性
我公司的剖面模型是類似於:
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,
'[email protected]',
[self.email])
在這裏,我從用戶配置和發送激活郵件繼承公司,我想使用屬性和父類用戶和用戶配置文件的方法。它的直接父代是userprofile,而用戶與userprofile有一對一的關係。所以我試圖調用在userpofile中定義的self.activation_key
,並調用self.username,它是用戶類/表的屬性,並在self.username上獲取錯誤。
所以好像我以錯誤的方式調用self.username,那麼如何訪問self.username?任何細節都會有所幫助和讚賞。從UserProfile
如果我理解正確,您的層次結構是User - > UserProfile - > Company? – themanatuf 2012-07-31 18:42:28
@themanatuf是的,你理解正確 – Hafiz 2012-07-31 19:02:09
你可以嘗試在調用'self.activation_key'之前打印'self.username'? 'self.activation_key'方法是靜態的嗎? – themanatuf 2012-07-31 19:06:35