Matching blank entries in django queryset for optional field with corresponding ones in a required

Posted by gramware on Stack Overflow See other posts from Stack Overflow or by gramware
Published on 2010-04-08T21:48:37Z Indexed on 2010/04/11 10:53 UTC
Read the original article Hit count: 594

Filed under:
|

I have a django queryset in my views whose values I pack before passing to my template. There is a problem when the queryset returns none since associated values are not unpacked. the quersyet is called comments.

Here is my views.py

def forums(request ):


post_list = list(forum.objects.filter(child='0')&forum.objects.filter(deleted='0').order_by('postDate'))
user = UserProfile.objects.get(pk=request.session['_auth_user_id'])
newpostform = PostForm(request.POST)
deletepostform = PostDeleteForm(request.POST)
DelPostFormSet = modelformset_factory(forum, exclude=('child','postSubject','postBody','postPoster','postDate','childParentId'))
readform = ReadForumForm(request.POST)
comments =list( forum.objects.filter(deleted='0').filter(child='1').order_by('childParentId').values('childParentId').annotate(y=Count('childParentId')))


if request.user.is_staff== True :
    staff = 1
else:
    staff = 0
staffis = 1




if newpostform.is_valid():
    topic = request.POST['postSubject']
    poster = request.POST['postPoster']
    newpostform.save()
    return HttpResponseRedirect('/forums')

else:
    newpostform = PostForm(initial = {'postPoster':user.id})

if request.GET:
    form = SearchForm(request.GET)
    if form.is_valid():
        query = form.cleaned_data['query']
        post_list = list((forum.objects.filter(child='0')&forum.objects.filter(deleted='0')&forum.objects.filter(Q(postSubject__icontains=query)|Q(postBody__icontains=query)|Q(postDate__icontains=query)))or(forum.objects.filter(deleted='0')&forum.objects.filter(Q(postSubject__icontains=query)|Q(postBody__icontains=query)|Q(postDate__icontains=query)).values('childParentId')))

if request.method == 'POST':
    delpostformset = DelPostFormSet(request.POST)
    if delpostformset.is_valid():
        delpostformset.save()
        return HttpResponseRedirect('/forums')

else:
    delpostformset = DelPostFormSet(queryset=forum.objects.filter(child='0', deleted='0'))



"""if readform.is_valid():
    user=get_object_or_404(UserProfile.objects.all())
    readform.save()
else:
    readform = ReadForumForm()"""

post= zip( post_list,comments, delpostformset.forms)
paginator = Paginator(post, 10) # Show 10 contacts per page

# Make sure page request is an int. If not, deliver first page.
try:
    page = int(request.GET.get('page', '1'))
except ValueError:
    page = 1

# If page request (9999) is out of range, deliver last page of results.
try:
    post = paginator.page(page)
except (EmptyPage, InvalidPage):
    post = paginator.page(paginator.num_pages)

return render_to_response('forum.html', {'post':post, 'newpostform': newpostform,'delpost':delpostformset, 'username':user.username, 'comments':comments, 'user':user,  },context_instance = RequestContext( request ))

I realised that the issue was with the comments queryset comments =list( forum.objects.filter(deleted='0').filter(child='1').order_by('childParentId').values('childParentId').annotate(y=Count('childParentId'))) which will only returns values for posts that have comments. so i now need a way to return 0 comments when a value in post-list post_list = list(forum.objects.filter(child='0')&forum.objects.filter(deleted='0').order_by('postDate')) does not have any comments (optional field).

Here is my models.py

class forum(models.Model):
    postID = models.AutoField(primary_key=True)
    postSubject = models.CharField(max_length=100)
    postBody = models.TextField()
    postPoster = models.ForeignKey(UserProfile)
    postDate =  models.DateTimeField(auto_now_add=True)
    child = models.BooleanField()
    childParentId = models.ForeignKey('self',blank=True, null=True)
    deleted = models.BooleanField()

    def __unicode__(self):
        return u' %d' % ( self.postID)

© Stack Overflow or respective owner

Related posts about django

Related posts about python