0

I am developing an audit management information system where I can record all finding related to an audit. I have models with foreignkeys relationship. How do I see all findings with a particular assignment and audit_title and unit? See relevant codes below.

model.py content

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',
                ]

Am I have the following in my views.py but I have this error invalid literal for int() with base 10: ''

views.py content

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,)
simplyvic
  • 213
  • 6
  • 17
  • Can you give an example of the values you want to filter against? – Daniel Roseman Aug 28 '19 at 20:32
  • These might be what you're looking for. https://stackoverflow.com/questions/291945/how-do-i-filter-foreignkey-choices-in-a-django-modelform https://stackoverflow.com/questions/1821289/django-modelform-instance-with-custom-queryset-for-a-specific-field – 416e6f6e Aug 28 '19 at 20:40

1 Answers1

0
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)
simplyvic
  • 213
  • 6
  • 17