def handle(self, *args, **options): print('DB command!') # проверили работу, все ли правильно #выведем все статьи, которые есть на данный момент articles = Article.objects.all() print(articles) # сделаем запрос на конкретную статью art_ = Article.objects.get(article_name='Адаптогены') print(art_) # возвращает все теги, кроме указанного tags = Tag.objects.exclude(tag_name='адаптогены') print(tags) # возвращает указанный тег tag1 = Tag.objects.get(tag_name__icontains='фитотерапия') print(tag1) # Показывает статью, созданную последней по времени last_art = Article.objects.latest('article_data') print(last_art) # Выводит статьи, начиная с пятой art2 = Article.objects.all()[4:] print(art2) # сортируем статьи по алфавиту art_sort = Article.objects.order_by('article_name') print(art_sort) # выводит все названия статей по указанному тегу art2_ = Article.objects.filter(article_tag__tag_name="фитотерапия") print(art2_) # добавляем в базу данных #новый тег Tag.objects.create(tag_name='статьи') tags = Tag.objects.all() print(tags) t = Tag(tag_name='седативные') t.save() tags3 = Tag.objects.all() print(tags3) # новая статья (другие добавляются аналогично) a = Article.objects.create( article_name='Седативные', article_text='Описание, что это такое и список трав со ссылками', article_data=timezone.now()) a.article_tag.add(t) articles = Article.objects.all() print(articles)
def readContent(self): with open(staticfiles_storage.path('content_api.json'), encoding="utf-8") as f: data = json.load(f) for article in data['results']: if not Article.objects.filter(uuid=article['uuid']).exists(): newArticle = Article() newArticle.authorByLine = article['byline'] newArticle.body = article['body'] newArticle.publishDate = article['publish_at'].split('T')[0] newArticle.headline = article['headline'] newArticle.promo = article['promo'] newArticle.disclosure = article['disclosure'] newArticle.uuid = article['uuid'] newArticle.slug = slugify(article['headline']) for img in article['images']: if img['featured']: newArticle.featuredImage = img['url'] for ins in article['instruments']: newArticle.save() if Instrument.objects.filter( instrumentId=ins['instrument_id']).exists(): newArticle.relatedInstruments.add( Instrument.objects.get( instrumentId=ins['instrument_id'])) else: i = Instrument() i.instrumentId = ins['instrument_id'] i.companyName = ins['company_name'] i.symbol = ins['symbol'] i.exchange = ins['exchange'] i.save() newArticle.relatedInstruments.add(i) for tag in article['tags']: newArticle.save() if Tag.objects.filter(uuid=tag['uuid']).exists(): newArticle.tags.add(Tag.objects.get(uuid=tag['uuid'])) else: t = Tag() t.uuid = tag['uuid'] t.tagName = tag['name'] t.tagSlug = tag['slug'] t.tagTypeName = tag['tag_type']['name'] t.tagTypeSlug = tag['tag_type']['slug'] t.save() newArticle.tags.add(t) newArticle.save() for tag in article['tags']: if self.first == '' and tag['slug'] == '10-promise': self.first = Article.objects.get(uuid=article['uuid'])
def handleTag(tagStr, newEssay): tagEssayRelation.objects.filter(essayId = newEssay.id).delete() tag_id = 0 final_tag = Tag() tagList = tagStr.split(',') for item in tagList: essay_tag = item.strip().lower() try: old_tag = Tag.objects.get(tag_name = essay_tag) except ObjectDoesNotExist: new_tag = Tag( tag_name = essay_tag, category = 0, ) new_tag.save() final_tag = new_tag else: final_tag = old_tag tagessay_relation = tagEssayRelation( essayId = newEssay, tagId = final_tag, ) tagessay_relation.save()
def handleTag(tagStr, newEssay): tagEssayRelation.objects.filter(essayId=newEssay.id).delete() tag_id = 0 final_tag = Tag() tagList = tagStr.split(',') for item in tagList: essay_tag = item.strip().lower() try: old_tag = Tag.objects.get(tag_name=essay_tag) except ObjectDoesNotExist: new_tag = Tag( tag_name=essay_tag, category=0, ) new_tag.save() final_tag = new_tag else: final_tag = old_tag tagessay_relation = tagEssayRelation( essayId=newEssay, tagId=final_tag, ) tagessay_relation.save()