将多个模型关系与django

时间:2017-11-10 08:52:16

标签: django

我有一个带有自定义用户模型的项目,用户可以登录并创建公司,这一切都运行正常。现在,我希望项目中的其他所有内容与他们创建的公司相关联,即该公司的潜在客户。将用户与公司联系起来的第一层效果很好,但现在结合第三层似乎无法正常工作。这是代码,然后我想要它做什么。

我的模板中的链接

<li><a href="{% url 'nodiso:leadlist' pk=company.id %}">Sales</a></li>

URl

url(r'^leads/(?P<pk>\d+)/$', views.LeadListView.as_view(), name='leadlist'),

视图

class LeadListView(LoginRequiredMixin, generic.ListView):
    login_url = '/scrty/login/'
    template_name = "nodiso/leadslist.html"
    model = models.Leads
    def get_queryset(self):
        return models.Leads.objects.filter(company=self.pk)

模型

class Leads(models.Model):
    company = models.ForeignKey(Company)
    name = models.CharField(max_length=265)
    email = models.EmailField(max_length=265)
    tel = models.IntegerField()
    dateenq = models.DateField()

所以基本上我正在尝试为潜在客户创建列表视图,但它应该只是与公司相关联的潜在客户,我也想以某种方式在潜在客户的观点中显示有关公司的信息。基本上是链接,但也将视图相互结合。

不确定我的方法是否正确。

1 个答案:

答案 0 :(得分:0)

我设法通过在会话中存储值来解决问题

def comp_select(request, company_id):
    comp = models.Company.objects.get(id=company_id)
    request.session['company'] = comp.name
    request.session['compid'] = company_id
    return redirect('nodisoapp:working',company_id)
相关问题