Django Form, save to multiple tables

Viewed 224

I'm working on an order entry system where it'll track products, recipes, and orders for customers. I'm trying to figure out how to create a recipe_ingredient form/template that will create a product entry, a recipe entry and an associated recipe ingredient entry(s) when the user creates a new recipe. The form should ask for the recipe name, description, along with the ingredients(products table), and percentage.

Database Design (I don't know if I got the symbols correct) enter image description here

Here's my models.py for the product/recipe/recipe ingredient

class Product(models.Model):
    name = models.CharField(max_length=200, null=True)
    sku = models.CharField(max_length=200, null=True, blank=True)
    supplier = models.ForeignKey(Supplier, null=True, on_delete= models.SET_NULL)
    description = models.CharField(max_length=200, null=True, blank=True)
    cost_per_pound = models.DecimalField(max_digits=7, decimal_places=2, blank=True)
    cost_per_ounce = models.DecimalField(max_digits=7, decimal_places=2, blank=True)
    note = models.CharField(max_length=1000, null=True, blank=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)

class Recipe(models.Model):
    name = models.ForeignKey(Product, null=True, on_delete=models.SET_NULL)
    description = models.CharField(max_length=200, null=True, blank=True)

class Recipe_Ingredient(models.Model):
    recipe_name = models.ForeignKey(Recipe, null=True, on_delete=models.SET_NULL)
    ingredient = models.ForeignKey(Product, null=True, on_delete=models.SET_NULL)
    recipe_percent = models.DecimalField(max_digits=8, decimal_places=5, blank=True)

I've got a semi-working recipe ingredient form, but the user will need to create the product first and then select it when creating the recipe and recipe ingredients.

def recipeCreate(request):
    RecipeIngredientFormSet = forms.inlineformset_factory(Recipe, Recipe_Ingredient, form=RecipeIngredientForm, extra=10, fields=('ingredient', 'recipe_percent',))
    form = RecipeForm()
    formset = RecipeIngredientFormSet()
    if request.method == 'POST':
        form = RecipeForm(request.POST)
        if form.is_valid():
            recipe = form.save()
            formset = RecipeIngredientFormSet(request.POST, instance=recipe)
            if formset.is_valid():
                formset.save()
            return redirect('/')
    context = {'form':form, 'formset':formset}
    return render(request, 'accounts/recipe_form.html', context)

This is what I'm trying to have displayed to the end user... enter image description here When they create a recipe, this form shows up. They enter the name, description, ingredients and percentages. When they click "Submit", I want the following to happen

  1. the name supplied, will create a new entry in the PRODUCT table w/ a default supplier of "8" (supplier FK), everything else blank
  2. create an entry in the RECIPE table with the name and description entered
  3. create the entries to the RECIPE_INGREDIENTS table with the information supplied

I know that I'll need to remove the FK relationship from PRODUCT to RECIPE....I guess once I figure out how to save a form to multiple tables, I can figure out how to re-design the database.

0 Answers
Related