Thanks Thomas your are right concerning the sum there is no need for that. But you say: Your invoice items have no invoice attribute, as the error says.
Isn't this line which defines the attribute ? invoice = models.ForeignKey(Invoice) Thanks Torsten On 26 Aug., 11:41, Thomas Orozco <g.orozco.tho...@gmail.com> wrote: > Your invoice items have no invoice attribute, as the error says. > > By the way, although I'm not sure of what you're trying to achieve, let me > point out that there is not really a need for a sum field in your invoice as > you can just sum all the invoice items' amount (and respect the DRY > principle - you use a few other similar issues). > Le 26 août 2011 11:35, "Torsten" <torstenzan...@googlemail.com> a écrit : > > > > > > > > > Hi > > > I have the following model: > > > class Invoice(models.Model): > > customer = models.ForeignKey(Customer) > > project = models.ForeignKey(Project, blank=True, null=True) > > sum = models.FloatField(default=0) > > tax = models.IntegerField(default=16) > > payed_at = models.DateField(blank=True, null=True) > > payable_at = models.DateField('payable at') > > created_at = models.DateTimeField(auto_now_add=True) > > > def invoice_number(self): > > invoice_number = str(self.created_at.year) + '/' + > > str(self.id) > > return invoice_number > > > def __unicode__(self): > > return unicode(str(self.created_at.year) + '/' + str(self.id)) > > > class InvoiceItem(models.Model): > > invoice = models.ForeignKey(Invoice) > > text = models.TextField() > > unit = models.CharField(max_length=255) > > amount = models.IntegerField() > > sum = models.FloatField() > > > and try to run this test > > > def test_save_invoice(self): > > InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100) > > InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120) > > invoice = Invoice.objects.create(created_at="2011-07-28", > > customer_id=1, payable_at='2012-12-12') > > invoice.invoiceitem_set.all() > > self.assertEqual(240, invoice.sum) > > > with this error: > > IntegrityError: invoice_invoiceitem.invoice_id may not be NULL > > > since it is the relation id Django should take care for the > > invoice_invoiceitem.invoice_id > > > Or is there anything wrong with the code ? > > > Torsten > > > -- > > 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. > > > > > > > > -- 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.