Sometime, set null = true would allow a field to be empty On Fri, Aug 26, 2011 at 5:41 PM, Thomas Orozco <[email protected]>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" <[email protected]> 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 [email protected]. > > To unsubscribe from this group, send email to > [email protected]. > > 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 [email protected]. > To unsubscribe from this group, send email to > [email protected]. > 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 [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

