On Nov 19, 3:46 pm, Jonas Geiregat <[email protected]> wrote:
> I'm trying to save a model and then insert it into a generic relation.
> Having this models.py file:
>
> from django.db import models
> from datetime import datetime
> from django.conf import settings
> from django.contrib.contenttypes.models import ContentType
> from django.contrib.contenttypes import generic
> from django.db.models.signals import post_save
>
> STATUSCHOICE = (
> ('d', 'Working draft'),
> ('p', 'Publish!')
> )
>
> # Create your models here.
>
> class Tag(models.Model):
> name = models.CharField(max_length=20, unique=True)
>
> def __unicode__(self):
> return self.name
>
> class PostBase(models.Model):
> title = models.CharField(max_length=200)
> content = models.TextField()
> pub_date = models.DateTimeField()
> tags = models.ManyToManyField(Tag, null=True)
> status = models.CharField(max_length=1, choices=STATUSCHOICE)
>
> def clean(self):
> self.pub_date = datetime.now()
>
> class Meta:
> abstract = True
>
> def __unicode__(self):
> return self.title
>
> class Post(PostBase):
> pass
>
> class Review(PostBase):
> image = models.ImageField(upload_to="blog/upload")
>
> class BlogContent(models.Model):
> content_type = models.ForeignKey(ContentType)
> object_id = models.PositiveIntegerField()
> content_object = generic.GenericForeignKey('content_type', 'object_id')
>
> class Comment(models.Model):
> content = models.CharField(max_length=300)
> pub_date = models.DateTimeField()
> email = models.EmailField()
> name = models.CharField(max_length=30)
> post = models.ForeignKey(Post)
>
> # Signals
> def post_save_callback(sender, instance, **kwargs):
> bc = BlogContent(content_object=instance)
> bc.save()
> post_save.connect(post_save_callback)
>
> The problem occurs when creating a Post model and saving it:
>
> p = Post(title="foobar", ....)
> p.save()
>
> I get the following error message:
> RuntimeError: maximum recursion depth exceeded
>
> The problem should be located in the post_save_callback function.
>
> Kind regards,
>
> Jonas Geiregat
> [email protected]
You haven't specified that your post_save callback is associated with
the Post model. As a result, it's connected to *every* save -
including the save of the BlogContent object that you create within
the callback. Hence the infinite recursion.
Do this:
post_save.connect(post_save_callback, sender=Post)
to associated the callback just with the Post model.
--
DR.
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
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.