Monday, February 29, 2016

Re: Absolute beginner question -- recipes

Hey everybody,

thank you very much for your answers. I think I have already learned a lot from you! Well - there is quite a difference between stepping through a tutorial and trying to do something on my own. Regarding Mikes question whether a step of a recipe should be a sub-recipe. Well, as far as I know hierarchical relationships are hard to model in relational databases; that's why I kept my hands off! My decision to create a recipe app has a simple reason: I read some comparisons between django and ruby on rails and I decided to go for django since it seems to leave more freedom. I found a RoR tutorial on creating a recipe app some time ago and I am trying to do the same thing in django now.

Concerning my model - this is how it looks like:

from __future__ import unicode_literals

from django.db import models

class Unit(models.Model):
    abbreveation
= models.CharField(max_length=5, primary_key=True)
    description
= models.CharField(max_length=50)

   
def __str__(self):
       
return "%s (%s)" % (self.description, self.abbreveation)

class Ingredient(models.Model):
    name
= models.CharField(max_length=255)

   
def __str__(self):
       
return self.name

class Step(models.Model):
    name
= models.CharField(max_length=255, blank=True)
    recipe
= models.ForeignKey('Recipe', related_name='steps', on_delete=models.CASCADE, null=True)
    description
= models.TextField()
    ingredient
= models.ManyToManyField(Ingredient, through='StepIngredient')

   
def __str__(self):
       
if (self.name == ''):
           
return "Step %d" % self.id
       
else:
           
return "Step %d (%s)" % (self.id, self.name)

# additional information for many to many relationship between schritt and zutat
class StepIngredient(models.Model):
   
class Meta:
        unique_together
= (('step', 'ingredient'),)
    step
= models.ForeignKey(Step, null=True)
    ingredient
= models.ForeignKey(Ingredient, null=True)
    amount
= models.DecimalField(max_digits=7, decimal_places=3)
    unit
= models.ForeignKey(Unit, null=True)

   
def __str__(self):
       
return "%d %s %s" % (self.amount, self.unit.abbreveation, self.ingredient)

class Recipe(models.Model):
    name
= models.CharField(max_length=255)
   
   
# file will be uploaded to MEDIA_ROOT/images
   
# TODO: Upload works but link fails. Maybe sth. wrong with MEDIA_URL???
    photo
= models.ImageField(upload_to='images/', null=True)
    portionen
= models.IntegerField(default=2)

   
def __str__(self):
       
return self.name

Thank you James for pointing me at how to do my queries with django; I can finally list my steps using:

{% for step in recipe.steps.all %}

within my template. However, given the model above I am still not sure how to access a single steps ingredients ... any ideas or suggestions on my current model?

Thanks, Simon



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cef5e940-6947-4fe2-b20b-1c805ea09572%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment