Hi Pankaj,

I tested the code at it works perfectly. Thanks again!

I have one question out of curiosity (I don't need this for this project). When a ticketitem is deleted, def save_formset is called, but no instance is made, hence an error message "instance referenced before assignment"

What is the correct way to deal with this?

I don't need this for my current project, so if you don't answer it, it is no problem at all.

Regards,

Rob

On 04-02-13 18:50, Pankaj Singh wrote:
Yes, save_formset() has only those instances whose fields were changed.

Sincerely,
Pankaj Singh
http://about.me/psjinx


On Mon, Feb 4, 2013 at 10:46 PM, Rob van Dam <r...@wieskamp.nl> wrote:
Hi Pankaj,

Thank you so much!!!! You have saved my day :-) I will be able to test it
tomorrow, and I will let you know if it worked.

I tested all my previous attempts saving an existing item in Admin. But when
nothing is changed in the admin form, def save_formset is not called......

Regards,

Rob



On 04-02-13 17:26, Pankaj Singh wrote:
Hey Rob,

I tested following code and it works.

#### models.py

from django.db import models

# Create your models here.

class Ticket(models.Model):
      ticketnumber = models.IntegerField()
      total_amount = models.DecimalField(max_digits=7, decimal_places=2,
blank=True)

      def update_total_amount(self):
          total = 0
          for ti in self.ticketitem_set.all():
              total += ti.price * ti.amount
          self.total_amount = total
          self.save()

class TicketItem(models.Model):
      name = models.CharField(max_length=30)
      ticket = models.ForeignKey(Ticket)
      price = models.DecimalField(max_digits=7, decimal_places=2)
      amount = models.IntegerField()


#### admin.py
from django.contrib import admin
from tickets.models import Ticket, TicketItem


class TicketItemInline(admin.TabularInline):
      model = TicketItem

class TicketAdmin(admin.ModelAdmin):
      inlines = [TicketItemInline,]

      def save_formset(self, request, form, formset, change):
          instances = formset.save(commit=False)
          for instance in instances:
              instance.save()
          formset.save_m2m()
          instance.ticket.update_total_amount()


admin.site.register(Ticket, TicketAdmin)


I hope it helps.

Sincerely,
Pankaj Singh
http://about.me/psjinx


On Mon, Feb 4, 2013 at 9:01 PM, Rob van Dam | Camping het Wieskamp
<r...@wieskamp.nl> wrote:
Hi Pankaj,

I have tried many things today, but unfortunately I cannot get anything
working :-( Can you give me a bit more information on this issue? Any
hint
would be highly appreciated!

I cannot get anything saved in the database. For testing I made this
setup
(added ordernumber to the Tickets model):


class TicketAdmin(admin.ModelAdmin):
      def save_formset(self, request, form, formset, change):
          instances = formset.save(commit=False)
          for instance in instances:
              instance.ordernumber = 100
              instance.save()
          formset.save_m2m()

I expected the value 100 to be saved in the database....but nothing
works.

Rob




On 02-02-13 21:04, Pankaj Singh wrote:


http://stackoverflow.com/questions/8294889/override-save-on-django-inlinemodeladmin

Sincerely,
Pankaj Singh
http://about.me/psjinx


On Sun, Feb 3, 2013 at 1:34 AM, Pankaj Singh <ps.j...@gmail.com> wrote:
Hey Rob,

You can override save_formset() method of ModelAdmin for this. Create
a method update_total_amount(self) in your Ticket Model.

class TicketAdmin(admin.ModelAdmin):
       def save_formset(self, request, form, formset, change):
           instances = formset.save(commit=False)
           for instance in instances:
               instance.user = request.user
               instance.save()
           formset.save_m2m()

           ## call update_total_amount()
           instance.update_total_amount()

Useful Links -
1.

https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset
2.

https://docs.djangoproject.com/en/1.4/ref/contrib/admin/#django.contrib.admin.ModelAdmin.save_formset

Sincerely,
Pankaj Singh
http://about.me/psjinx


On Sat, Feb 2, 2013 at 8:29 PM, Rob <r...@wieskamp.nl> wrote:
    I have two models:

class Tickets(models.Model):
      ticketnumber = models.IntegerField()
      total_amount = models.DecimalField()

class TicketItems(models.Model):
      name = models.Charfield(max_length=30)
      ticket = models.ForeignKey(Tickets)
      price = models.DecimalField()
      amount = models.IntergerField()

I have an inline Adminpage were users can add TicketItems. How do I
update
the total_amount of model Tickets from all TicketItems on save (price
*
amount)?

Django 1.4

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


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


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



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


Reply via email to