Hi darryl, Ok I tried as you said above, but it gives me two problems:
When I try to make my first category to use for entries I get this error: <class 'digitalspaghetti.blog.models.Entry> instance needs to have a primary key value before a many-to-many relationship can be used. So ok, it's looking for an Entry instance to exist, but the workflow should be to create a category first. So I did a manual insert into the database creating an 'Undefined' category with a pk of 1. However when I then try view the admin screen, I get 'Category matching query does not exist'. From the errors I can see it finds it in the result set, but when I look at the traceback in the kwargs the params are: {'id__exact' : 0} However that's just problem 1. Problem two is in the entry model. I create a first post, and the Undefined category shows up fine in my many to many list - when I try save the entry I get: 'ManyRelatedManager' object is not iterable I tried making your line: for category in self.categories.all, however this still doesn't work. Any ideas??? On 06/12/2007, Darryl Ross <[EMAIL PROTECTED]> wrote: > 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? > >> > > > > > > > > > -- Tane Piper Blog - http://digitalspaghetti.me.uk AJAX Pastebin - http://pastemonkey.org This email is: [ ] blogable [ x ] ask first [ ] private --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---