我的网站上有一个搜索表单。当用户键入查询(如 iPhone)时,程序会将该关键字存储到名为“SearchTerms”的数据库中,其中包含搜索该关键字的人数。
例子:
search_term: iPhone
search_count: 50
这意味着有 50 人在我的网站上搜索了“iPhone”这个词。我希望在搜索词存在时更新搜索词,如果不存在则添加搜索词。
这是我尝试做的:
模型.py
class SearchTerms(models.Model):
search_terms = models.CharField(max_length=255, blank=True, null=True)
total_searches = models.IntegerField(default=0)
updated_on = models.DateTimeField(auto_now_add=True)
search_objects = SearchTermManager()
class Meta:
verbose_name_plural = "Search Terms"
def __str__(self):
return self.search_terms
Run Code Online (Sandbox Code Playgroud)
视图.py
class SearchView(ListView):
template_name = 'search.html'
paginate_by = 20
count = 0
def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['count'] = self.count or 0
context['query'] = self.request.GET.get('q')
return context
def get_queryset(self):
request = self.request
query = request.GET.get('q', None)
if query is …Run Code Online (Sandbox Code Playgroud) 我正在构建一个任务管理应用程序,其中每个用户都有多个项目(集合),每个项目内部都有任务(集合),每个任务内部都有子任务。
我能够使用此代码检索所有项目:
User user = Provider.of<User>(context);
CollectionReference projects = Firestore.instance.collection('users').document(user.uid).collection("projects");
Run Code Online (Sandbox Code Playgroud)
但我的问题是如何从每个项目中获取所有任务和子任务(如果存在)并将它们列在 StreamBuilder 中?
这是我当前的代码:
Flexible(
child: StreamBuilder<QuerySnapshot>(
stream: projects.snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Text('Something went wrong');
}
if (snapshot.connectionState == ConnectionState.waiting) {
return Text("Loading");
}
return new ListView(
children:
snapshot.data.documents.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document.data['project_name']),
subtitle: new Text(document.data['project_description']),
);
}).toList(),
);
},
),
),
Run Code Online (Sandbox Code Playgroud)
如果项目有任务,我想在项目描述下面显示它们,如果任务有子任务,我想在其父任务下面显示它们。