使用基于另一个表的结果创建for循环

时间:2016-11-17 10:47:16

标签: python html django for-loop

我有两个对象列表,buildnumbers和partrequestnumbers。 这两个列表都包含buildnumbers,我想要实现的是buildnumbers的html列表及其相关的parturequestnumbers,链接是buildnumber。到目前为止,我已经看过numpy数组和一些for循环建议,但没有实现我想要实现的目标。我当前的for循环只是循环遍历所有数字...... The image shows the build list, I'd like the partnumbers next to it too Django观点:

def manufacturelist(request, mug=None, slug=None):
    if not request.user.is_staff or not request.user.is_superuser:
        raise Http404 
    partrequests = PartRequestNumbers.objects.order_by('-id')
    latest_preparebuild_list = PrepareBuild.objects.order_by('-pub_date')
    builds=[]
    for preparebuild in latest_preparebuild_list:
        bb = preparebuild.buildno
        builds.append(PartRequestNumbers.objects.filter(buildno=bb))
    context = {
        "latest_preparebuild_list": latest_preparebuild_list,
        "partrequests": partrequests,
        "builds": builds,
    }
    return render(request, "buildpage/manufacturelist.html", context)

HTML:

 {% if latest_preparebuild_list %}
    <ul>
    {% for preparebuild in latest_preparebuild_list %}

       <li><a class = "subtitle" >{{ preparebuild.buildno }}: </a><a style= "float: right" href="/buildpage/{{ preparebuild.part_request }}/manufacturebuild">{{ partrequests.part_request }}</a><p style= "float: right">{{ preparebuild.build_status }}</p></li>
      {% for partrequest in partrequests %} 
      <center><a href="/buildpage/{{ partrequest.part_request }}/manufacturebuild">{{ partrequest.part_request }}&nbsp;&nbsp;</a></center>
      {% endfor %}
    {% endfor %}
    </ul>
{% else %}
    <p>No builds are available.</p>
{% endif %}

模型:

class PrepareBuild(models.Model):
    pub_date = models.DateTimeField(default= datetime.now)
    STATUS = Choices('Complete', 'In-Build', 'Awaiting Build', 'Cancelled', 'Design Issue', 'M/C Issue')
    buildstatus = StatusField()
    status_changed = MonitorField(monitor='buildstatus')
    buildno = models.CharField(_('Build Number'),max_length=10, default= bnumber)
    current_location = models.ForeignKey(BuildLocation, default='', null=True)
    machine = models.ForeignKey(Machine, max_length=10, default ="")
    batchno = models.CharField(_('Batch Number'),max_length=100, default="")
    partnumber = models.CharField(_('Part Number(s)'), max_length = 100, default = 'e.g. 002109_1')
    buildtime = models.CharField(_('Build Time'), default= '', max_length=100)
    buildnotes = models.CharField(_('Build Notes'), max_length=200, default = "", blank=True)
    mug = models.SlugField(unique=True, null=True)
    def __unicode__(self):
        return self.buildno or default
class PartRequestNumbers(models.Model):
    buildno = models.CharField(_('Build Number'),max_length=10, default= bnumber)   
    part_request = models.ForeignKey(PartRequest, related_name='PR Number+', null=True) 
    def __str__(self):
        return '{}' .format(self.part_request)  

0 个答案:

没有答案