I am trying to make an application for recipes using Django. I am
starting simple and going from the tutorial but not getting the
results I want. In a less strict framework I'd be able to represent
what I want to easily but I'm having trouble figuring out how to do it
the Django way while keeping the database clean and easy to query to
prevent performance issues.

For this example, let's say I have two different models.

Recipe, and Ingredient.

A recipe has a name and a set of ingredients and an amount and
measurement corresponding to each ingredient. An ingredient has only a
name.

Here's an example schema of what the database in my mind would look like in 3NF:

create table drinks (id int(11) auto_increment, name varchar(255));
create table ingredients (id int(11) auto_increment, name varchar(255));
create table drinks_ingredients (drinks_id int(11) not null,
ingredients_id int(11) not null, amount int(11) not null, unit
varchar(12));

or similar. That allows me to create only one row per a specific
ingredient in the ingredients table(e.g. salt) and then use any
measurement of it in the drinks_ingredients table. That way you can
easily search for recipes containing a specific ingredient.

I'm having trouble figuring out how to get Django to recreate this
database structure. The tutorial example with the poll application is
similar but I'm not happy with the database schema because you can't
use the same choice in multiple polls. This means that if you had 50
yes/no polls, you'd have "Yes" and "No" replicated in the database 50
times which, frankly, sucks.

I think I need to use a ManyToManyField for this but I'm having
trouble finagling it to be the way I want. Here's what I have right
now but I can't figure out where to put the amount and unit for the
ingredients:

class Ingredient(models.Model):
    name        = models.CharField(max_length=255)
    amount      = models.IntegerField()
    unit           = models.CharField(max_length=12)

    def __unicode__(self):
        return self.name
    class Admin:
        pass

class Recipe(models.Model):
    name        = models.CharField(max_length=255)
    ingredients = models.ManyToManyField('Ingredient')

    def __unicode__(self):
        return self.name
    class Admin:
        pass


Any ideas would be greatly appreciated :)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to