def fill_articles(self): if Article.objects.count() > 0: return fake = Faker() tags_set = Tags.objects.all() print() for i in range(40): post = Article() post.title = fake.sentence(nb_words=6, variable_nb_words=True, ext_word_list=None) post.text = fake.text(max_nb_chars=3000, ext_word_list=None) post.creationDate = fake.date(pattern="%Y-%m-%d", end_datetime=None) post.departureDate = fake.date(pattern="%Y-%m-%d", end_datetime=None) post.returnDate = fake.date(pattern="%Y-%m-%d", end_datetime=None) post.shortDescription = fake.text(max_nb_chars=100, ext_word_list=None) post.image = random.choice(photos) post.id = i post.location = fake.sentence(nb_words=1, variable_nb_words=True, ext_word_list=None) post.save() # заполениение поля ManyToManyField() должно происходить после сохранения записи в базу (после post.save()) # иначе будет instance is on database "None", value is on database "default" - невозможно связать с несуществующей записью for j in range(3): t = random.choice(tags_set) post.tags.add(t) self.stdout.write('add tag [%s] in article [%d]' % (t, i)) self.stdout.write('add article [%d]' % post.id)
def post(request): now=time.localtime() article =Article() if request.POST.has_key('content'): article.author = cgi.escape(request.POST['author'].decode('utf-8')) article.content = request.POST['content'].decode('utf-8') article.tags = request.POST['tags'].decode('utf-8') article.title = cgi.escape(request.POST['title'].decode('utf-8')) now_date = datetime.datetime.now()+timedelta(hours=+8) s = now_date.ctime() article.date = str(now_date)[:-7] article.gmtdate = "%s, %s %s %s %s GMT" % (s[:3] ,s[8:10], s[4:7] ,s[-4:] ,s[11:19]) article.year = str(datetime.datetime.now()+timedelta(hours=+8))[:4] article.month = str(datetime.datetime.now()+timedelta(hours=+8))[5:7] if not article.title: if len(article.content) > 11: article.title = article.content[:12] + '...' else: article.title=article.content article.id=time.strftime('%Y%m%d%H%M%S',now) if article.content: article.put() for tag_name in filter(None,article.tags.split(' ')): tag = Tag() tag.tag = tag_name tag.article_id = article.id tag.put() return HttpResponseRedirect("/blog/")