Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Customising the Admin Save Models in DJango 1.1 [RSS Feed]

#1 Jan. 26, 2011 11:48:41

Prof F.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Customising the Admin Save Models in DJango 1.1


Hi all,

I'm relatively new to DJango (I've been using it for about three
months) and so far I'm loving it - makes a really nice change from
PHP... ;)

I've run into an issue which my Google foo skills just can't find an
answer to - how to I edit the Admin models and the way that they are
saved?

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.

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.

2) Each ticket is created by a user, each additional piece of
information is created by a user. Both sections should take this
information from the current session, not a drop down list, otherwise
I cannot guarantee that the reporting engine I am also writing will
return accurate data about who opened/edited/closed each ticket.

I don't particularly want to write my own forms for this, but it's not
something that I can find a way of doing. I'm sure I'm not the first
to do this, however if I do get a solution through this group, I
promise to put up a blog post about it! :)

To answer a few questions I'm sure some of you will have:

Q) Why DJango 1.1, why not upgrade?
A) Because 1.1 is what ships with CentOS and that's what we use at
work. Company policy is not to install from "source".

Q) Where is the code?
A) The code is part of the Edison project and the exact model can be
found athttps://github.com/proffalken/edison/blob/master/changemanagement/models.pyKind regards,

Matt

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 26, 2011 12:20:24

Adrian B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Customising the Admin Save Models in DJango 1.1


Hu,

On 26 Jan 2011, at 07:22, Prof Falken 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.

> 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)

In order that the user does not set (or need to set) the above fields, set the
fields attribute of the Admin object so only the values you want the user to
control are visible. Here's mine,

class ChargeInlineAdmin(admin.TabularInline):
fields = ('frequency','amount', 'currency', 'vat_chargeable')
model = Charge
extra = 0


Regards,

aid

Adrian Bool
a...@logic.org.uk



--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 27, 2011 13:28:49

Matthew M.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Customising the Admin Save Models in DJango 1.1


Hi,

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

On 26 January 2011 12:20, Adrian Bool <a...@logic.org.uk> 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 django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 27, 2011 13:42:53

Adrian B.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Customising the Admin Save Models in DJango 1.1


On 27 Jan 2011, at 13:28, Matthew Macdonald-Wallace wrote:
> 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?

If the user had selected a valid ChangeStatus for the Status attribute on the
change form; then I would expect you to be able to query the Status in the way
you do above (although my situation was slightly different; so just because I
would't expect something doesn't mean that is the way it is ;-)).

If you run with the above code, do you get an Exception?

aid


--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Customising the Admin Save Models in DJango 1.1 [RSS Feed]

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support