过滤项目状态

发布于 2024-10-03 10:19:30 字数 1168 浏览 0 评论 0原文

我正在尝试创建一个“按状态排序”功能,例如显示..状态=“报价”的所有项目。我在弄清楚应该如何解决这个问题时遇到了一些困难。

这是我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆悲凉 2024-10-10 10:19:30

这看起来不像排序;而是排序。这看起来像是一个过滤问题。

如果是这样,您必须决定您的过滤键是否旨在成为可添加书签的项目,或者它们是否是动态的并且您应该从某个基础开始。决定是否使用 URL 作为排序键或 CGI 参数。

无论哪种方式,处理程序都是相似的。首先,您可以在 urls.py 中创建:

url('/projects/(?P<status>\d+)/', quote_projects),

您的项目可能如下所示:

@login_required
def quote_projects(request, status):
    status = dict(STATUS_CHOICES).get(status, None)
    if not status:
        raise Http404

    return render_to_response(
        'project/index.html',
        {'projects': Projects.objects.filter(status__value__exact = status)},
        context_instance=RequestContext(request))

您根本不需要“用户”;这是由 RequestContext 自动为您提供的,并且由您指定此方法 login_required 的事实来保证。现在您可以参考“http://example.com/projects/quoted”

对于 CGI 参数,您的 urls.py 行如下所示:

url('/projects/', quote_projects),

您的函数如下所示:

@login_required
def quote_projects(request):
    status = request.GET.get('status', None)
    status = dict(STATUS_CHOICES).get(status, None)
    if not status:
        raise Http404

    return render_to_response(
        'project/index.html',
        {'projects': Projects.objects.filter(status__value__exact = status)},
        context_instance=RequestContext(request))

现在您的 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:

url('/projects/(?P<status>\d+)/', quote_projects),

And your projects could look like:

@login_required
def quote_projects(request, status):
    status = dict(STATUS_CHOICES).get(status, None)
    if not status:
        raise Http404

    return render_to_response(
        'project/index.html',
        {'projects': Projects.objects.filter(status__value__exact = status)},
        context_instance=RequestContext(request))

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:

url('/projects/', quote_projects),

And your function looks like this:

@login_required
def quote_projects(request):
    status = request.GET.get('status', None)
    status = dict(STATUS_CHOICES).get(status, None)
    if not status:
        raise Http404

    return render_to_response(
        'project/index.html',
        {'projects': Projects.objects.filter(status__value__exact = status)},
        context_instance=RequestContext(request))

And now your URL would be: http://example.com/project/?status=quoted

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文