Probably my very poor python coding rather than Django but I have the
model below set up to update another table (reverse foreign key
relationship I guess you would call it)

class PriceOverride(models.Model):
  """ Product Price Override """
  date_override = models.DateField()
  product_id = models.ManyToManyField(Product,
related_name="product_override", blank=True, null=True)
  customer_id = models.ForeignKey(Customer,
related_name="customer_override", blank=True, null=True)
  override_percentage = models.DecimalField(max_digits=10,
decimal_places=2, editable=True, default='0.00', help_text="This is
the percentage override amount expressed as 0.xx", blank=True,
null=True)

  def __unicode__(self):
    return '%s' % self.customer_id

  def save(self):
    super(PriceOverride, self).save()
    ppo = self.positive_percentage_override.filter(override = self.id)
    if self.override_percentage:
      #give the positive percentage override relationship a variable
      #if there is no match in related positive_percentage_table then
      #add a new record for each product in the list
      if not ppo:
        for product in self.product_id.all():
          p = PositivePercentageOverride(
             override_id=self.id,
             customer = self.customer_id,
             product=product,
             value=self.override_percentage,
             )
          p.save()
      else:
      #if there are existing products in the table
        for product in self.product_id.all():
          product_match = self.positive_percentage_override.filter
(product = product)
          for item in product_match:
            p = PositivePercentageOverride(
                value=self.override_percentage,
            )
            p.save(force_update=True)

And the related model:

class PositivePercentageOverride(models.Model):
  override = models.ForeignKey(PriceOverride,
related_name="positive_percentage_override")
  customer = models.ForeignKey(Customer,
related_name="positive_percentage_customer")
  product = models.ForeignKey(Product,
related_name="positive_percentage_product")
  value = models.DecimalField(max_digits=10, decimal_places=2)

So when a new 'Price Override' record is saved I'm checking for an
override_percentage value and seeing if an associated record in the
PositivePercentageOverride is there.  If it is then I want the save()
method to update that record.  If not, save a set of new records for
each product in the ManyToMany list.

Saving a new set of records is fine but I cannot get the records in
the related table to update at all.

I get "Cannot force an update in save() with no primary key."

Really not sure where to go from here, hope I've explained myself
OK!!  Help please!

Thanks
--~--~---------~--~----~------------~-------~--~----~
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