django-signals

Apps aren't loaded yet. with signals

我与影子孤独终老i 提交于 2019-12-04 03:04:47
I have an app not ready style error when i use signal. I think this is due to the User auth in the profile model , from what i've see using google there is some issue with the user auth. i think that the error is here : class Profile_User(models.Model): user = models.OneToOneField(User, null=True) but i have no clue on how to solve it. The model with the signal : from django.db import models from Modif_Box.models import Modif_Box_User from Profile.models import Profile_User from Post.models import Post_User from django.utils import timezone #from Modif_Box.get_models import get_modif_box

How can I send signals from within Django migrations?

左心房为你撑大大i 提交于 2019-12-04 02:57:57
问题 I use Django 1.7 migrations, and in particular, want to populate a newly-created database with initial data. Thus, I use a data migration for this. It looks like this: def populate_with_initial_data(apps, schema_editor): User = apps.get_model("auth", "User") new_user = User.objects.create(username="nobody") class Migration(migrations.Migration): ... operations = [ migrations.RunPython(populate_with_initial_data), ] At the same time, I want to have an instance of the UserDetails model for

django m2m_changed with custom through model

ε祈祈猫儿з 提交于 2019-12-04 02:35:40
In Django I do have two models "Author" and "Publication" that are connected with a Many-to-Many-Field, so that I can assign different authors to a publication. Additionally, I have to use a custom through-model "Authorship" to define the correct order. class Author(models.Model): first_name = models.CharField(max_length=48) ..... class Authorship(models.Model): author = models.ForeignKey(Author) publication = models.ForeignKey('Publication') order_of_authorship = models.IntegerField(default=1) class Publication(models.Model): title = models.CharField(max_length=128) authors = models

Django: Before a model is updated, I'd like to “look at” its previous attributes

时光总嘲笑我的痴心妄想 提交于 2019-12-03 10:48:39
When an update/create is performed on a Django model ( .save() ) I would like to be able to "step in" and compare some particular attributes to what they were set to previously (if they previously existed at all). I'm thinking Pre-Save Signals , with a look-up to the original model doing a .objects.get(instance.id) , but that feels wasteful. Also, has the validation already happened in pre_save() ? about model validation : Note that full_clean() will not be called automatically when you call your model’s save() method Then, about the pre-save signal , note that you get the instance that is

handle `post_save` signal in celery

北慕城南 提交于 2019-12-03 09:01:05
问题 I have a rather long running task that needs to be executed after inserting or updating an specific model. I decided to use post_save signal instead of overriding save method to reduce coupling. Since Django signals are not asynchronous I had to do the long running job as a Celery task (which we already have in our stack). A simplified version of my signal handling function is as follows: @receiver(post_save, sender=MyModel) def my_model_post_save(sender, instance, **kwargs): handle_save_task

Is it possible to selectively suppress a post_save (or other) signal in Django?

拈花ヽ惹草 提交于 2019-12-03 07:46:08
I'm wondering whether it's possible to selectively suppress a Django signal (such as post_save or post_init ) on object creation, or, alternatively, send it certain parameters. What I have is a User object, which can be created in many different ways and places in my code. So to automatically assign a custom Profile object to each User , I use the post_save signal. However, in one specific case, there is extra information that I want to bind to the created Profile object. Passing it as arguments to the post_save signal would be great, but it doesn't look possible. The other option is to

Django: Obtaining the absolute URL without access to a request object

一笑奈何 提交于 2019-12-03 05:04:58
I have a model like the one below. When an instance is created, I want to send out an e-mail to an interested party: class TrainStop(models.Model): name = models.CharField(max_length=32) notify_email = models.EmailField(null=True, blank=True) def new_stop_created(sender, instance, created, *args, **kwargs): # Only for new stops if not created or instance.id is None: return # Send the status link if instance.notify_email: send_mail( subject='Stop submitted: %s' % instance.name, message='Check status: %s' % reverse('stop_status', kwargs={'status_id':str(instance.id),}), from_email='admin@example

Is there a way to list Django signals?

怎甘沉沦 提交于 2019-12-03 01:25:51
Is there a way to see which signals have been set in Django? It's not really exposed in docs but Signal is just a class that contains a list of receivers which are called on event. You can manually check this list: from django.db.models.signals import * for signal in [pre_save, pre_init, pre_delete, post_save, post_delete, post_init, post_syncdb]: # print a List of connected listeners print signal.receivers priestc There's a django app called django-debug-toolbar which adds a little toolbar at the top of all django served pages providing info related to the backend of the page's rendering,

handle `post_save` signal in celery

随声附和 提交于 2019-12-02 23:06:25
I have a rather long running task that needs to be executed after inserting or updating an specific model. I decided to use post_save signal instead of overriding save method to reduce coupling. Since Django signals are not asynchronous I had to do the long running job as a Celery task (which we already have in our stack). A simplified version of my signal handling function is as follows: @receiver(post_save, sender=MyModel) def my_model_post_save(sender, instance, **kwargs): handle_save_task.apply_async(args=(instance.pk,)) Also, because the job is done asynchronously I passed the primary key

Django signal emitting once, received twice — Why?

烈酒焚心 提交于 2019-12-02 21:50:24
I'm working with Django signals, but they seem to be received twice, even if emitted once. Here's the code I'm working with (it's a simple wrapper to use Uploadify with Django)... # Signal-emitting code... emits whenever a file upload is received # ---------------------------------------------------------------- upload_recieved = django.dispatch.Signal(providing_args=['data']) def upload(request, *args, **kwargs): if request.method == 'POST': if request.FILES: print 'sending signal' upload_recieved.send(sender='uploadify', data=request.FILES['Filedata']) return HttpResponse('True') # Signal