⬅︎ Back to How and why to use django-mongokit (aka. Django to MongoDB)
I think it's lacking DRY. This is more beautiful IMO:#models.pyfrom 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 thenfrom models import Computercomputer=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.
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 Computercomputer=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
Comment
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
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