Django - Is there a possibility to somehow combine multiple foreign keys into 1 field value from another model?

24 views Asked by At

I want to create a model called DailyPerformance with following fields:

class DailyPerformance(models.Model):
  date = models.DateField()
  driver = models.ForeignKey(Employee, on_delete=models.CASCADE)
  TYPE_OF_GOODS_CHOICES = (
    ("Excavated soil", "Excavated soil"),
    ("Sand", "Sand"),
    ("Crushed stone", "Crushed stone"),...
  )
  type_of_goods = models.CharField(blank=True, null=True, max_length=30, choices=TYPE_OF_GOODS_CHOICES)
  place_of_loading = models.ForeignKey(ConstructionSite, on_delete=models.CASCADE)
  place_of_unloading = models.ForeignKey(ConstructionSite, on_delete=models.CASCADE)
  number_of_rounds = models.IntegerField(blank=True, null=True)

My biggest problem is with fields place_of_loading and place_of_unloading. Place of loading could be Construction site, Gravel pit, Sand pit, etc., while place of unloading could be Construction site or Landfill. Since I already have ConstructionSite model, I wanted to create models GravelPit, SandPit, Landfill and combine all of them in DailyPerformance model.

This is the idea: Our drivers will need to fill the form at the end of working day, and I want for place_of_loading drop-down menu to have values from ConstructionSite, GravelPit and SandPit. The same goes for place_of_unloading.

  place_of_loading = models.ForeignKey(ConstructionSite, GravelPit, SandPit)
  place_of_unloading = models.ForeignKey(ConstructionSite, Landfill)

I know this doesn't work, but wanted to know if there is any other solution.

0

There are 0 answers