0

I have a notice model that creates a notice, I would like to schedule notices to be created at certain dates. Basically, I mean using another model I want to schedule and create the notices at that date specified in the schedule notice.

How would I do this? What's the easier road to achieve this? All I can think of is rabbitmq and celery. And i don't even know how it would create a notice at a dynamic date.

class Notice(models.Model):
    title = models.CharField(max_length=200)
    contentt = RichTextField(blank=True, null=True)
    slug = models.SlugField(unique=True)


    def __str__(self):
        return self.title

class Schedule(models.Model):
    title = models.CharField(max_length=200)
    contentt = RichTextField(blank=True, null=True)
    date = models.DateTimeField()

    def __str__(self):
        return self.title

Above is a models.py example to clarify what o mean, I actually don't have a model. I use an api to read and write to a collection in an external mongodb.

So for every schedule created, it uses the date on it to create a notice that contains the title and the content of the schedule model. So different persons can create schedules for notices and at that particular date it creates it. How do I do this?

blockhead
  • 364
  • 3
  • 18

1 Answers1

1

I assume that you mean for every schedule objects are created, it would automatically create the notice objects.

If that is what you mean. You just need to use the built-in signals.

from django.dispatch import receiver
from django.db.models.signals import post_save

@receiver(post_save, sender=Schedule)
def post_save_schedule(sender, instance, created, **kwargs):
   if created:
      Notice.objects.create(
         title = instance.title, 
         content = instance.content
      )
Hoan Tr
  • 61
  • 1
  • 4