过滤项目状态
我正在尝试创建一个“按状态排序”功能,例如显示..状态=“报价”的所有项目。我在弄清楚应该如何解决这个问题时遇到了一些困难。
这是我的 models.py (简化的)
class Project(models.Model):
client = models.ForeignKey(Clients, related_name='projects')
created_by = models.ForeignKey(User, related_name='created_by')
#general information
API_id = models.IntegerField(max_length=10, verbose_name='aC ProjectID', null=True, blank=True)
proj_name = models.CharField(max_length=255, verbose_name='Project Name')
...
notes = models.TextField(verbose_name='Notes', null=True, blank=True)
class Status(models.Model):
project = models.ForeignKey(Project, related_name='status')
value = models.CharField(max_length=20, choices=STATUS_CHOICES, verbose_name='Status')
date_created= models.DateTimeField(auto_now=True)
我在views.py 中执行单独的视图没有问题,但必须有一种比创建多个视图更有效的方法。
以下是我的观点,仅显示报价状态项目:
@login_required
def quote_projects(request):
project_list = Project.objects.filter(status__value__exact='Q')
return render_to_response('project/index.html',{'project_list': project_list, 'user':user}, context_instance=RequestContext(request))
任何帮助将不胜感激!
谢谢,
史蒂夫
I am trying to create a 'sort by status' function that shows, for example .. all projects with the status = 'quote'. I am having a bit of trouble figuring out how I should go about this.
Here is my models.py (simplified)
class Project(models.Model):
client = models.ForeignKey(Clients, related_name='projects')
created_by = models.ForeignKey(User, related_name='created_by')
#general information
API_id = models.IntegerField(max_length=10, verbose_name='aC ProjectID', null=True, blank=True)
proj_name = models.CharField(max_length=255, verbose_name='Project Name')
...
notes = models.TextField(verbose_name='Notes', null=True, blank=True)
class Status(models.Model):
project = models.ForeignKey(Project, related_name='status')
value = models.CharField(max_length=20, choices=STATUS_CHOICES, verbose_name='Status')
date_created= models.DateTimeField(auto_now=True)
I have no problem doing seperate views in the views.py but there has to be a more efficient way then creating multiple views.
Here is my view to show only quote status projects:
@login_required
def quote_projects(request):
project_list = Project.objects.filter(status__value__exact='Q')
return render_to_response('project/index.html',{'project_list': project_list, 'user':user}, context_instance=RequestContext(request))
Any help would be greatly appreciated!
Thanks,
Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这看起来不像排序;而是排序。这看起来像是一个过滤问题。
如果是这样,您必须决定您的过滤键是否旨在成为可添加书签的项目,或者它们是否是动态的并且您应该从某个基础开始。决定是否使用 URL 作为排序键或 CGI 参数。
无论哪种方式,处理程序都是相似的。首先,您可以在 urls.py 中创建:
您的项目可能如下所示:
您根本不需要“用户”;这是由 RequestContext 自动为您提供的,并且由您指定此方法 login_required 的事实来保证。现在您可以参考“http://example.com/projects/quoted”
对于 CGI 参数,您的 urls.py 行如下所示:
您的函数如下所示:
现在您的 URL 将是: http://example.com/project/?status=quoted
This doesn't look like sorting; this looks like a filtering issue.
If so, you have to decide if your filtering keys are intended to be bookmarkable items, or if they're dynamic and you're meant to start from some base. The decides whether or not you're going to use URLs as your sorting keys, or CGI arguments.
Either way, the handler is similar. For the first, you would create in your urls.py:
And your projects could look like:
You don't need the 'user' at all; that's provided for you automagically by the RequestContext, and is assured by the fact that you've specified this method login_required. Now you can refer to "http://example.com/projects/quoted"
For CGI arguments, your urls.py line looks like this:
And your function looks like this:
And now your URL would be: http://example.com/project/?status=quoted