save() prohibited to prevent data loss due to unsaved related object 'committee'

431 views
Skip to first unread message

Yaniv Mirel

unread,
Oct 20, 2015, 2:02:27 PM10/20/15
to Django users
Hi,

I am getting this error when running model post save signal.
The error traceback is for gr.save()

it seems like the committee model is not saving before it need to be assigned to GroupRole model as a FK.
this is the post_save signal code.


@receiver(post_save, sender=Community)
def set_default_reasons(sender, instance, created, dispatch_uid='set_default_reasons', **kwargs):
if created:
committee = Committee(community=instance, name='Board', slug='main')
committee.save()

for group_role in settings.DEFAULT_GROUP_ROLES:
gr = GroupRole(committee=committee, role=instance.roles.get(title=group_role), group=instance.groups.get(title=group_role)) 
gr.save() 
 
 Thanks.

Yaniv Mirel

unread,
Oct 20, 2015, 2:22:24 PM10/20/15
to Django users
I've found out that this error is happening because I also have a def save() function on the committee model.
So I am not sure if this is a bug or I am not suppose to have it like that.

Tim Graham

unread,
Oct 20, 2015, 2:32:20 PM10/20/15
to Django users
What does the Committee.save() method look like? You have no problem if you remove that method?

Yaniv Mirel

unread,
Oct 21, 2015, 8:12:24 AM10/21/15
to Django users
The Committee.save() is:
def save(self, *args, **kwargs):
       if not self.slug:
       self.slug = 'committee-%s' % self.id

And yes, after removing that save function everything works fine.

Tim Graham

unread,
Oct 21, 2015, 8:59:19 AM10/21/15
to Django users
You need to call super() in your custom method to ensure the object is actually saved to the database.
Reply all
Reply to author
Forward
0 new messages