1 from django.db import models
2 from django.contrib.auth.models import User
4 from django.core.urlresolvers import reverse
5 from django.template.defaultfilters import slugify
8 class Ingredient(models.Model):
9 name = models.CharField(max_length=100)
10 slug = models.SlugField()
12 def __unicode__(self):
16 class Sandwich(models.Model):
17 adjective = models.CharField(max_length=200)
18 slug = models.SlugField()
19 date_made = models.DateField()
20 ingredients = models.ManyToManyField(Ingredient)
21 date_added = models.DateTimeField(default=datetime.datetime.now, editable=False)
22 picture = models.ImageField(upload_to='sandwiches/')
23 notes = models.TextField()
24 user = models.ForeignKey(User)
27 ordering = ['date_added']
28 verbose_name_plural = "Sandwiches"
30 def __unicode__(self):
33 def get_absolute_url(self):
34 return reverse('sandwich_by_slug', kwargs={'slug':self.slug})
36 def save(self, *args, **kwargs):
37 self.slug = slugify(self.adjective)
38 super(Sandwich, self).save(*args, **kwargs)