<filename>profiles/signals.py
from django.db.models.signals import post_save, pre_delete, pre_save
from django.dispatch import receiver
from django.contrib.auth import get_user_model as user_model

from posts.models import Post, image_of_growth_stage

User = user_model()

from .models import Profile, Relationship


@receiver(post_save, sender=User)
def post_save_create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

# @receiver(pre_save, sender=Post)
# def pre_save_new_plant_image(sender, instance, **kwargs):
#     if not instance._state.adding:
#         print("tut")
#         h = image_of_growth_stage(plant_name=instance.plant_name , image=instance.image )
#         h.save()
#         instance.history.add(h)
#
#     else:
#         print("tut1")


@receiver(post_save, sender=Relationship)
def post_save_add_to_friends(sender, instance, created, **kwargs):
    sender_ = instance.sender
    receiver_ = instance.receiver
    if instance.status == 'accepted':
        sender_.friends.add(receiver_.user)
        receiver_.friends.add(sender_.user)
        sender_.save()
        receiver_.save()


@receiver(pre_delete, sender=Relationship)
def pre_delete_remove_from_friends(sender, instance, **kwargs):
    sender = instance.sender
    receiver = instance.receiver
    sender.friends.remove(receiver.user)
    receiver.friends.remove(sender.user)
    sender.save()
    receiver.save()

<filename>profiles/signals.py
from django.db.models.signals import post_save, pre_delete, pre_save
from django.dispatch import receiver
from django.contrib.auth import get_user_model as user_model

from posts.models import Post, image_of_growth_stage

User = user_model()

from .models import Profile, Relationship


@receiver(post_save, sender=User)
def post_save_create_profile(sender, instance, created, **kwargs):
    if created:
        Profile.objects.create(user=instance)

# @receiver(pre_save, sender=Post)
# def pre_save_new_plant_image(sender, instance, **kwargs):
#     if not instance._state.adding:
#         print("tut")
#         h = image_of_growth_stage(plant_name=instance.plant_name , image=instance.image )
#         h.save()
#         instance.history.add(h)
#
#     else:
#         print("tut1")


@receiver(post_save, sender=Relationship)
def post_save_add_to_friends(sender, instance, created, **kwargs):
    sender_ = instance.sender
    receiver_ = instance.receiver
    if instance.status == 'accepted':
        sender_.friends.add(receiver_.user)
        receiver_.friends.add(sender_.user)
        sender_.save()
        receiver_.save()


@receiver(pre_delete, sender=Relationship)
def pre_delete_remove_from_friends(sender, instance, **kwargs):
    sender = instance.sender
    receiver = instance.receiver
    sender.friends.remove(receiver.user)
    receiver.friends.remove(sender.user)
    sender.save()
    receiver.save()

# @receiver(pre_save, sender=Post) # def pre_save_new_plant_image(sender, instance, **kwargs): #     if not instance._state.adding: #         print("tut") #         h = image_of_growth_stage(plant_name=instance.plant_name , image=instance.image ) #         h.save() #         instance.history.add(h) # #     else: #         print("tut1")