Changing Queryset into String

26 views
Skip to first unread message

Lim Kai Wey

unread,
Jan 24, 2020, 11:22:23 AM1/24/20
to Django users
Greetings everyone,

I would like to ask if anyone knows how to convert my  queryset into string or if there is a better approach to what I am trying to accomplish. In theory, I am trying to assign all the text into a variable and tokenize the whole string so that I can find the top 10 most common word.

Following is the snippet of my code:

Models.py
class Tweet(models.Model):
    tweet_id = models.BigIntegerField()
    text = models.TextField()
    polarity = models.DecimalField(decimal_places=5,max_digits=6,null=True)
    keyword = models.ForeignKey(Keyword, on_delete=models.CASCADE)
    country = models.CharField(max_length=255,null=True)
    stored_at = models.DateTimeField(editableFalse)

    def __str__(self):
        return self.text


Views.py
def tweet_visualizer(requestword = None):
    if(word == None):
        num_comments = Tweet.objects.all().count()
        latest_comments = Tweet.objects.all().order_by('-stored_at')[:6]
        all_tweets = Tweet.objects.all()

        all_comments = all_tweets.__str__()
        tokenized_text = word_tokenize(all_comments)
        fdist = FreqDist(tokenized_text)
        top_10_common = fdist.most_common(10)
    else:
        num_comments = Tweet.objects.filter(keyword__keyword=word).count()
        latest_comments = Tweet.objects.filter(keyword__keyword=word).order_by('-stored_at')[:6]
        all_tweets = Tweet.objects.filter(keyword__keyword=word)

        all_comments= all_tweets.__str__()
        tokenized_text = word_tokenize(all_comments)
        fdist = FreqDist(tokenized_text)
        top_10_common = fdist.most_common(10)


My Current Output:

<QuerySet [<Tweet: Just each machine team recognize.>, <Tweet: Card shoulder option majority with green conference.>, <Tweet: Sometimes feel news chance either.>, <Tweet: Second pull so across drug term.>, <Tweet: Result wife my sport without.>, <Tweet: Modern pick indeed wall north.>, <Tweet: Standard sense each might loss.>, <Tweet: Wish sport small.>, <Tweet: Feel shoulder two go.>, <Tweet: Social state just perhaps.>, <Tweet: Daughter son although development house.>, <Tweet: Before political Democrat during pass.>, <Tweet: Natural development story music and eye water.>, <Tweet: Yourself agency school since what benefit force or.>, <Tweet: Join floor gun do.>, <Tweet: Scientist here very at risk computer.>, <Tweet: Maybe leave seek on big policy.>, <Tweet: Activity close defense should personal.>


My Desired Output:
Just each machine team recognize Card shoulder option majority with green conference Sometimes feel news chance either ... etc

Thank you in advance for anyone who can help me. I really do appreciate it! 

Sincerely,
Kai Wey

mohamed Alisaleh

unread,
Jan 25, 2020, 1:10:02 PM1/25/20
to Django users
aborabieamohame

mohamed Alisaleh

unread,
Jan 25, 2020, 1:10:02 PM1/25/20
to Django users
Github.com

mohamed Alisaleh

unread,
Jan 25, 2020, 1:10:02 PM1/25/20
to Django users
Reply all
Reply to author
Forward
0 new messages