Django Signals how to create clone or duplicate objects?

I am building an notification system for send notifications blog subscribers and bog author. I am using Django post_save signals which creating notifications objects. Is it possible to create two separate notification objects or create an clone objects for my BlogCommnet model .

here is my code:

class BlogComment(models.Model):
      blog = models.ForeignKey(Blog,on_delete=models.CASCADE,null=True, blank=True)
      #my others fields 
      #here signals starting
      def user_comment(sender, instance, *args, **kwargs):
            comment= instance
            blog = comment.blog
            sender = comment.user
            commnet_notes = comment.rejected_comment_notes
            comment_text = comment.comment
            
            if sender != blog.author and comment.is_published == "pending":
               notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250], notification_type="New Comment")
               notify.save()

post_save.connect(BlogComment.user_comment, sender=BlogComment)  

Right now it’s creating only one notifications objects. see the picture: enter image description here

I want it will create another clone objects of it’s orginal. is it possible ???

I tried this for create an duplicate objects but didn’t work:

if sender != blog.author and comment.is_published == "pending":
               notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250], notification_type="New Comment")
               notify.save()
               notify2 = notify
               notify2.save()

With this statement:
notify2 = notify
you’re not making a copy of the object - you’re assigning that same object to a different name. Both names, notify and notify2 are referring to the same object.

Now, even if you copy the object, this still isn’t going to work.

When you create a notification object here:
notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250], notification_type="New Comment")

you’re not assigning a value for the primary key. (Since you haven’t shown your Notification model, I don’t know for certain the name of that field, but I’ll assume for this discussion that it’s the default, "id".)

When you save that object:
notify.save()
one of the functions performed is that the database assigns the value of the id field, and sets it in the object.

If you make a complete copy of the object, it’s going to copy that id field, and so saving it is going to overwrite the previously saved version.

What you want to do here is create a new instance of the Notifications object.

1 Like

KenWhitesell Thanks for your details explanation. How to get id or primary key of notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250], notification_type="New Comment") for save an duplicate???

For accessing that field, see the docs - Models | Django documentation | Django.

However, what you want to do here is create a new instance of the Notifications object.

Should I add this id = models.BigAutoField(primary_key=True) in my model??? then somethings like this
notify2 = notify.id, notify2.save()???

What specific line of code creates your first instance?

I want to create a new instance or duplicate object because I want to add delete functionality for my author and customer. Right now if author delete the object then it automatically deleting from customer site because my database have only one objects for sender(who is commenter) and receiver(who is blog author). If I have 2 objects then I will appoint one object for sender and another for receiver. This line of code create my first instance

notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250], notification_type="New Comment") then `notify.save()’ for save objects

Ok, so if this line creates an instance:
notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250], notification_type="New Comment")

then copying that line (changing the assignment from notify to notify2), will create your second instance.

1 Like

Thanks it worked but why didn’t work notify2 = notify then notify2.save() as you said it assigning that same object to a different name. if it assigning that same object then why it can’t save the same object again???

What is the purpose of a Primary Key in a database?

I think Primary Key keep unique record for each objects or value.

So what happens when you try to save two objects with the same primary key?

KenWhitesell I understood. My previous problems solved. Now it’s creating duplicate objects of it’s original. Now my problem is author and customer seeing the two objects at a time as my database saving duplicate objects of it’s original. is it possible to show only original objects to author and duplicate objects to customer. so they will see only one objects in html template??? see the picture:

should I post new topic for this problems ?

Sure it’s possible. Take some time, think about what data is available and being propagated between your components, and figure out how you can manage this.

1 Like

KenWhitesell I think I need to be do somethings in my queryset.
Notifications.objects.all().filter(sender=user).order_by('-date') #This queryset for coustomer I need to be find duplicate then appoint first duplicate to customer and second duplicate to author Notifications.objects.all().filter(receiver=user).order_by(’-date’) #This queryset for author Can you please sent me any document link where I can read how to identify duplicate objects in django ??

I find out an solution by adding default value in my models then using filter in my views.

duplicate = (('commenter','commenter'),('author','author')), 
duplicate_value = models.CharField(choices=duplicate,default='commenter',max_length=250)

I am using commenter for sender and 'author for receiver. here is the signals code:

if sender != blog.author and comment.is_published == "pending":

   notify = Notifications(blog=blog, sender=sender, receiver=comment.blog.author,text_preview=comment_text[:250], notification_type="New Comment") #here I am not passing duplicate value because I already set an  duplicate value in my models.

   notify.save()

   notify2 =Notifications.objects.create(blog=blog, sender=sender, receiver=comment.blog.author, text_preview=comment_text[:250], notification_type="New Comment",duplicate_value="author")

   notify2.save()

then using filter in my views:
#this filter for sender(who is commenter)
Notifications.objects.all().filter(sender=user,duplicate_value="commenter")

#This filter for receiver(who is author)
Notifications.objects.all().filter(sender=user,duplicate_value=“receiver”)

Hope it will helps others. KenWhitesell if you have any others better solution then please don’t forget to write.