Re: [Django] #13950: Add "post save" hook to ModelAdmin class

2012-03-25 Thread Django
#13950: Add "post save" hook to ModelAdmin class
---+--
 Reporter:  3point2|Owner:  pandres
 Type:  Uncategorized  |   Status:  closed
Component:  contrib.admin  |  Version:  1.2
 Severity:  Normal |   Resolution:  wontfix
 Keywords: | Triage Stage:  Unreviewed
Has patch:  1  |  Needs documentation:  1
  Needs tests:  1  |  Patch needs improvement:  1
Easy pickings:  0  |UI/UX:  0
---+--
Changes (by anonymous):

 * ui_ux:   => 0
 * type:   => Uncategorized
 * severity:   => Normal
 * easy:   => 0


Comment:

 this was actually added in r16498 (see #16115) in the form of a similar
 hook called save_related (for anyone coming across this old ticket)

 also, using save_formset wasn't ideal as it gets called once for every
 inline.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

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



Re: [Django] #13950: Add "post save" hook to ModelAdmin class

2010-11-21 Thread Django
#13950: Add "post save" hook to ModelAdmin class
---+
  Reporter:  3point2   | Owner:  pandres
Status:  closed| Milestone: 
 Component:  django.contrib.admin  |   Version:  1.2
Resolution:  wontfix   |  Keywords: 
 Stage:  Unreviewed| Has_patch:  1  
Needs_docs:  1 |   Needs_tests:  1  
Needs_better_patch:  1 |  
---+
Changes (by kmtracey):

  * status:  reopened => closed
  * resolution:  => wontfix

Comment:

 Replying to [comment:3 3point2]:
 > * The inline invoice entries are now saved. At this point, it's no
 longer possible to hook into the save process to calculate the invoice's
 total. The only workaround would be to have each invoice entry update its
 parent invoice's total when it is saved, which will cause a lot of
 unnecessary save() calls on the invoice (one for each entry).

 No, you can override the !ModelAdmin's save_formset (see:
 
http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset)
 and add whatever code you like after all the inline models have been
 saved.

 >
 > My post_save hook allows the user to call a function *after* the inlines
 have been saved. This allows the total to be calculated using
 invoice.invoiceentry_set.all()

 So does the existing capability of overriding save_formset. I don't see
 why another way is needed.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

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



Re: [Django] #13950: Add "post save" hook to ModelAdmin class

2010-11-16 Thread Django
#13950: Add "post save" hook to ModelAdmin class
---+
  Reporter:  3point2   | Owner:  pandres
Status:  reopened  | Milestone: 
 Component:  django.contrib.admin  |   Version:  1.2
Resolution:|  Keywords: 
 Stage:  Unreviewed| Has_patch:  1  
Needs_docs:  1 |   Needs_tests:  1  
Needs_better_patch:  1 |  
---+
Changes (by 3point2):

  * status:  closed => reopened
  * resolution:  wontfix =>

Comment:

 I'll try explain the problem better.

 Let's start with the example I described above: the Invoice and
 InvoiceEntry models. I have registered the Invoice model with the admin
 and also registered the InvoiceEntry model as an inline (as you suggest).

 Let's say a user creates a new invoice, and adds several invoice entries
 using the inline forms. When the user clicks on "Save", there is no way to
 calculate the total of the invoice. This is why:

 * First, the invoice is saved. This calls the save_model method on the
 modeladmin, which calls the save() method on the Invoice model. At this
 point, none of the invoice entries have been saved yet, so it's not
 possible to calculate a total in save_model() or save()
 * The post_save signal is sent when save() completes. The invoice's
 invoiceentry_set manager still returns an empty list as the inlines have
 not been saved yet.
 * The inline invoice entries are now saved. At this point, it's no longer
 possible to hook into the save process to calculate the invoice's total.
 The only workaround would be to have each invoice entry update its parent
 invoice's total when it is saved, which will cause a lot of unnecessary
 save() calls on the invoice (one for each entry).

 My post_save hook allows the user to call a function *after* the inlines
 have been saved. This allows the total to be calculated using
 invoice.invoiceentry_set.all()

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

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



Re: [Django] #13950: Add "post save" hook to ModelAdmin class

2010-11-13 Thread Django
#13950: Add "post save" hook to ModelAdmin class
---+
  Reporter:  3point2   | Owner:  pandres
Status:  closed| Milestone: 
 Component:  django.contrib.admin  |   Version:  1.2
Resolution:  wontfix   |  Keywords: 
 Stage:  Unreviewed| Has_patch:  1  
Needs_docs:  1 |   Needs_tests:  1  
Needs_better_patch:  1 |  
---+
Changes (by pandres):

  * status:  new => closed
  * needs_better_patch:  0 => 1
  * resolution:  => wontfix
  * needs_tests:  0 => 1
  * needs_docs:  0 => 1

Comment:

 I'm not sure if I understand what do you mean. How will the model know if
 all of its related objects have been saved?

 There are many ways you can do this, including showing the related objects
 in the same admin view of the main object
 (http://docs.djangoproject.com/en/dev/ref/contrib/admin/#inlinemodeladmin-
 objects).

 In your example you can also compute the total when getting the attribute.

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

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



Re: [Django] #13950: Add "post save" hook to ModelAdmin class

2010-11-13 Thread Django
#13950: Add "post save" hook to ModelAdmin class
---+
  Reporter:  3point2   | Owner:  pandres
Status:  new   | Milestone: 
 Component:  django.contrib.admin  |   Version:  1.2
Resolution:|  Keywords: 
 Stage:  Unreviewed| Has_patch:  1  
Needs_docs:  0 |   Needs_tests:  0  
Needs_better_patch:  0 |  
---+
Changes (by pandres):

  * owner:  nobody => pandres
  * needs_better_patch:  => 0
  * needs_tests:  => 0
  * needs_docs:  => 0

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

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



[Django] #13950: Add "post save" hook to ModelAdmin class

2010-07-15 Thread Django
#13950: Add "post save" hook to ModelAdmin class
--+-
 Reporter:  3point2   |   Owner:  nobody
   Status:  new   |   Milestone:
Component:  django.contrib.admin  | Version:  1.2   
 Keywords:|   Stage:  Unreviewed
Has_patch:  1 |  
--+-
 If a model with a set of related objects is edited in the admin, there is
 currently no way to run any code once the admin site has saved the related
 objects.

 For example, consider an invoice:

 models.py:

 {{{
 class Invoice(models.Model):
 # fields like customer_name, etc
 total = models.DecimalField(max_digits=11, decimal_places=2)

 def update_total(self):
 self.total = sum([x.cost for x in self.invoiceentry_set.all()]

 class InvoiceEntry(models.Model):
 invoice = models.ForeignKey(Invoice)
 item = models.CharField(max_length=100)
 cost = models.DecimalField(max_digits=11, decimal_places=2)
 # etc
 }}}

 When clicking "save" in the admin after editing or adding an invoice in
 the admin interface, there is no way to have the invoice update its total
 using the update_total method. Overriding the save() method on the Invoice
 model doesn't help, because it is called before the related
 !InvoiceEntries are created and saved.

 I supposes the functionality I want could be implemented by overriding the
 save() method for the !InvoiceEntry model and have it update the total of
 the parent instance, although this seems a little inelegant as the Invoice
 instance would then be updated and saved once for every invoice entry
 created or edited.

 I suggest adding a "post_save" option to the !ModelAdmin class, which will
 be called once all related objects have been saved. Please consider the
 attached patch. It allows the following:

 admin.py:

 {{{
 class InvoiceAdmin(admin.ModelAdmin):
 def post_save(self, instance):
 instance.update_total()

 admin.site.register(Invoice, InvoiceAdmin)
 }}}

 With the patch, the post_save method above is called after the invoice and
 all its related entries have been created and saved.

 If this is considered for inclusion into Django, I'd be happy to modify
 the patch to include documentation.

 Vasili

-- 
Ticket URL: 
Django 
The Web framework for perfectionists with deadlines.

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