2015-07-11 38 views
-1

考慮我的模板:如何複製與登記的DetailView,讓PK(Django的)

entry_detail.html

<form class="navbar-form navbar-right" action="." method="get"> 
    <!-- add --> 
    <!-- <p name="filter_link" class="pull-right"><a href="">Produtos em baixo estoque</a></p> --> 
    <a name="new_customer" href="{% url 'proposal_list' %}"> 
     <button type="button" class="btn btn-primary"> 
      <span class="glyphicon glyphicon-plus"></span> Criar Orçamento 
     </button> 
    </a> 
</form> 

而且considerer我的觀點:

views.py

class EntryDetail(DetailView): 
    template_name = 'core/entry/entry_detail.html' 
    model = Entry 

    def create_proposal(self, employee_pk=8): 
     if 'new_customer' in self.request.GET: 
      employee = Employee.objects.get(pk=employee_pk) 
      num_last_proposal = NumLastProposal.objects.get(
       pk=1) # sempre pk=1 
      entry = Entry.objects.get(pk=self.request.GET[self.id]) 
      obj = Proposal(
       num_prop=num_last_proposal.num_last_prop + 1, 
       type_prop='R', 
       category=entry.category, 
       description=entry.description, 
       work=entry.work, 
       person=entry.person, 
       employee=employee, 
       seller=entry.seller, 
      ) 
      obj.save() 

      entry.is_entry = True 
      entry.save() 

      num_last_proposal.num_last_prop += 1 
      num_last_proposal.save() 

問題:如何使這個工作。如何獲得entry.pk中的DetailView用於

entry = Entry.objects.get(pk=self.request.GET[self.id]) 

在manage.py殼工作

shell_create_proposal.py

from core.models import Entry, Proposal, Employee, NumLastProposal 

employee = Employee.objects.get(pk=8) 
num_last_proposal = NumLastProposal.objects.get(pk=1) 
entry = Entry.objects.get(pk=1) 
obj = Proposal(
    num_prop=num_last_proposal.num_last_prop + 1, 
    type_prop='R', 
    category=entry.category, 
    description=entry.description, 
    work=entry.work, 
    person=entry.person, 
    employee=employee, 
    seller=entry.seller, 
) 
obj.save() 

entry.is_entry = True 
entry.save() 

num_last_proposal.num_last_prop = num_last_proposal.num_last_prop + 1 
num_last_proposal.save() 

回答

0

在URL中指定的PK和詳細信息視圖應爲你處理它

url(r'^yourview/(?P<pk>\d+)/' , YourDetailView.as_view() , name='your_view_name'), 

看看這裏的get_object方法:

http://ccbv.co.uk/projects/Django/1.8/django.views.generic.detail/DetailView/

它得到的PK如下:

pk = self.kwargs.get(self.pk_url_kwarg, None) 
+0

這個我知道,問題是如何獲得的PK鑑於複製數據。 –

+0

我更新了答案,以顯示get對象方法如何實現它。你可以在我給你看的網頁上查看它。 –