Thanks Thomas you helped me a lot these are my first step in python and django. And I really starting to like it
Torsten On 26 Aug., 12:23, Thomas Orozco <g.orozco.tho...@gmail.com> wrote: > While we're at it, here are a few suggestions. > So long as you can, you should use unicode inside python code. The main > logic would be that input should be converted to unicode as soon as > possible, and that output should be encoded (likely to UTF-8) as late as > possible. > > Consequently, you might want to rewrite your invoice methods so that they do > the following. > invoice_number = unicode(self.created_at.year) + u'/' + unicode(self.id) > You could also write this the following way: > return u'{0}/{1}'.format( self.created_at.year, self.id ), which would be > more elegant (if you use format with a unicode string it calls your object's > __unicode__ method, and if you use format with a string, it calls the > __str__ method.). > > By the way, your __unicode__ method actually returns your invoice's > invoice_number, so you might want to have __unicode__ return > self.invoice_number(). > You could even use a single method for both in your code, but that might not > be a very good idea: if one day you want to change your display, if you have > __unicode__ return self.invoice_number() you can change it. > > Regarding your test code, you will need to add a few details I didn't point > out first. > None of your fields have "blank = True", so you're going to have to either > give them default values or give them actual values in your tests. > > Hope this helps. > > Thomas > > 2011/8/26 Torsten <torstenzan...@googlemail.com> > > > > > > > > > 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.