覆盖Django配置文件';纵断面图

覆盖Django配置文件';纵断面图,django,django-profiles,Django,Django Profiles,我安装了django配置文件/注册,一切似乎都很好。当用户注册时,也会创建他们的配置文件。现在我想做的是根据用户的用户id查询另一个模型,即Company。我不想更改django profiles视图,但在URL上添加额外字段以匹配和查询公司模型。当我硬编码url时(例如:输入userprofile的id号,如so userprofile=1,它可以工作)。因此,当用户登录并转到配置文件详细信息页面时,将根据其user.id查询分配给他们的公司 class UserProfile(models.

我安装了django配置文件/注册,一切似乎都很好。当用户注册时,也会创建他们的配置文件。现在我想做的是根据用户的用户id查询另一个模型,即Company。我不想更改django profiles视图,但在URL上添加额外字段以匹配和查询公司模型。当我硬编码url时(例如:输入userprofile的id号,如so userprofile=1,它可以工作)。因此,当用户登录并转到配置文件详细信息页面时,将根据其user.id查询分配给他们的公司

class UserProfile(models.Model):
    user = models.OneToOneField(User)
    #email = models.CharField(max_length=200, blank=True, null=True)
    # Other fields here
    #company = models.ForeignKey(Company,blank=True,null=True)    
    #office = models.CharField(max_length=200, blank=True, null=True)    
    def __unicode__(self):
        return self.user.username




class Company(models.Model):
    userprofile = models.ForeignKey(UserProfile, null=True, blank=True)
    comp_name = models.CharField(max_length=200,blank=True,null=True)
    comp_address = models.CharField(max_length=200,blank=True, null=True)
    comp_email = models.CharField(max_length=200,blank=True, null=True)
    comp_zip = models.IntegerField(blank=True, null=True)
    comp_phone = models.IntegerField(blank=True, null=True)
    comp_city = models.CharField(max_length=200,blank=True, null=True)
    #comp_state = models.USStateField(blank=True, null=True
    comp_state = models.CharField(blank=True, max_length=2)
    compwebsite = models.URLField(max_length=200, blank=True, null=True)
    twitterurl = models.URLField(max_length=200, blank=True, null=True)
    facebookurl = models.URLField(max_length=200, blank=True, null=True)
    def __unicode__(self):
        return self.comp_name



url(r'^profiles/(?P<username>\w+)/$', 'profiles.views.profile_detail', {'extra_context':{'queryset':Company.objects.filter(userprofile=request.user.id)}},),
类用户配置文件(models.Model):
用户=模型。OneToOneField(用户)
#email=models.CharField(最大长度=200,空白=True,空=True)
#这里还有其他领域
#company=models.ForeignKey(company,blank=True,null=True)
#office=models.CharField(最大长度=200,空白=True,空=True)
def ___; unicode(自):
返回self.user.username
公司类别(型号.型号):
userprofile=models.ForeignKey(userprofile,null=True,blank=True)
comp_name=models.CharField(max_length=200,blank=True,null=True)
comp_address=models.CharField(最大长度=200,空白=True,空=True)
comp_email=models.CharField(最大长度=200,空白=True,空=True)
comp_zip=models.IntegerField(blank=True,null=True)
comp_phone=models.IntegerField(空=真,空=真)
comp_city=models.CharField(max_length=200,blank=True,null=True)
#comp_state=models.USStateField(空=真,空=真
comp_state=models.CharField(blank=True,max_length=2)
compwebsite=models.URLField(最大长度=200,空白=True,空=True)
twitterurl=models.URLField(最大长度=200,空白=True,空=True)
facebookurl=models.URLField(最大长度=200,空白=True,空=True)
def ___; unicode(自):
返回self.comp_名称
url(r“^profiles/(?P\w+/$”,“profiles.views.profile_detail”,{'extra_context':{'queryset':Company.objects.filter(userprofile=request.user.id)},),

您可能希望从视图内部调用它

from *** import profile_detail

def my_view(request, username):
    extra_context = {}
    return profile_detail(request, queryset=Company.objects.filter(userprofile=request.user.id),
                       template_name="my_template.html",
                       paginate_by=20,
                       extra_context=extra_context)

看这里,我没有在视图中调用它的原因是因为它是一个第三方应用程序=>“Django profiles”。事实证明,通过调用user.userprofile.company_set.all,我的问题很容易解决。你可以在urlconf本身中这样做吗?不,不在URL中,在模板中。啊,是的,这也可以。我不太确定这与你的问题有关。