根据单击的数据表行将默认值设置为ForeignKey字段

时间:2017-06-07 01:59:23

标签: javascript jquery python html django

我的应用程序中有2个数据表,onClick任何project-content-datatable行都会在第二个表(内容表)上显示其子内容,现在在我的第二个表中我有一个弹出的按钮 - 在模式中创建一个表单以在其中创建新内容,在该表单中,我可以根据用户单击的行parent_project_content = models.ForeignKey(ProjectContent, on_delete=models.CASCADE)设置project-content-datatable的默认值。

非常感谢任何帮助,谢谢

这是我的代码 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)

1 个答案:

答案 0 :(得分:0)

创建一个单独的视图来处理工单形式,放置一个&#39; href&#39;在弹出工作订单表单的按钮上,这将引导您到处理工作订单提交的视图:

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

你的urls.py就像:`

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

我在这里想象事物,取代需要替换的东西