Skip to main content

Create Excel Sheet from Django Model and load them automatically

Project description

django-batch-sheet

Most of the projects we worked on needs a feature to upload data in batch, we always ended up writing another code to handle the Excel sheet and where each column shall go in the model(s), in the latest project we decided to handle it differently and that is why we wrote a django app that handles this like ModelForm handles models and surprisingly, it worked.

Features

  • Generate a Sheet from a Django model or a combination of models (through Combined Sheet),
  • Add the validation rules for Choices, Foreign Keys, Integers automatically in Excel,
  • Showing the required field in red by default
  • Sheet can be validated automatically and check with is_valid()
  • Allow overriding the behavior of model in the class,
  • Allow alot of functions to override the class behavior (row_preprocess,save,pre_load,post_process),
  • Automatic Loading of the data in the sheet to the model.

Install

  1. Install the package
pip install django-batch-sheet
  1. Add it to INSTALLED_APPS
INSTALLED_APPS=[
...
'batch_sheet'
...
]

Example

Let's assume that we have the following models with these rules

  • Patient can have multiple tests but saved once as a Patient Object.
class Test(models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name


class Gender (models.Model):
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

class Patient(models.Model):
    name = models.CharField(max_length=50,verbose_name="Patient Name")
    MRN = models.CharField(max_length=50)
    gender= models.ForeignKey(Gender,on_delete=models.PROTECT,null=True, verbose_name="Gender")
    date_of_birth = models.DateField(null=True,blank=True,verbose_name="Birth Date")
    date_admitted = models.DateField(auto_now_add=True,auto_created=True)
    lastUpdate = models.DateTimeField(auto_now=True)


class RequestedTest(models.Model):
    patient = models.ForeignKey(Patient,on_delete=models.CASCADE)
    test = models.ForeignKey(Test,on_delete=models.PROTECT,verbose_name="Test")
    sample_date = models.DateField(auto_now_add=True,auto_created=True)
    sample_type = models.CharField(max_length=50, verbose_name="Sample Type",choices=(('Blood','Blood'),('DNA','DNA')))
    lastUpdate = models.DateTimeField(auto_now_add=True)

We want to combine both Models in on sheet, so we put the patient information with the requested test in one row.

So For Patient, we will implement the class PatientSheet as follows

class PatientSheet(Sheet):
    def save(self, obj:Patient, row_objs:dict):
        """Lets handle the save manually to check if the MRN exists"""
        patient = Patient.objects.filter(MRN = obj.MRN)
        if patient.exists():
            p = patient[0]
        else:
            p = obj
            p.save()
        return p
    class Meta:
        exclude=('id','date_admitted','lastUpdate')
        Model = Patient
        obj_name = "patient"
        title_header = True

For the Meta attributes, please go to the Meta Attributes sections

For RequestedTest Sheet, it goes like this

class RequestedTestSheet(Sheet):
    def save(self,obj,row_objs):
        patient = row_objs.get('patient')
        if patient is None:
            return None
        else:
            obj.patient = patient
            obj.save()
            return obj

    class Meta:
        exclude =('id','sample_date','lastUpdate')
        validation_exclude = ('patient',)
        title_header = True
        Model = RequestedTest

Now Lets combine them in one sheet

class TestSheet(CombinedSheet):
    patient = PatientSheet()
    test = RequestedTestSheet()

Now Let's Generate The sheet

$ python manage.py generate_sheet --xls App.xls --sheet test_app.sheets.TestSheet

This will the file to App.xls in the folder, now, lets open App.xls

docs/imgs/xls.png

We got the dropdown automatically, based on the value in the database.

Meta Options

    Class Meta:
        rows_count = 10              # Number of rows to apply validations on
        columns = ()                 # Columns to add from the Model to the sheet
        exclude = ()                 # Columns to exclude
        Model = None                 # Model to scan
        raw_cols = []                # Foreign Key Field that shouldn't be set as dropdown
        title_header = False         # Make the titles as "First Name"
        validation_exclude=[]        # Don't validate these field, important in case of objects relationship  
        object_name = None           # The name of the object saved, important in case of Combined Sheet.
            

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_batch_sheet-0.20.1.tar.gz (11.4 kB view details)

Uploaded Source

File details

Details for the file django_batch_sheet-0.20.1.tar.gz.

File metadata

  • Download URL: django_batch_sheet-0.20.1.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for django_batch_sheet-0.20.1.tar.gz
Algorithm Hash digest
SHA256 720409c87f44f6257249648aa18166afa5d8eefeae4e7e42fd8790a64f1fb838
MD5 f9d3c4404f0a9addbdd59c05886b629a
BLAKE2b-256 0c25a41109989479d3d8478ca456dc197bbf4ccc3099c3a5079b055a81eba025

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page