Hello Djanglers,

I am using a named many-to-many table ("Asking") to join "Questions"
to "Users".  Asking has some fields I'd like to filter against.

I now use a dictionary called filters:

     filters = {}

     if 'question' in request.GET:
         filters['text__icontains'] = request.GET['question']
     if 'is_visible' in request.GET:
         filters['is_visible'] = request.GET['is_visible']

[...]

     questions = Question.objects.filter(**filters)


I need to add filtering by the publish dates of the question, which
are stored in the many-to-many join table "Asking", which joins users
to questions.

I can't figure out how I would do such a thing.


model Question:

class Question(models.Model):
     text = models.TextField()
     question_type = models.ForeignKey('QuestionType')
     user = models.ManyToManyField(User, through='Asking', null=True)
     submit_date = models.DateTimeField('Date Submitted',
default=datetime.datetime.now)
     responses = models.IntegerField(default=0)


model Asking:

class Asking(models.Model):
     user = models.ForeignKey(User)
     question = models.ForeignKey(Question)
     is_primary_asker = models.BooleanField()
     is_targeted_answerer = models.BooleanField()
     question_targeted_date = models.DateTimeField(null=True,
blank=True)
     question_scheduled_post_date = models.DateTimeField(null=True,
blank=True)
     question_published_date = models.DateTimeField(null=True,
blank=True)



The current filter code:

     filters = {}

     if 'question' in request.GET:
         filters['text__icontains'] = request.GET['question']
     if 'is_visible' in request.GET:
         filters['is_visible'] = request.GET['is_visible']
     if 'featured_status' in request.GET:
         filters['featured_status'] = request.GET['featured_status']
     if 'has_responses' in request.GET:
         filters['responses'] = request.GET['has_responses']
     if 'user_id' in request.GET:
         filters['user'] = request.GET['user_id']

     if 'vertical' in request.GET:
         if not request.GET['vertical'].isdigit():
             # if vertical is passed in by name, get id
             vertical =
Vertical.objects.filter(vertical=request.GET['vertical'])[0]
             filters['verticals'] = vertical.id
         else:
             filters['verticals'] = request.GET['vertical']

     if filters == {}:
         logging.debug("no filters, selecting all.")
         questions = Question.objects.all().order_by(order_by)
[offset:limit]
     else:
         logging.debug("using filters.")
         questions =
Question.objects.filter(**filters).order_by(order_by)[offset:limit]



Thanks for any suggestions!

-Jim

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to