在Django模板中使用related_name

1sbrub3j  于 2023-03-04  发布在  Go
关注(0)|答案(1)|浏览(162)

我有两个模型:Usecase和Usecase_progress。我正在创建一个页面,在那里我可以看到所有用例的列表,以及它们的信息+每个用例的用例进度。
用例模型:

class Usecase(models.Model):
usecase_id = models.CharField(primary_key=True, max_length=20)
usecase_name = models.CharField(max_length=256)
user_email = models.ForeignKey('User', models.DO_NOTHING, db_column='user_email')
usecase_type = models.ForeignKey('UsecaseType', models.DO_NOTHING)
kpi = models.ForeignKey(Kpi, models.DO_NOTHING)
business_owner = models.ForeignKey(BusinessOwner, models.DO_NOTHING)
usecase_description = models.CharField(max_length=5000)
usecase_creation_date = models.DateField()
estimated_date = models.DateField()
usecase_priority = models.CharField(max_length=100)
usecase_git = models.CharField(max_length=512, blank=True, null=True)
current_phase = models.ForeignKey(Phase, models.DO_NOTHING)
delivery_date = models.DateField()
class Meta:
    managed = False
    db_table = 'usecase'

用例进度模型:

class UsecaseProgress(models.Model):
usecase_progress_date = models.DateTimeField(primary_key=True)
usecase = models.ForeignKey(Usecase, models.DO_NOTHING, related_name='usecaseids')
phase = models.ForeignKey(Phase, models.DO_NOTHING)
pipeline = models.ForeignKey(Pipeline, models.DO_NOTHING)
usecase_progress_value = models.IntegerField()
estimated_date_delivery = models.DateField(db_column='Estimated_Date_Delivery')

我的网站views.py:

@user_login_required
def view_usecase(request):
     usecase_details = Usecase.objects.all()
     context = {'usecase_details': usecase_details}
     return render(request, 'ViewUsecase.html', context)

我的模板:

{% extends 'EmpDashboard.html' %}

{%块正文%}
{% if usecase_details不为none并且usecase_details %}

<div class="col-12 mb-4">
    {% for result in usecase_details %}
    <div class="card border-light shadow-sm components-section  d-flex ">
        <div class="card-body  d-flex ">
            <div class="row mb-4">                 
                <div class="card-body">
                    <div class="row col-12">                            
                    <form>
                        <div class="mb-4">
                            <h6 class="fs-5 fw-bold mb-0 border-bottom pb-3">{{result.usecase_id}} - {{result.usecase_name}}</h6>
                            <div class="mb-0 mt-2">{{result.usecase_description}}</div>                      
                        </div>
                        <div class="form-row mb-4">
                            <div class="col-lg-4 mr-f">
                                <label class="h6" for="exampleFormControlTextarea1">Business Owner:</label>
                                    <div class="mb-0">{{result.business_owner.business_owner_name}}</div>                      
                                </div>
                          <div class="col-lg-4 mr-f">
                            <label class="h6" for="exampleFormControlTextarea1">Owner:</label>
                                <div class="mb-0">{{result.user_email}}</div>                      
                            </div>
                        </div>
                        <div class="form-row mb-4">
                            <div class="col-lg-4 mr-f">
                              <label class="h6" for="exampleFormControlTextarea1">Usecase type:</label>
                                  <div class="mb-0">{{result.usecase_type.usecase_type_name}}</div>                      
                              </div>
                              {% for progress in usecase.usecaseids.all %}
                              <div class="col-lg-4 mr-f">
                              <label class="h6" for="exampleFormControlTextarea1">Progress:</label>
                                  <div class="mb-0">{{progress.usecase_progress_date}}</div>     
                              </div>
                              {% endfor %}
                          </div>
                          <div class="form-row mb-4">
                            <div class="col-lg-4 mr-f">
                              <label class="h6" for="exampleFormControlTextarea1">Usecase creation date:</label>
                                  <div class="mb-0">{{result.usecase_creation_date}}</div>                      
                              </div>
                            <div class="col-lg-4 mr-f">
                              <label class="h6" for="exampleFormControlTextarea1">Estimated date:</label>
                                  <div class="mb-0">{{result.estimated_date}}</div>                      
                              </div>
                          </div>
                          
                      </form>
                  </div>
                </div>
            </div>
        </div>
    </div>
    <br>
    {% endfor %}
</div>

{% endif %}{% endblock %}
无论如何,我无法显示每个用户的进度日期,因为它显示的是空行。

dw1jzc5e

dw1jzc5e1#

显示进度的循环不好。变量名为result,而不是usecase

{% for progress in result.usecaseids.all %}
    <div class="col-lg-4 mr-f">
        <label class="h6" for="exampleFormControlTextarea1">Progress:</label>
        <div class="mb-0">{{progress.usecase_progress_date}}</div>     
    </div>
{% endfor %}

您可以在视图中添加数据预取,以便进行SQL连接来检索所有数据。如果没有,Django将对每一行进行查询。

@user_login_required
def view_usecase(request):
     usecase_details = Usecase.objects.all()
     usecase_details = usecase_details.prefetch_related("usecaseids")
     context = {'usecase_details': usecase_details}
     return render(request, 'ViewUsecase.html', context)

相关问题