0

I have a System model in one app that I'd like to connect to a Queue model in another app (django-helpdesk). If no Queue object is selected for a System object, I'd like to initialize a new Queue object, using information from the System to generate it.

I've covered all of the required fields for the Queue, and yet this doesn't seem to work. It doesn't throw an error; it just never generates a new Queue object. Can anyone spot the issue in question, or recommend another way of covering this?

#models.py

import helpdesk

....

class System(models.Model):
    queue = models.ForeignKey(
        helpdesk.models.Queue,
        blank = True,
        null = True,
        editable = True,
        verbose_name = _('Queue'),
    )

    def __save__(self, *args, **kwargs):
        if not self.queue:
            slug = slugify(self.name)
            queue = helpdesk.models.Queue(title=self.name, slug=slug)
            queue.save()
        self.queue = queue.pk
        super(System, self).save(*args, **kwargs)

EDIT: For reference, there's an additional mistake beyond using the double underscores for the save() method. The line self.queue = queue.pk should instead be self.queue = queue.

Erik D.
  • 27
  • 7

3 Answers3

1

You should override save() method, not __save__(), see examples:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
1

Rename:

def __save__(...)

to

def save(...)
Joseph Victor Zammit
  • 14,760
  • 10
  • 76
  • 102
1

No need for the double underscores in the save() method. That's for Python magic methods only.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895