Hi Tane, [untested with Many2Many, but should work, it works for ForeignKeys]
In your Entry save() method call the save method of each of your related
categories after you do your super().save()
{{{
class Entry(models.Model):
...
def save(self):
if not self.slug:
self.slug = slugify(self.title)
super(Entry, self).save()
for category in self.categories:
category.save()
...
}}}
Then in the save method for your Category object you need to calculate
the number of entries in that category before you call super().save()
{{{
class Category(models.Model):
...
def save(self):
if not self.slug:
self.slug = slugify(self.name)
self.num_entries = self.entry_set.count()
super(Category, self).save()
...
}}}
Hope that helps, or at least gives you a pointer.
Regards
Darryl
Tane Piper wrote:
> Here is my blog application, which contains the two models:
>
> from django.db import models
> from django.db.models import permalink
> from django.core import urlresolvers
> from django.contrib.auth.models import User
> from django.template.defaultfilters import slugify
>
> import datetime
> # Create your models here.
>
> class Category(models.Model):
> """ A Category is a way to manage the taxonomy of the site"""
> name=models.CharField(max_length=50) #The name of the category
> description=models.TextField()
> slug=models.CharField(max_length=75, null=True, blank=True)
> active=models.BooleanField()
> generate_feed=models.BooleanField()
> parent=models.ForeignKey('self', null=True, blank=True)
> def __unicode__(self):
> return self.name
> def save(self):
> if not self.slug:
> self.slug = slugify(self.name)
> super(Category, self).save()
> def num_entries(self):
> """Returns the number of entries in this category"""
> return self.entry_set.count()
> num_entries.short_description = "Number of Entries"
> def get_absolute_url(self):
> """Get the URL of this entry to create a permalink"""
> return ('cat-detail', (), {
> "slug": self.slug
> })
> get_absolute_url = permalink(get_absolute_url)
> class Meta:
> verbose_name_plural = 'Categories'
> class Admin:
> fields = (
> ('Category Details', {'fields': ('name',
> 'description', 'parent',)}),
> ('Category Settings', {'fields': ('active',
> 'generate_feed', 'slug',)}),
> )
> list_display = ('name', 'slug', 'active', 'generate_feed',
> 'num_entries', 'parent',)
> search_fields = ['name','parent']
>
> PUBLISHED_CHOICES = (
> (0, 'Draft'),
> (1, 'Pending Review'),
> (2, 'Published'),
> (3, 'Archived'),
> )
>
> class Entry(models.Model):
> title=models.CharField(max_length=255)
> body=models.TextField()
> user=models.ForeignKey(User)
> slug=models.CharField(max_length=75, null=True, blank=True)
> pub_date=models.DateTimeField('date published')
> published=models.IntegerField(max_length=1,
> choices=PUBLISHED_CHOICES, verbose_name="Publishing Status")
> front_page=models.BooleanField(default=True)
> sticky=models.BooleanField()
> allow_comments=models.BooleanField(default=True)
> truncate=models.BooleanField()
> categories=models.ManyToManyField(Category, limit_choices_to =
> {'active':1})
> def save(self):
> if not self.slug:
> self.slug = slugify(self.title)
> super(Entry, self).save()
> def __unicode__(self):
> return "%s" % self.title
> def was_published_today(self):
> """Flag to show if post was published today"""
> return self.pub_date.date() == datetime.date.today()
> was_published_today.short_description = 'Published today?'
> def is_published(self):
> return bool(self.published==2)
> is_published.short_description = 'Has Been Published?'
> def get_absolute_url(self):
> """Get the URL of this entry to create a permalink"""
> return ('news-detail', (), {
> "slug": self.slug,
> "year":self.pub_date.year,
> "month":self.pub_date.strftime("%b").lower(),
> "day":self.pub_date.day
> })
> get_absolute_url = permalink(get_absolute_url)
> class Meta:
> verbose_name_plural = 'Entries'
> ordering = ['-pub_date']
> class Admin:
> fields = (
> ('Entry Content', {'fields': ('title', 'body',
> 'user', 'categories')}),
> ('Date information', {'fields': ('pub_date',)}),
> ('Publishing Details', {'fields': ('slug',
> 'published', 'front_page', 'allow_comments', 'sticky', 'truncate')}),
> )
> list_display = ('title', 'user', 'front_page', 'sticky',
> 'allow_comments', 'truncate', 'pub_date', 'is_published',
> 'was_published_today', )
> list_filter = ['pub_date', 'categories']
> search_fields = ['body', 'title']
> date_hierarchy = 'pub_date'
> ordering = ('pub_date',)
>
> On Dec 5, 2007 6:28 PM, RajeshD <[EMAIL PROTECTED]> wrote:
>>
>>> Hope that makes sense, as if I can do it this way instead of
>>> calculating it on the view, that would make this inclusion tag so much
>>> easier.
>> What does your Entry model look like?
>>
>
>
>
signature.asc
Description: OpenPGP digital signature

