Save django Models in different script and run that script with HTML button

94 views Asked by At

I have model.py as

    class Fruit(models.Model):
        name = models.CharField(max_length=16)
        weight = models.IntegerField(max_length=16)
        color = models.CharField(max_length=16)
    class Veg(models.Model):
        name = models.CharField(max_length=16)
        weight = models.IntegerField(max_length=16)

I have myFunction.py as

    
    def makeModels(file):
        with open(file, 'r') as f:
            line=f.readline()
            while line:
                 if line.split(',')[0] == 'fruit':
                      a = Fruit(name=line.split(',')[1], weight=line.split(',')[2],
                                 color=line.split(',')[3])
                      a.save()
                 if line.split(',')[0] == 'veg':
                      b = Veg(name = line.split(',')[1], weight = line.split(',')[2]
                      b.save()
                 line = f.readline()

and files as abc.txt

"fruit,apple,3,red\n fruit,banana,1,green\n veg,spinach,4\n veg,collard,2\n fruit,banana,3,yelow\n veg,broccoli,3\n fruit,orange,1,orange"

I want to create an HTML button to trigger this function/script and also create a celery scheduler to schedule a trigger to run at a time on a daily basis. This is on a django framework.

Please help me on how to approach it. The running external function to save models through html button and scheduling it part. Thank you!

0

There are 0 answers