I have these models and I want to build forms based on the data:
class Location(models.Model):
name=models.CharField(max_length=255)
location_id=models.CharField(max_length=255)
organization=models.CharField(max_length=255)
def __unicode__(self):
return self.name
class Beverage(models.Model):
name=models.CharField(max_length=255)
location=models.ForeignKey(Location)
fill_to_standard=models.IntegerField(max_length=10)
order_when_below=models.IntegerField(max_length=10)
def __unicode__(self):
return self.name
class Inventory(models.Model):
location=models.ForeignKey(Location)
beverage=models.ForeignKey(Beverage)
units_reported=models.IntegerField(max_length=10)
timestamp=models.DateTimeField(auto_now=True)
Here is what I want to happen, when a user goes to update inventory for a particular location, I want them to get a form that lists out all the possible beverages for that location (the beverages differ per location) and then create an Inventory form for it that will create a new line in the Inventory table for each Beverage for that Location. Each one needs a timestamp so we can have a history. I think I get that I need formsets, but I have not had any success figuring out how to implement them for this. So, this is kind of a two-parter:
- Is my model design right for this problem?
- How can I make forms that depend on the number of beverages to build themselves?
Question 1: model design – I would drop the location field from the Inventory model, since it is already in the Beverage model, and is redundant. Otherwise, they look good to me.
Question 2: formsets…
forms.py
views.py
modelformset_factory will create a form for every object (Beverage) in the queryset which is filtered by the desired location, and has an extra field in the customized ModelForm to report units.