【问题标题】:Query django foreignkey relationship查询django外键关系
【发布时间】:2020-01-02 02:22:41
【问题描述】:

我正在开发一个审计管理信息系统,我可以在其中记录与审计相关的所有发现。我有具有外键关系的模型。如何查看具有特定作业和 audit_title 和单元的所有结果? 请参阅下面的相关代码。

model.py 内容

class Unit(models.Model):
    unit_name = models.CharField(max_length=30, blank=True, null=True)
    def __unicode__(self):
       return self.unit_name

class Assignment(models.Model):
    assignment_name = models.CharField(max_length=30, blank=True, null=True)
    def __unicode__(self):
       return self.assignment_name

class Task(models.Model):
    task_title = models.CharField(max_length=35, blank=True, null=True)
       return self.task_title



class Finding(models.Model):
    assignment = models.ForeignKey(Assignment, blank=True, null=True)
    audit_title = models.ForeignKey(Task, blank=True, null=True)
    auditor = models.ManyToManyField(User, blank=True)
    unit = models.ForeignKey(Unit, blank=True, null=True)
    audit_period = models.DateField(auto_now_add=False, auto_now=False, blank=True, null=True)
    contact_person = models.CharField('Contact Person', max_length=500, blank=True, null=True)
    finding = models.TextField('Detail Finding', max_length=500, blank=True, null=True)
    be = models.CharField(max_length=30, blank=True, null=True)

form.py

class FindingSearchForm(forms.ModelForm):
    class Meta:
        model = Finding
        fields = ['assignment',
                'audit_title',
                'unit',
                'be',
                ]

我的views.py中有以下内容,但我有这个错误invalid literal for int() with base 10: ''

views.py 内容

def finding_list(request):
    title = 'List of Finding'
    queryset = Finding.objects.all()
    queryset_count = queryset.count() 
    form = FindingSearchForm(request.POST or None)
    context = {
         "title": title,
         "form": form,
         "queryset_count": queryset_count,
    }
    if request.method == 'POST':
        unit = form['unit'].value()
        audit_title = form['audit_title'].value()
        assignment = form['assignment'].value()

        queryset = Finding.objects.all().order_by('-timestamp').filter(be__icontains=form['be'].value(),
                                                            unit_id=unit,
                                                            assignment_id=assignment,
                                                            audit_title_id=audit_title,)

【问题讨论】:

标签: django foreign-keys django-queryset


【解决方案1】:
if request.method == 'POST':
    unit = form['unit'].value()
    audit_title = form['audit_title'].value()
    assignment = form['assignment'].value()

    queryset = Finding.objects.all().order_by('-timestamp').filter(be__icontains=form['be'].value()
                                            )
    if (unit != ''):
        queryset = queryset.filter(unit_id=unit)

    if (audit_title != ''):
        queryset = queryset.filter(audit_title_id=audit_title)

    if (assignment != ''):
        queryset = queryset.filter(assignment_id=assignment)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-22
    • 2012-10-10
    • 2020-04-20
    • 1970-01-01
    • 2011-12-06
    • 2010-09-10
    • 2016-07-29
    相关资源
    最近更新 更多