Generating reports with dynamic field filters in django

Viewed 725

I am displaying a list of model fields and related fields to the user. Users can set the filter on one or multiple fields. I need to return results according to those filters.

Here is my Employee Model

    employeeID = models.AutoField(primary_key=True, db_index=True)
    joiningDate = models.DateField(null=True, blank=True)
    department = models.IntegerField()
    designation = models.IntegerField()
    qualification = models.CharField()
    highestDegree = models.CharField()    
    name = models.CharField(max_length=50)
    fatherName = models.CharField(max_length=50, null=True, blank=True)
    motherName = models.CharField(max_length=50, null=True, blank=True)
    dob = models.DateField(null=True, blank=True)
    gender = models.IntegerField(choices=GenderTypes.choices(), null=True, blank=True)
    religion = models.IntegerField(choices=ReligionChoices.choices())
    category = models.IntegerField(choices=CategoryChoices.choices())
    address = models.TextField(null=True, blank=True)

Other than these model fields, Users can apply a filter on some connected fields as well like salary which is stored in another table with employeeID as a foreign key. Filters can be particular values as well as range, as in the case of salary (10000 - 20000).

I know Django does not provide such a facility and I need to write custom logic. I just needed to confirm that should I write hard-coded logic for every field or there is a way where I can find if a particular field belongs to that model or not. In case it does not, is there a way where I can derive the relation of that particular field from the model? These two things combined should give us enough to implement this.

Thanks a lot for your time and help

1 Answers

Take a look at Django-Filter which is the standard package used for filtering in Django applications.

Related