Django call_command from Admin

111 views
Skip to first unread message

Aziz Mek

unread,
Oct 20, 2022, 12:05:37 PM10/20/22
to Django users
Hi All,

I was wondering  if you have come across the following:

I have a field in the model that's empty, when the user fills it up and clicks Save, it
should trigger/call a management Command (This command is already build that sends emails ), Django docs say i can use call_command but not sure how to implement it with the save

I am just after the trigger really when the save takes place

Many thanks in advance

Kind regards
Aziz


Tega Ukavwe

unread,
Oct 20, 2022, 12:10:04 PM10/20/22
to django...@googlegroups.com
Do a little read up on post_save signal.

Below is a link to a good start point for you:

I hope this helps you in your search.
Best Wishes Aziz.

--
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 view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a0ca69f0-6065-4b86-a977-cfb6dcab8fd7n%40googlegroups.com.

Aziz Mek

unread,
Oct 20, 2022, 12:17:41 PM10/20/22
to Django users
Hi 

Thank you very much for the  swift response and for the link, much appreciated.

I did have a look at that article this morning but it is using signals which will only add one more layer to my stack and
i think essentially does what call_command will do anyway.

I really appreciate your response

PS: I couldn't see your name in the email hence why I didn't address you by it, apologies

Kind regards
Aziz 

Tega Ukavwe

unread,
Oct 20, 2022, 12:30:21 PM10/20/22
to django...@googlegroups.com
The name's Tega,

If I may ask you, what makes you favour making a management command as opposed to creating a simple post_save signal in your present situation.

From the django documentation, post_save is literally called on every save, something similar to the Publisher Subscriber pattern.

I'd really appreciate your response on this.

Regards,
Tega Ukavwe
Python Like English

On Thu, Oct 20, 2022 at 5:06 PM Aziz Mek <azi...@gmail.com> wrote:

Aziz Mek

unread,
Oct 20, 2022, 7:09:19 PM10/20/22
to Django users
Hi Tega, 

Nice to meet you and apologies for the delay in getting back to you, 

Signals are both the best and worst. Yes, they'd "solve" this problem. Though so would overriding the model's save method or the admin's save method.

But the issue I find with them is that because they are decoupled from the model in a year, or the next dev, someone won't have a clue why an email is being sent or how to troubleshoot it.

My general guideline for using them is as a last resort. They are the lazy way of solving problems but will almost always cause different problems in the future.

Also you now have to be keenly aware of the circumstances where signals don't fire, also signals in my case will block the thread as the number of subscribers is big, 

Please let me know if you have different point of view, i am not dismissing any opinions


the management command is already written by another developer and i am trying to leave as few footprint in the code as I can, I am not even sure

if it is the right way to go about this. 

Will let you know how it pans out if you want.


Kind regards

Aziz Meknassi


Muhammad Juwaini Abdul Rahman

unread,
Oct 20, 2022, 8:28:52 PM10/20/22
to django...@googlegroups.com

I think it's quite straightforward. Just add call_command(command) after (or before the save() line) in your code.

Depending on whether you're using FBV or CBV, you need to pinpoint where exactly the save() function resides in your code.

Aziz Mek

unread,
Oct 21, 2022, 7:37:44 AM10/21/22
to Django users
Hi,

Thank you very much for your input,

I will try this

Kind regards
Aziz

Aziz Mek

unread,
Oct 21, 2022, 7:38:55 AM10/21/22
to Django users
Hi Tega,

I have just come across this article today about signals and thought to share, hopefully it will answer your question from yesterday more clearly


Kind regards
Aziz

On Thursday, October 20, 2022 at 5:30:21 PM UTC+1 justt...@gmail.com wrote:

Mohammed Mohamed

unread,
Oct 21, 2022, 1:59:41 PM10/21/22
to Django users
Hello there Aziz, are you a beginner in Python? I'm one. Could we make some Google meet arrangements so that I can learn from you.

Mohammad Ehsan Ansari

unread,
Oct 21, 2022, 2:32:02 PM10/21/22
to django...@googlegroups.com
Please refer the signal concept in django you will get how to implement it

Paul Kudla

unread,
Oct 24, 2022, 9:03:08 AM10/24/22
to django...@googlegroups.com

you need to basically add a hook (save class) to your save model and
process stuff along the way.

you will need to modify this example but it's how i update info when
saving an scom email user for dovecot.

you can also override the admin class as well with something simaliar

Note :
super(EmailUsers, self).save(*args, **kwargs) #Save the email changes

is extemely important as stuff will process but any field updates will
not save without the above line......

example :


def save(self, *args, **kwargs): #This will create an email account if
nessesary.
emailaddress = str(self.username) #Set the email address to see if we
need to create the mailbox?
emailpassword = str(self.password) #Set the password (used for check
alias)

#prefill username/domain is blank
if self.domain == None or str(self.domain) == '' :
self.domain = self.username.split('@')[1]

if self.source == None or str(self.source) == '' :
self.source = self.username

if self.destination == None or str(self.destination) == '' :
self.destination = self.username




domain = str(self.domain)
send_settings = str(self.send_settings)
if self.send_settings == None :
send_settings = ''
self.send_settings = ''
dontupdate = self.dontupdate
self.dontupdate = False

#set dovecot directory & split user / domain
if self.username.split('@')[1] == 'preload.scom.ca' :
self.home = '/data/dovecot/users/%s/%s' %(
self.domain,self.destination.split('@')[0] )
else:
self.home = '/data/dovecot/users/%s/%s' %( self.domain,self.destination )

self.user = self.username.split('@')[0]

#Set alias Flag
if self.password == 'alias' or (self.source != self.destination) :
self.alias_flag = True
#self.sieve_forwards = ''
#self.vacation_active = False
else :
self.alias_flag = False

self.username_search = self.username
self.username_search = self.username_search.replace ('.','')
self.username_search = self.username_search.replace ('@','')


super(EmailUsers, self).save(*args, **kwargs) #Save the email changes

i email with something like :

#Try to get info for this account
if dontupdate == False :

imap_test = Dovecot_Command ('INFO',self.username) #do i have this
account ?

if 'BAD' in imap_test.answer :
try : #Try to Create the account, note that the db must be updated
properly before it will work
imap_create = Dovecot_Command ('CM',self.username)
if 'OK' in imap_create.answer :
send_subject = 'Email Account Created : %s' %(str(self.username) )

except :
send_subject = 'Error Account : %s' %(str(self.username) )
pass

else :
send_subject = 'Email Account Updated : %s' %(self.username)

#Send update email

send_from = 'mon...@scom.ca'
send_files = []
send_to = ['mon...@scom.ca']
send_text = '\n\n'+ send_subject + '\n'
sendmail(send_from,send_to,send_subject,send_text,send_files) #Send
the warning email




Happy Monday !!!
Thanks - paul

Paul Kudla


Scom.ca Internet Services <http://www.scom.ca>
004-1009 Byron Street South
Whitby, Ontario - Canada
L1N 4S3

Toronto 416.642.7266
Main 1.866.411.7266
Fax 1.888.892.7266
Email pa...@scom.ca

On 10/21/2022 2:30 PM, Mohammad Ehsan Ansari wrote:
> Please refer the signal concept in django you will get how to implement it
>
> On Thu, 20 Oct, 2022, 9:36 pm Aziz Mek, <azi...@gmail.com
> <mailto:azi...@gmail.com>> wrote:
>
> Hi All,
>
> I was wondering  if you have come across the following:
>
> I have a field in the model that's empty, when the user fills it up
> and clicks Save, it
> should trigger/call a management Command (This command is already
> build that sends emails ), Django docs say i can use call_command
> but not sure how to implement it with the save
>
> I am just after the trigger really when the save takes place
>
> Many thanks in advance
>
> Kind regards
> Aziz
>
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a0ca69f0-6065-4b86-a977-cfb6dcab8fd7n%40googlegroups.com <https://groups.google.com/d/msgid/django-users/a0ca69f0-6065-4b86-a977-cfb6dcab8fd7n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAF8ux6Li0BjKS2zvQuPWL6zq-R6Buo9wa1fBx68m4ESpTptHug%40mail.gmail.com <https://groups.google.com/d/msgid/django-users/CAF8ux6Li0BjKS2zvQuPWL6zq-R6Buo9wa1fBx68m4ESpTptHug%40mail.gmail.com?utm_medium=email&utm_source=footer>.
>
> --
> This message has been scanned for viruses and
> dangerous content by *MailScanner* <http://www.mailscanner.info/>, and is
> believed to be clean.

Paul Kudla

unread,
Oct 24, 2022, 9:33:10 AM10/24/22
to django...@googlegroups.com

example for admin.py

def save_model(self, request, obj, form, change): #This will save create
#Check to see if this will be an invoice copy
copy = False
current_invoice = obj.invoice_number
if obj.new_invoice : #clear the flag and save the current object then
set new & date
obj.new_invoice = False
obj.save()
copy = True




Happy Monday !!!
Thanks - paul

Paul Kudla


Scom.ca Internet Services <http://www.scom.ca>
004-1009 Byron Street South
Whitby, Ontario - Canada
L1N 4S3

Toronto 416.642.7266
Main 1.866.411.7266
Fax 1.888.892.7266
Email pa...@scom.ca

> --
> 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
> <mailto:django-users...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a0ca69f0-6065-4b86-a977-cfb6dcab8fd7n%40googlegroups.com <https://groups.google.com/d/msgid/django-users/a0ca69f0-6065-4b86-a977-cfb6dcab8fd7n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>
> --

Aziz Mek

unread,
Oct 25, 2022, 12:57:48 PM10/25/22
to Django users
Hi Paul,

Many thanks for this, much appreciated,

I will have a look and try to implement it,
will let you know.

Kind regards
Aziz Meknassi

Aziz Mek

unread,
Oct 25, 2022, 12:59:43 PM10/25/22
to Django users
Thank you Mdehs, 

Much appreciated for the advice,
I try to avoid signals as they are thread huggers but maybe i will 
or try to circumvent them somehow

Kind regards
Aziz

Aziz Mek

unread,
Oct 25, 2022, 1:02:44 PM10/25/22
to Django users
Hi Jualam,

We are all beginners my friend but i am sure we can learn from each other,
Yes by all means, please email me and we'll try to get something sorted

Kind regards
Aziz Meknassi

Aziz Mek

unread,
Oct 25, 2022, 1:18:08 PM10/25/22
to Django users
Thank you for getting back to me,
I read that in the documentation but it lacks clarity
however your comments may help, i will give it ago and see

I appreciate your help

Kind regards
Aziz

On Friday, October 21, 2022 at 1:28:52 AM UTC+1 juw...@gmail.com wrote:

Mike Oliver

unread,
Oct 25, 2022, 7:16:00 PM10/25/22
to django...@googlegroups.com
Aziz,

I had a similar need and I created my own middleware that checked the request path and did the call when I got a match.  





Mike Oliver Founder, Open 4 Business Online
Tel: +1(951)260-0793 | Mobile:*NEW 639479927462
US Toll free: 1-800-985-4766 *NEW
http://www.o4bo.com
Mas marunong akong umunawa ng salitang tagalog kaysa magkapagsalita nito
Facebook Twitter LinkedIn AngelList Blogger eBay YouTube Google Plus Page
Contact me: Google Talk mikeol...@open4businessonline.com Skype MikeOliverAZ



Aziz Mek

unread,
Oct 25, 2022, 9:14:39 PM10/25/22
to Django users
Hi Mike,

Many thanks for the heads and up and confirmation that it can be done, much appreciated

Kind regards
Aziz

Reply all
Reply to author
Forward
0 new messages