выберите связанные данные для пользовательского фильтра списка - PullRequest
0 голосов
/ 21 марта 2012
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.u_id_id, SecureVirtualElection_answer.e_id_id из SecureVirtualElection_answer, SecureVirtualElection_userrequestpool где SecureVirtualElection_userrequestpool.u_id_id = SecureVirtualElection_answer.u_id_id и SecureVirtualElection_userrequestpool.e_id_id = SecureVirtualElection_answer.e_id_id и rq_type = 'C' AND u_allowed = 0;

Я пытался написать то же самое в админке django. Класс AnswerListFilter (SimpleListFilter): title = _ («утверждено»)

    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 голосов
/ 21 марта 2012

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

...