Monday, May 14, 2012

Django: creating dynamic forms

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: 1) is my model design right for this problem? 2) how can I make forms that depend on the number of beverages to build themselves?



Thanks!





No comments:

Post a Comment