Say I have two Django apps, abc and xyz. The abc app is a package that should be installed using pip3, and xyz is some custom app using features from abc.
How can xyz provide a delegate method to be used in a view in abc? Say abc need to send a message, it doesn't care if it should be send via SMS, email or avian carrier, so if xyz could provide a delegate method, this method could be called from abc, without abc caring about the implementation.
Python handles delegates smoothly, but how do I wire it up in a Django view?
I know I can use a message queue or a callback url, but it seems a bit strange to me that there isn't an easier way to do this.
Signals can help you achieve what you're looking for.
From Django's documentation on the subject:
Django includes a “signal dispatcher” which helps allow decoupled applications get notified when actions occur elsewhere in the framework. In a nutshell, signals allow certain senders to notify a set of receivers that some action has taken place. They’re especially useful when many pieces of code may be interested in the same events.
Here are a few other resources on the subject:
Using signals in your Django app
A simplified explanation
Related
I have read lots of documentation and articles about using signals in Django, but I cannot understand the concept.
What is the purpose of using signals in Django?
How does it work?
Please explain the concept of signals and how to use it in Django code.
The Django Signals is a strategy to allow decoupled applications to get notified when certain events occur. Let’s say you want to invalidate a cached page everytime a given model instance is updated, but there are several places in your code base that this model can be updated. You can do that using signals, hooking some pieces of code to be executed everytime this specific model’s save method is trigged.
Another common use case is when you have extended the Custom Django User by using the Profile strategy through a one-to-one relationship. What we usually do is use a “signal dispatcher” to listen for the User’s post_save event to also update the Profile instance as well.
I want to send emails in django rest framework, for example when someone creates an account,or comments something and etc. I found this in django documents. But I don't know where to use it. maybe in Viewset but in which method.
so what is the best way and where is the best place to send emails in Django Rest Framework?
I would like to give you couple of suggestions(Of course, with reasons):
That is synchronous. Maybe that would be not desired/efficient. I would suggest you to use something like this package (django-anymail) , this will decouple your email sender. Assume now you're using AWS SES, tomorrow you want to switch to sendgrid, then you'll only need to change setting variables with this.
Assuming User, Comment each of these are separate models. I would suggest you to override the save() method of these models and send email from there. I am not suggesting signals because I feel (fully personal view) that signals are to be used when access to the source is not possible, like some event in 3rd party library, etc.
Assuming that "Creating an account" and "commenting on something" both create some model instance, you could use existing model signals.
If you need to catch events that don't already send a signal, you will have to define new signals and send them from the appropriate place (which depends on what you're interested in so no 'one-size-fits-all' answer here).
Also if you have more than one mail to send at a time you may want to consider using some async task queue to send the emails - this avoids delaying the response (django signals are NOT async).
You can register sending function to some django signals or You can call it in Class-based Views or Function Based Views.
I have an django app, a model which stores data entered via a web interface by a user.
I need to consume an third party REST api when viewing / saving a model instance. I know how to do this but, what I am unsure about is where this code should live with the django app.
my gut is to put this code with in the model class, but then you could also use a view... I am just not sure.
How has this been done before, there are lots of posts asking how to do this, but none stating best place to put the code.
any guidance would be gratefully received.
Cheers
This is a subjective question, so here is a subjective answer.
First of all, ensure that any code that interacts with this external REST API resides in a separate module. E.g, if you're grabbing word definitions from a dictionary API, all the code that talks to this API should ideally be in a separate dictionary module, which you can then import into your view.
Secondly, your models.py should merely declare your application's data model and define the operations on this model, and little else. They should not be concerned with request/response cycles, reading files, rendering templates, making HTTP calls, or anything else. By this logic, you should be making these REST API calls from your views and, if required, passing the returned data into your models.
And finally, think twice about making REST calls from your Django app. Python does synchronous (blocking) I/O by default, which means as long the app is waiting for the REST call to finish, it can't service any incoming HTTP requests. This is not an issue if you don't have too many users, but it's something to keep in mind for apps that need to scale. You might want to look into async I/O libraries for Python.
I've done research on Django Postman and it seems to be the most solid private user to user messaging platform out there. I've looked at the Django Postman documentation but it's very template orientated. For developers who use Django as a back end and only care about the views.py and urls.py, the documentation doesn't say much.
I did however find this: https://bitbucket.org/psam/django-postman/src/6ff9fdf9c33f7365a7235a789af2e47f47d9c4fa/postman/views.py?at=default
It seems pretty promising so I'm going to give it a try. My only issue is how can one set up the postman views in views.py and the urls in urls.py to create a messaging system similar to Facebook's?
(ie. A thread like messaging conversation system, a central inbox where all the messages come together from each user showing the last message from each user, messages in the inbox are sorted by conversation rather than the message, the time of the last message sent, allowing multiple recipients)
Below I've posted a picture of Facebook's messaging platform. This is what I am essentially trying to achieve with Django Postman.
Facebook Example http://screenshots.en.sftcdn.net/en/scrn/73000/73077/facebook-19-371x535.jpg
If you have any pointers, hints and ideas on how I can set up the views.py, I would greatly appreciate it! Thank You
I've run into this issue before.
You need to strictly override some of the views in there by clonning/forking the project and install it from your own location, because as you noted, postman is template-oriented because it's meant to only get the needed templates configured and a few settings. I mean, the backend is meant to work as is.
What you need to do is override stuff like:
Message model's recipient field to be a ManyToManyField
customize the views based on your needs and be careful with Message.replied_at
make sure you allow a user to reply to their own messages (by default, it was not allowed when I ran into this, not sure now)
Depending on your needs, maybe you'll want to override something else, but this is a good start. If you need it facebook-like, you'll need to use some push libraries as Pusher or Juggernaut, maybe you're interested in them also.
Good luck! :)
I'd like to send an activation link to a new registered user. Should I write my function in my models.py or managers.py?
It's always confused to me to know where put the function, even after reading the documentation.
None of them, models and managers are related to application data. Sending emails are related to the logic of your app: actions, decisions, answers ... so you should do this in a view.
If you need to save time, you can use Django Registration as #karthikr suggests to you, this app is a good wrapper for reaching this aim.
I've assumed you need to send the link in the moment that the user has registered, but if you want to do this in other moment you can use a scheduled task: a django cron, an external python process or ...; that is up to you: your porpuse, the design of your app.
This blog could help you understand the use of managers better.
I would put the activation link in managers, because it would be easier to manage the various activities around it - resend activation link, validation, etc. It could be done with models too, but managers make it more modular.
Django Registration is quitely widely used for registration - you could see how it is implemented there as well.
Yes , you can write your customized function in models.py but not sure about manage.py .
I have used signal method in models.py to for mailservice facility.