You must realize that models are what they claim to be... models. They are not items of their own, they just define how you would model a particular item in your database.
The problem in your test scenario is that the actual invoice items that you are creating are attached to no actual invoice :
InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100)
InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120)
InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120)
You would actually need to do it the other way around.
First, create the invoice,
my_test_invoice = Invoice.objects.create(created_at="2011-07-28", customer_id=1, payable_at='2012-12-12')
Then, create the items.
InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100, invoice = my_test_invoice)
InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120, invoice = my_test_invoice)
InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120, invoice = my_test_invoice)
I actually don't use the create syntax, so you might need to call my_test_invoice's save method before creating the invoice items (In a foreign key relationship, you will always need to save the foreign key item before the item that has a foreign key to it, else it has no id and you get an integrity error).
Thomas
2011/8/26 Torsten <torstenzander@googlemail.com>
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
> Le 26 août 2011 11:35, "Torsten" <torstenzan...@googlemail.com> a écrit :
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).
>
>
>
>
>
>
>
> > 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+unsubscribe@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+unsubscribe@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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
No comments:
Post a Comment