samedi 30 janvier 2021

Django: Adding monthly goal for products model, and daily production

I'm sorry if my question seems weird, I'll try to explain:

I'm building an app for my family's company, there are some products, and every month I need to set a goal for each product for each user.

I also need each user to be able to submit how much they've sold of each product every day

I'm confused about the approach I should use to relate all of these

What I've done is created models for the Products, for the goals and for 'production' (how much they've sold that day)

First, I put each product as field in the 'Production' model, but that wouldn't allow me to change anything using the admin panel

So, how can use the Products models in the Production models as "fields". ex:

class ProductsModel(models.Model):#This is the form each worker is supposed to fill every day, Worker is set accordingly to logged in user

     Worker = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)  
     ProductsModel1 = models.FloatField()#How much they've sold of this product
     ProductsModel2 = models.FloatField()#How much they've sold of this product
     I'd like to do this for as many Products as there are

And for the goals, this is what would be ideal:

class GoalsModel(models.Model):#This is me or the manager that are supposed to use, could be done throught the django admin page
      
        Worker = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)
        Month = models.CharField(max_length=9, choices=MONTH_CHOICES, default='1')
        Product1 = models.FloatField() #The goal for this product in the selected month
        Product2 = models.FloatField() #Goal for this product in the selected month
        .... This for as many Products as there are

This is what I have so far in my models.py:

class ProductsModel(models.Model):
    Name = models.CharField(max_length=50)

    def __str__(self):
        return self.Name


class GoalsModel(models.Model):
    Worker = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)
    Product = models.ForeignKey(ProductsModel, on_delete=models.SET_NULL, null=True)
    Month = models.CharField(max_length=9, choices=MONTH_CHOICES, default='1')
    Goal = models.FloatField()

class ProductionModel(models.Model):
    Worker = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.SET_NULL)
    Day = models.DateField(default=now)

    #What I had done was: I placed each product here, instead of creating a model for them, but that wouldn't allow me to add new products without changing the code directly

    def  __str__(self):
        answer = ('{}s production in {}'.format(self.Worker, self.Day))
        return answer

I'm sorry if it feels like I'm trying to get someone to do everything for me, but I am just trying to explain so that everything hopefully makes sense.




Aucun commentaire:

Enregistrer un commentaire