Fixed URLs of one-sandwich views. Fixed adding sandwiches.
[~kgodey/maayanwich.git] / models.py
1 from django.db import models
2 from django.contrib.auth.models import User
3 import datetime
4 from django.core.urlresolvers import reverse
5 from django.template.defaultfilters import slugify
6
7
8
9
10 class Ingredient(models.Model):
11         name = models.CharField(max_length=100)
12         slug = models.SlugField()
13         
14         def __unicode__(self):
15                 return self.name
16
17
18 class Sandwich(models.Model):
19         adjective = models.CharField(max_length=200)
20         slug = models.SlugField()
21         date_made = models.DateField()
22         ingredients = models.ManyToManyField(Ingredient)
23         date_added = models.DateTimeField(default=datetime.datetime.now, editable=False)
24         picture = models.ImageField(upload_to='sandwiches/')
25         notes = models.TextField()
26         user = models.ForeignKey(User)
27         
28         class Meta:
29                 ordering = ['date_added']
30                 verbose_name_plural = "Sandwiches"
31                 
32         def __unicode__(self):
33                 return self.adjective
34                 
35         def get_absolute_url(self): 
36             return reverse('sandwich_by_slug', kwargs={'slug':self.slug})
37         
38         def save(self, *args, **kwargs):
39                 self.slug = slugify(self.adjective)
40                 super(Sandwich, self).save(*args, **kwargs)