def test_database_canbe_query(self): note1 = Note() note1.id = 5 note1.name = "for testing" note1.save() n = Note.objects.filter(id=5)[0] self.assertEqual(n.name, "for testing")
# 这行代码必须在初始化django之后 from notes.models import Note import pymongo myclient = pymongo.MongoClient("mongodb://192.168.0.104:27017/") mydb = myclient["xiaohongshu"] mycol = mydb["notes2"] myquery = {} mydoc = mycol.find(myquery) for x in mydoc: if x['NoteView']['noteInfo']['type'] == 'normal': note = Note() note.id = x['NoteView']['noteInfo']['id'] note.title = x['NoteView']['noteInfo']['title'] note.type = x['NoteView']['noteInfo']['type'] note.desc = x['NoteView']['noteInfo']['desc'] note.likes = x['NoteView']['noteInfo']['likes'] note.cover = x['NoteView']['noteInfo']['cover']['original'].replace('http://ci.xiaohongshu.com/','') note.collects = x['NoteView']['noteInfo']['collects'] imageStr = '' for image in x['NoteView']['noteInfo']['images']: imageStr += (image['original'].replace('http://ci.xiaohongshu.com/','') + ',') note.images = imageStr note.user_id = x['NoteView']['noteInfo']['user']['id'] # 检查msyql数据库是否含有改笔记 checkresults = Note.objects.filter(id=note.id) if len(checkresults) == 0: note.save()