On Tue, Nov 13, 2012 at 8:49 PM, luke lukes <lordluk...@gmail.com> wrote:
> Hi everyone. hi have these models:
>
>       #models.py
>
> class Subject(models.Model):
> name = models.CharField("Name",max_length=50, blank=True)
> ...
> ...
>
> class Activity(models.Model):
> label = models.CharField("Act. name",max_length=150)
> price = models.DecimalField("price", max_digits=10,
> decimal_places=2,default=0)
> count = models.IntegerField("Count", default=0)
>
> def __unicode__(self):
>    return u"%s" % (self.label)
> class Meta:
>    verbose_name_plural = "Activities"
>
>
> class Invoice(models.Model):
> subject = models.ForeignKey(Subject)
> date = models.DateField(default=date.today())
> activities = models.ManyToManyField(Activity)
> ....
> ....
>
>
> while creating a new Invoice instance on admin, i can select the many to
> many fields 'activities', but i'd like to have an additional counter (eg. an
> IntegerField) as an Invoice field to count and save the quantity of each
> activity added to my Invoice instance. Is this possible?
>
> I mean: for each 'Activity' added to an Invoice instance, i need to count
> the amount of that activity.
>
>
> thanks,
>
> LuKe
>

Perhaps I'm missing something, everyone else is rattling on about
aggregating and counting, but this is about invoicing for the right
number of activities, right?

That is a classical invoice scenario. With each item ('Activity')
added to the invoice, you need to record the number of those items
added so that you can calculate the value of that line in the invoice.
This is done by adding additional information to the join table, which
in django is done by adding a 'through' model to model the ManyToMany
link.

https://docs.djangoproject.com/en/1.4/topics/db/models/#intermediary-manytomany

Typically, when dealing with invoices, this model would be called a
LineItem. I would model it like so:

class Invoice(Model):
  activities = ManyToManyField('Invoice', through='LineItem')
  …

class Activity(Model):
  price = DecimalField(decimal_places=2)
  …

class LineItem(Model):
  per_item_price = DecimalField(decimal_places=2)
  num_items = PositiveIntegerField(default=1)
  invoice = ForeignKey('Invoice')
  activity = ForeignKey('Activity')

Copying the item price to the LineItem when you create it allows your
invoice to remain the same total even if you change the price of the
activity (which is a plus in any financial system, once an invoice is
created, clients expect them to remain the same total!).

Cheers

Tom

-- 
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 at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to