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
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