How to correct my signals call?

21 views
Skip to first unread message

prateek gupta

unread,
Nov 21, 2018, 3:57:47 AM11/21/18
to Django users
Hi All,

I have an admin panel built on Django 2.0.6, Mysql 5.7 and Python 3.6.

Now I have written following signal code in signals.py-
from .models import MerchantStores
from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender = MerchantStores, weak=False)
def ensure_store_id_exists(sender, **kwargs):
   print ("check fucntion")
   ex = kwargs.get("instance").store_id
   print ("store id:",ex)
   import requests
   print("url:",URL)
   data = {'Content-Type': 'application/json',
           'app-id': 'APP_ID',
           'secret-key': 'SECRET_KEY',
           'aid': 'PG'}

   r = requests.post(url=URL, headers=data)
   response_text = r.text
   print(response_text)

And in apps.py I have done following settings-
from django.apps import AppConfig

class AdminappConfig(AppConfig):
name = 'adminapp'

def ready(self):
import adminapp.signals

In above signals I am sending a post request to resetPin api.
I need to send the request once form is saved but my signal is posting request before the save method.
I confirmed the above from server logs. Following is the logs I am getting-
[21/Nov/2018 14:18:03] "GET /jsi18n/ HTTP/1.1" 200 3185
check fucntion
store id: 1533
{"success":false,"code":"8050","message":"Merchant not active","response":null}
[21/Nov/2018 14:18:29] "POST /adminapp/merchantstores/add/ HTTP/1.1" 302 0
[21/Nov/2018 14:18:32] "GET /adminapp/merchantstores/ HTTP/1.1" 200 12659
[21/Nov/2018 14:18:34] "GET /jsi18n/ HTTP/1.1" 200 3185

As per above log, my signal is invoked before teh save() method but it should be after the save() method.
Can anyone please help my to find out what I am doing wrong here?

Thanks,
Prateek

Yavin Aalto Arba

unread,
Nov 21, 2018, 4:16:41 AM11/21/18
to django...@googlegroups.com
Can you show us the view where the save() is invoked?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9edd586b-604b-4734-ab1d-28f7fda9d527%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

prateek gupta

unread,
Nov 21, 2018, 4:26:21 AM11/21/18
to Django users
@Yavin Aalto Arba

I am using models.py, admin.py for my view, no any customized forms or view.

screen_3.JPG

screen_2.JPG

screen_1.JPG

PFA screen shot for the same.

Yavin Aalto Arba

unread,
Nov 21, 2018, 4:49:56 AM11/21/18
to django...@googlegroups.com
Something doesn't add up. Can you check the ["created"] keyword? What does it say?

prateek gupta

unread,
Nov 21, 2018, 4:54:33 AM11/21/18
to Django users
Can you please clear where you saw ["created"] keyword?

prateek gupta

unread,
Nov 21, 2018, 5:33:47 AM11/21/18
to Django users
I just printed the value of created as 
kwargs.get("created")

And it''s vaulue is True.

Yavin Aalto Arba

unread,
Nov 21, 2018, 8:25:23 AM11/21/18
to django...@googlegroups.com
if created is true it means that the signal is being sent AFTER the instance was saved(I am assuming this is a new object and not an update!)

weird. 

...what happens when you manually do an MerchantStores.objects.create(...) call with all the mandatory details?

Saurabh Agrawal

unread,
Nov 21, 2018, 8:38:14 AM11/21/18
to django...@googlegroups.com
I am sorry, maybe I am understanding wrong, but isn't log as expected?

Since the signal is run synchronously within the Django request, "POST /adminapp/merchantstores/add/" gets logged, only after the signal has finished running.

Pradeep Singh

unread,
Nov 21, 2018, 9:27:07 AM11/21/18
to django...@googlegroups.com
how to use place in model based form 

Pradeep Singh

unread,
Nov 21, 2018, 9:27:26 AM11/21/18
to django...@googlegroups.com
how to use placeholder in model based form

Yavin Aalto Arba

unread,
Nov 21, 2018, 2:44:28 PM11/21/18
to django...@googlegroups.com

Saurabh Agrawal, I am not sure either. That's why I wanted to check if manually the API results are the same. The created keyword returns true so it HAS to have been run after save().


Reply all
Reply to author
Forward
0 new messages