Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Jan. 15, 2011 15:21:41

m.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Signals and foreign key


Hey,
Im trying to get the signal pre_save to work on my model but there
seems to be some problem with a foreign key or something. My model is
like this:

class BlogEntry(models.Model):
headline = models.CharField(max_length=100)
content = models.CharField(max_length=2000)
written_by = models.ForeignKey(User)
date = models.DateTimeField(auto_now=True)
project = models.ForeignKey(Project)

And here is where the form is processed and saved:

def form_valid(self, blogform): # post a blogentry
blog_entry = blogform.save(commit=False)
blog_entry.written_by = self.request.user
blog_entry.project = blog_entry.project #debugging shows that this
is a valid instance of the Project model
blog_entry.save()

return TemplateResponseMixin.render_to_response(self,
self.get_context_data())

And here's my signal reciever function

@receiver(post_save, sender=BlogEntry)
def project_blog_entry_signal(sender, **kwargs):
log = Log()
log.project = sender.project.field
log.content_type = Log.content_new_blogentry
log.save()

And here's the exception:
Cannot assign "<django.db.models.fields.related.ForeignKey object at
0x21eef86c>": "Log.project" must be a "Project" instance. @the line
-----> log.project = sender.project.field in
project_blog_entry_signal() funktion

The sender object doesn't seem to pass the project as a model instance?

--
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
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 16, 2011 13:54:58

m.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Signals and foreign key


Solved. Look athttp://stackoverflow.com/questions/4704924/django-signal-callback-function-missing-foreign-on-model--
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
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support