Added a better slugify function, monthly archives work, notes are optional now.
[~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 slugify import SlugifyUniquely
6
7
8 class Ingredient(models.Model):
9         name = models.CharField(max_length=100)
10         slug = models.SlugField(unique=True)
11         
12         def __unicode__(self):
13                 return self.name
14
15
16 class Sandwich(models.Model):
17         adjective = models.CharField(max_length=200)
18         slug = models.SlugField(unique=True)
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/', blank=True)
23         notes = models.TextField(blank=True)
24         user = models.ForeignKey(User)
25         
26         class Meta:
27                 ordering = ['date_added']
28                 verbose_name_plural = "Sandwiches"
29                 
30         def __unicode__(self):
31                 return self.adjective
32                 
33         def get_absolute_url(self): 
34             return reverse('sandwich_by_slug', kwargs={'slug':self.slug})
35         
36         def save(self, *args, **kwargs):
37                 self.slug = SlugifyUniquely(self.adjective, Sandwich)
38                 super(Sandwich, self).save(*args, **kwargs)