选择自定义列表过滤器的相关数据

时间:2012-03-21 05:53:12

标签: django django-models django-admin

class Answer(models.Model):
    u_id=models.ForeignKey(User)                                               
    e_id=models.ForeignKey(Event)                                               
    c_desc=models.TextField(verbose_name="Candidate Description")               
    c_vote_count=models.IntegerField(default=0,verbose_name="Candidate vote count")       
    c_logo=models.ImageField(upload_to="candidateimages",null=True,blank=True)      
    class Meta:
        verbose_name="Candidate"
        verbose_name_plural = "Candidates"
        unique_together=("e_id","u_id")

class UserRequestPool(models.Model):
    u_id=models.ForeignKey(User)
    e_id=models.ForeignKey(Event)
    u_allowed=models.BooleanField(default=False,verbose_name="User allowed")
    rq_date=models.DateField(auto_now_add=True,verbose_name="Request Date")
    request_list = (('V', 'Voter'), ('C','Candidate' ))
    rq_type=models.CharField(max_length=1,choices=request_list,verbose_name="Request Type")

        class Meta:
        unique_together=("e_id","u_id","rq_date","rq_type")

我希望通过以下sql查询提供数据

从SecureVirtualElection_answer,SecureVirtualElection_userrequestpool中选择SecureVirtualElection_answer.u_id_id,SecureVirtualElection_answer.e_id_id 其中SecureVirtualElection_userrequestpool.u_id_id = SecureVirtualElection_answer.u_id_id 和SecureVirtualElection_userrequestpool.e_id_id = SecureVirtualElection_answer.e_id_id 和rq_type ='C'和u_allowed = 0;

我一直试图在django admin中写同样的东西     class AnswerListFilter(SimpleListFilter):         title = _('approved')

    parameter_name = 'select'

    def lookups(self, request, model_admin):
        return (
            ('yes', _('yes')),
            ('no', _('no')),
        )

    def queryset(self, request, q):
       print "hello"
       if self.value() == 'yes':
            print "yes"
            return Answer.objects.raw('select SecureVirtualElection_answer.u_id_id,SecureVirtualElection_answer.e_id_id from SecureVirtualElection_answer,SecureVirtualElection_userrequestpool  where SecureVirtualElection_userrequestpool.u_id_id=SecureVirtualElection_answer.u_id_id  and SecureVirtualElection_userrequestpool.e_id_id=SecureVirtualElection_answer.e_id_id and rq_type=\'C\' AND u_allowed=1')

        if self.value() == 'no':
            return Answer.objects.raw('select SecureVirtualElection_answer.u_id_id,SecureVirtualElection_answer.e_id_id from SecureVirtualElection_answer,SecureVirtualElection_userrequestpool  where SecureVirtualElection_userrequestpool.u_id_id=SecureVirtualElection_answer.u_id_id  and SecureVirtualElection_userrequestpool.e_id_id=SecureVirtualElection_answer.e_id_id and rq_type=\'C\' AND u_allowed=0')
        print "control should not reach here"

1 个答案:

答案 0 :(得分:0)

result = UserRequestPool.objects.filter(rq_type='C',u_allowed=False)