2012-07-31 57 views
1

我使用的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

+0

如果我理解正确,您的层次结构是User - > UserProfile - > Company? – themanatuf 2012-07-31 18:42:28

+0

@themanatuf是的,你理解正确 – Hafiz 2012-07-31 19:02:09

+0

你可以尝试在调用'self.activation_key'之前打印'self.username'? 'self.activation_key'方法是静态的吗? – themanatuf 2012-07-31 19:06:35

回答

0

Company继承,但就像你说的UserProfile不从User继承 - 它有它OneToOne关系。所以没有这样的东西,如self.username - 从selfUser.username只有一个关系。我猜猜这种关系是通过一个叫做user的字段来实现的,在这种情况下,你需要self.user.username

+0

谢谢丹尼尔,你的猜测工作:) – Hafiz 2012-07-31 23:02:50