2017-06-07 76 views
0

我有2個數據表在我的應用程序,onClick任何project-content-datatable行將顯示其第二個表(內容表)上的子內容,現在在我的第二個表我有一個按鈕來彈出一個表單模式來創建新的內容,在這種形式下,我可以根據用戶點擊哪一行project-content-datatable來設置parent_project_content = models.ForeignKey(ProjectContent, on_delete=models.CASCADE)的默認值。基於數據錶行的點擊設置默認值

任何幫助非常感謝,謝謝

這裏是我的代碼 HTML

<table id="project-content-datatable" class="display table table-hover table-responsive" style="width: 100%"> 
    <thead> 
    <tr> 
     <th class="small text-muted text-uppercase"><strong>Name</strong></th> 
     <th class="small text-muted text-uppercase"><strong>Description</strong></th> 
    </tr> 
    </thead> 
    <tbody> 
    {% for content in content_list %} 
     {% if content.search_type.name == searchtype.name %} 
      <tr class="text-primary"> 
       <td class="text-left" style="cursor: pointer" 
        onclick='load_workorder({{ content.id }});'> 
        {{ content.id }} 
       </td> 
       <td class="text-left" style="cursor: pointer" onclick='load_workorder({{ content.id }});'> 
        {{ content.name }} </td> 
      </tr> 
     {% endif %} 
    {% endfor content_list %} 
    </tbody> 
</table> 
<!-- END Hover Table --> 


<!-- WORKORDER START--> 
    <h3 class="panel-title text-white">WorkOrders</h3> 
<div class="btn-group" role="group" aria-label="..."> 
    <button 
     data-toggle="modal" data-target="#create_new_workorder" 
     type="button" class="btn btn-sm btn-success btn-outline"><span 
     class="hidden-md hidden-sm hidden-xs">Create Workorder</span> <i 
     class="fa fa-fw fa-plus"></i> 
    </button> 
</div> 
<!--Create Workorder Form Modal --> 
<div class="modal fade" id="create_new_workorder" tabindex="-1" role="dialog" 
    aria-labelledby="TitleLabel"> 
    <div class="modal-dialog" role="document"> 
     <div class="modal-content"> 
      <div class="modal-header"> 
       <button type="button" class="close" data-dismiss="modal" aria-label="Close"> 
        <span aria-hidden="true">&#xD7;</span></button> 
       <h4 class="modal-title">Create New Workorder</h4> 
      </div> 
      <form method="POST" enctype="multipart/form-data">{% csrf_token %} 
       <div class="modal-body"> 
        {{ workorder_form.as_p }} 
       </div> 
       <div class="modal-footer"> 
        <input class="btn btn-primary" type="submit" 
          value="Create Workorder"/> 
        <button type="button" class="btn btn-default" data-dismiss="modal">Cancel 
        </button> 
       </div> 
      </form> 
     </div> 
    </div> 
</div> 

model.py

class WorkOrder(models.Model): 
    parent_project_content = models.ForeignKey(ProjectContent, on_delete=models.CASCADE) 
    description = models.TextField(default='workorder of project content', blank=True) 
    assign_to = models.ForeignKey(Employee, on_delete=models.CASCADE, related_name='assign_to', null=True, blank=True) 

form.py

class CreateWorkorderForm(forms.ModelForm): 
    class Meta: 
     model = WorkOrder 
     fields = ['parent_project_content','description', 'assign_to',] 

view.py

def content_view(request, project_id): 
    """ 
    :param request: 
    :param project_id: 
    :return: 
    """ 
    selected_project = get_object_or_404(Project, id=project_id) 
    search_type_list = selected_project.search_type.all() 
    project = get_object_or_404(Project, id=project_id) 
    form = CreateProjectContentForm(request.POST or None, request.FILES or None) 
    workorder_form = CreateWorkorderForm(request.POST or None, request.FILES or None) 

    if workorder_form.is_valid(): 
     instance = workorder_form.save(commit=False) 
     instance.parent_project_content = WorkOrder.objects.get(parent_project_content=24) 
     instance.save() 

    if form.is_valid(): 
     inst = form.save(commit=False) 
     inst.project = project 
     inst.save() 

    context = { 
     'form': form, 
     'workorder_form': workorder_form, 
     "search_type_list": search_type_list, 
     "selected_project": selected_project, 
     "content_list": ProjectContent.objects.all().filter(project=selected_project.id), 
     "content_id": WorkOrder.objects.all, 
    } 
    return render(request, 'dashboard/projects/content.html', context) 

回答

0

創建工作單的處理形成獨立的觀點,放置在彈出的工作訂單的按鈕具有「href」,這將導致你到處理工作單的提交一些看法像:

href="{% url 'workorder_handle' pk=content.pk %}" 

你的urls.py會是這樣:`

url(r'/workorder_form/(?P<pk>\d+)/$', workorder_view, name='workorder_handle'), 

我在這裏胡思亂想,更換需要更換

什麼
+0

嗨@Mani,感謝您的回覆,您能否給我一個完整的視圖示例,因爲我現在正在嘗試,而且它不工作。 –

+0

你是否在視圖中檢索到pk?您可以從kwargs中檢索它,如:pk = self.kwargs.get('pk'),然後ProjectContent.objects.get(pk = pk)。我假設你想讓這個PK分配給新的工作單,對嗎? – Mani

+0

是的,現在的問題,我不知道如何得到PK,我很抱歉,我很新的編程 –