Hi,

Thanks for the advice, as usual with me, it's thrown up more questions... :D

On 26 January 2011 12:20, Adrian Bool <[email protected]> wrote:
> I have two models, the first is a 'change request ticket' and the
> second reflects the status of that ticket.  They are linked by a
> ForeignKeyField on the first table.
>
> You may find it best to collapse them into a single object; using the
> 'choice' option to allow the user to set the ticket's state.

Unfortunately this isn't an option for this value, these need to be as
flexible as possible, hard-coding into the model isn't an option. :(

> The two problems that I have are:
>
> 1) Some of the 'states' should mark the ticket as closed, so when the
> ticket is saved with the state of 'closed', it should be marked as
> 'completed' with the date and time the action was carried out.
>
> You can jump in and change the model's data before the model is saved by
> over-riding the 'save' method in your model.
> Below is one of my models.  My frequency attribute could be quite similar to
> what you need for your state field.  Below you can see where I'm setting a
> number of my attributes (based upon the parent object when the Charge object
> is created as an Inline) before this Charge is saved,
> class Charge (models.Model):
>     CHARGE_FREQUENCY_CHOICES = (
>         (u'NRC', u'Non-Recurring'),
>         (u'MRC', u'Monthly'),
>         (u'QRC', u'Quaterly'),
>         (u'ARC', u'Annual')
>         )
>
>     frequency = models.CharField(max_length=15,
> choices=CHARGE_FREQUENCY_CHOICES)
>    <cut more attributes>
>
>     def save(self, *args, **kwargs):
>         self.description = self.service
>         self.customer = self.service.customer
>         self.legacy_charge_id = None
>         self.prorata = False
>         self.created_date = datetime.date.today()
>         self.started_date = self.service.completed_date
>         self.expiry_date = self.service.expiry_date
>         self.terminated_date = self.service.terminated_date
>         super(Charge, self).save(*args, **kwargs)

Hmmm, I'm obviously missing something.  I want to execute something
like the following:

class ChangeStatus(models.Model):
        Description = models.CharField(max_length=128)
        ClosesChangeRequest = models.BooleanField()

        def __unicode__(self):
                return self.Description


class ChangeHeader(models.Model):
        Title = models.CharField(max_length=255)
        Requestor = models.ForeignKey(User)
        Summary = models.TextField()
        AffectedItems = models.ManyToManyField(ConfigurationItem)
        ScmRepo = models.ForeignKey(ScmRepo)
        Created = models.DateField()
        Due = models.DateTimeField()
        Status = models.ForeignKey(ChangeStatus)
        Completed = models.BooleanField()

        def save(self,*args,**kwargs):
                if not self.id:
                        self.created = datetime.date.today()
                if self.Status.ClosesChangeRequest == True:
                        self.completed = True
                super(ChangeHeader, self).save(*args,**kwargs)

but I assume the issue I'm running in to is that the Save handler
doesn't know the current state of the Status field.

Is there a better approach which avoids hard-coding?

Thanks,

M.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to