django 在创建另一个模型时创建多个模型示例

gajydyqb  于 2023-01-10  发布在  Go
关注(0)|答案(1)|浏览(185)

我正在努力实现以下目标:
用户在表单中搜索关键字。搜索查询保存在一个名为TweetSearch的模型中。当提交搜索时,将运行一个函数get_tweets(),该函数使用用户提交的关键字来抓取tweet。这些tweet必须保存在一个名为TweetInstance的模型中。然后,TweetInstance中的一些数据将显示在 Jmeter 板中。
我不知道如何在一个视图中创建多个模型的示例,所以我在searchform和dashboard之间添加了一个视图,该视图运行get_tweets()函数并将它们添加到数据库中,但效果不佳,必须有更好的方法来实现。
另一种方法是将该函数添加到def dashboard()中,但是每次post请求完成时都会执行该函数,这使得网站非常慢。
有人能帮帮我吗?
我尝试了以下方法:
models.py:

class TweetSearch(models.Model):
from datetime import datetime, timedelta
search_term = models.CharField(max_length=200, blank=True)
QUERY_CHOICES = (
    ('t', 'in tweet'),
    ('h', 'in hashtag'),
    ('u', 'username'),
)
id = models.UUIDField(primary_key=True, default=uuid.uuid4)
query_type = models.CharField(max_length=1, choices=QUERY_CHOICES, blank=True, default='t')
start_default = datetime.now() - timedelta(days=30)
start_date = models.DateTimeField(default=start_default)
end_date = models.DateTimeField(default=datetime.now)
language = models.CharField(max_length=200, blank=True, null=True)
country = models.CharField(max_length=200, blank=True, null=True)
city = models.CharField(max_length=200, blank=True, null=True)
searcher = models.ForeignKey(User, on_delete=models.SET_NULL, null=True, blank=True)
created = models.DateTimeField(auto_now_add=True, null=True)

def __str__(self):
    return f"Tweets with the word {self.search_term} from {self.start_date} till {self.end_date} written in " \
           f"{self.language} in {self.country}."

class TweetInstance(models.Model):
tweet_search = models.ForeignKey('TweetSearch', on_delete=models.RESTRICT, null=True)
content = models.CharField(max_length=280, blank=True, null=True)
date = models.DateTimeField(blank=True, null=True)
url = models.CharField(max_length=500, blank=True, null=True)
tweet_id = models.CharField(max_length=200, blank=True, null=True)
username = models.CharField(max_length=200, blank=True, null=True)
followers_count = models.IntegerField(blank=True, null=True)
reply_count = models.IntegerField(blank=True, null=True)
retweet_count = models.IntegerField(blank=True, null=True)
like_count = models.IntegerField(blank=True, null=True)
quote_count = models.IntegerField(blank=True, null=True)
language = models.CharField(max_length=200, blank=True, null=True)
outlinks = models.CharField(max_length=500, blank=True, null=True)
media = models.CharField(max_length=500, blank=True, null=True)
retweeted_tweet = models.CharField(max_length=280, blank=True, null=True)
quoted_tweet = models.CharField(max_length=280, blank=True, null=True)
in_reply_to_tweet_id = models.CharField(max_length=200, blank=True, null=True)
in_reply_to_user = models.CharField(max_length=200, blank=True, null=True)
mentioned_users = models.CharField(max_length=200, blank=True, null=True)
long = models.FloatField( null=True, blank=True)
lat = models.FloatField(null=True, blank=True)
country = models.CharField(max_length=200, blank=True, null=True)
city = models.CharField(max_length=200, blank=True, null=True)
hashtags = models.CharField(max_length=200, blank=True, null=True)
chashtags = models.CharField(max_length=200, blank=True, null=True)

views.py:

class TweetCreate(CreateView):
model = TweetSearch
form_class = TweetForm
success_url = reverse_lazy('create_dashboard')

def create_dashboard(request):
qs = TweetSearch.objects.all()
search_data = [
    {
        'search_term': x.search_term,
        'created': x.created
    } for x in qs
]
df = pd.DataFrame(search_data)
df = df.iloc[-1]
tweets_df = get_tweets(df['search_term'])
mylist = []
tweet_search = TweetSearch.objects.latest('created')

for i in range(0,len(tweets_df['content'])):
    mylist.append(TweetInstance(
        tweet_search=tweet_search,
        date=tweets_df['date'].iloc[i],
        content=tweets_df['content'].iloc[i],
        url=tweets_df['url'].iloc[i],
        username=tweets_df['username'].iloc[i],
        tweet_id=tweets_df['id'].iloc[i],
        followers_count=tweets_df['followers_count'].iloc[i],
        reply_count=tweets_df['reply_count'].iloc[i],
        retweet_count=tweets_df['retweet_count'].iloc[i],
        like_count=tweets_df['like_count'].iloc[i],
        quote_count=tweets_df['quote_count'].iloc[i],
        language=tweets_df['language'].iloc[i],
        outlinks=tweets_df['outlinks'].iloc[i],
        media=tweets_df['media'].iloc[i],
        retweeted_tweet=tweets_df['retweeted_tweet'].iloc[i],
        quoted_tweet=tweets_df['quoted_tweet'].iloc[i],
        in_reply_to_tweet_id=tweets_df['in_reply_to_tweet_id'].iloc[i],
        in_reply_to_user=tweets_df['in_reply_to_user'].iloc[i],
        mentioned_users=tweets_df['mentioned_users'].iloc[i],
        long=tweets_df['long'].iloc[i],
        lat=tweets_df['lat'].iloc[i],
        country=tweets_df['country'].iloc[i],
        city=tweets_df['city'].iloc[i],
        hashtags=tweets_df['hashtags'].iloc[i],
        chashtags=tweets_df['chashtags'].iloc[i])
    )
TweetInstance.objects.bulk_create(mylist)
return render(request, 'twitter_sentiment/loading.html')

def dashboard(request):
# Do things
p8h8hvxi

p8h8hvxi1#

我认为正确的方法是在TweetSearch中添加一个model方法,该方法可以抓取tweet并根据它找到的内容创建TweetInstance的模型。

def save_tweets(self):
    tweets = Twitter API call to get matching tweets # refer current instance with self keyword.

    # Then loop through the tweets and create the corresponding models one by one
    for tweet in tweets:
        TweetInstance.objects.create(..., tweet_search=self) # Updated

然后在视图中,示例化TwitterSearch并调用save_tweets()方法。
你的结构有点奇怪,对我来说可读性不是很强。CreateView是为了一个简单的特定目的而存在的,如果我是你,我宁愿扩展通用的View并手动处理它。
这可能是一个更好(更清晰)的实现的想法。我没有移植你所有的业务逻辑,因为我不想有反向工程,你必须自己做。

from views.generic import View

class TweetCreate(View):
    model = TweetSearch
    form_class = TweetForm
    success_url = reverse_lazy('create_dashboard')
    template_name = 'twitter_sentiment/loading.html'

def get(self, request):
    form = self.form_class()
    context = { 'form': form }
    return render(request, self.template_name, context)

def post(self, request):
    form = self.form_class(request.POST)
    if form.is_valid():
        tweet_search = form.save()
        tweet_search.save_tweets()
    else:
        print(form.errors)
    return redirect(self.success_url)

相关问题