Meekey
Meekey

Reputation: 59

Function save models django

There is an application model. The meaning of it is quite simple, the Author submits the application, the site administrator appoints the Contractor for its execution, which after completing the received application completes it. Everything seems to be fine, the application model was created, made sure that after the administrator chooses the Contractor the application immediately received the status of closed.

But here is one problem that I can not cope with when the Administrator closes the application (Status "Completed"), then the application does not acquire the status Completed, because the Artist is assigned to it because of the save function in the model.

How to make the status of the application complete, even if a contractor is appointed to this application? In advance I ask you to excuse me for such a presentation of the question, I'm still a beginner. Many thanks to all those who can help)

models.py

class Application(models.Model):
    STATUS_CHOICES = (
        ('In_the_work', 'В работе'),
        ('New', 'Новая'),
        ('Complited', 'Завершена')
    )

 author = models.ForeignKey('auth.User', related_name = '+', verbose_name = 'Автор')
 title = models.CharField(max_length=50, verbose_name = 'Заголовок')
 text = models.TextField(verbose_name = 'Описание проблемы')
 room = models.CharField(max_length = 4, verbose_name = 'Кабинет')
 published_date = models.DateField(blank=True, null=True, default = 
 datetime.datetime.now, verbose_name = 'Дата')
 status = models.CharField(max_length=15, choices=STATUS_CHOICES, 
 default='Новая', verbose_name = 'Статус')
 owner = models.ForeignKey('auth.User', related_name = '+', null = True, 
 blank = True, limit_choices_to={ 'groups__name': 'Техническая поддержка'}, 
 verbose_name = 'Исполнитель') 

def save(self, *args, **kwargs):
    if self.owner != None:
        self.status = 'In_the_work'

    super(Application, self).save(*args, **kwargs)

Upvotes: 0

Views: 92

Answers (1)

T.S.
T.S.

Reputation: 302

I am not sure about what you mean about an "Artist" being assigned to the application.

When you mean that a contractor is assigned to it, I am thinking you are referring to assigning an "owner" (owner field value) to the Application model. If that is the case, the error is that, in your save method you are checking for the owner being different than None, in which case you always overwrite the status to "In_the_work".

So maybe a solution would be something like:

def save(self, *args, **kwargs):
    if self.status != 'Complited' and self.owner is not None:
        self.status = 'In_the_work'
    super(Application, self).save(*args, **kwargs)

In that way, you are only overwritting status if status is different than complited in the first place, not everytime the owner is not None.

Thank you.

Upvotes: 1

Related Questions