Django通过增值税连接公司合同清单?

时间:2018-06-11 06:46:59

标签: python django

如何在不使用pk的情况下将某个公司与合同列表连接起来,因为当我插入合同时,他们会得到不同的pk id。两种型号都有增值税号,但不知道如何连接它们......

我需要做什么,列出公司增值税号码的所有合同? 增值税号在公司中是唯一的,但在合同中将有超过一个增值税号,因为它可以有多个合同。

来自company.html的

  <p><a href="{% url 'contracts-detail' company.pk %}">Contracts</a> </p>

models.py

class Company(models.Model):
    company = models.CharField(max_length=255)
    VAT= models.CharField(max_length=11) 
    def __str__(self):
        return self.company

    def get_absolute_url(self):
        return reverse('company-detail', args=[str(self.id)])

class Contracts(models.Model):
    company = models.CharField(max_length=255)
    VAT= models.CharField(max_length=11)
    contractNumber = models.CharField(max_length=255)

    def __str__(self):
        return '{0}'.format(self.VAT)

    def get_absolute_url(self):
        return reverse('contract-detail', args=[str(self.id)])

views.py

class CompanyListView(generic.ListView):
    model = Company
    context_object_name = 'company_list'  
    queryset = Company.objects.filter()
    paginate_by = 10

class ContractsListView(generic.ListView):
        model = Contracts
        context_object_name = 'contract_list'
        queryset = Contracts.objects.filter()
        paginate_by = 10

class ContractsDetailView(generic.DetailView):
            model = Contracts

        def contracts_detail_view(request, pk):
            try:
                company_id = Contracts.objects.get(pk=pk)
            except Exception as e:
                print(str("contracts_detail_view",str(e)))

            return render(
                request,
                'contracts_detail.html',
                context={'contract': company_id, }
            )

urls.py

urlpatterns = [
    path('', views.index, name='index'),
    path('com/', views.CompanyListView.as_view(), name='companies'),
    path('com/<int:pk>', views.CompanyDetailView.as_view(), name='company-detail'),
    path('cont/', views.ContractsListView.as_view(), name='contracts'),
    path('cont/<int:pk>', views.ContractsDetailView.as_view(), name='contracts-detail'),
    ]

0 个答案:

没有答案