Comment

Gudbergur Erlendsson

I think it's lacking DRY. This is more beautiful IMO:

#models.py
from mongokit import *
class Computer(DjangoDocument): # notice difference from above
make = UnicodeField(validate=lambda x: x.strip(), index=True)
model = UnicodeField()
purchase_date = DateTimeField(default=datetime.datetime.utcnow)
cpu_ghz = FloatField(validate=lambda x: x>0)

class Meta:
verbose_name_plural = "Computerz"

and then

from models import Computer
computer=Computer()
computer.make = u"Apple"
computer.model = u"G5"
computer.cpu_hrz = 2.66
computer.save()

and the library should default handle the connection internally, but expose then connection objects when required.

Replies

Aakarsh.

Hello Gudberger,
 I have tried to use mongo kit, I understand that the unicode part has to be done in models.py .
Can you please tell me where do I write this piece of code you have mentioned here:

from models import Computer
computer=Computer()
computer.make = u"Apple"
computer.model = u"G5"
computer.cpu_hrz = 2.66
computer.save()

Is it the pyhon shell itself or the models.py?
Thanks in advance