my Question is that how can i give a list of objects or items to a query :
for example i have an objects which has 4 user id like this :
list_of_user_id = [1 , 2 ,3 ,6]
and now i want to set the CreatorUserID
equal to list_of_user_id
to check how many of the users in list_of_user_id
exist in the TblAnswerQuestionRequest
table .
this is my Model.py :
class TblAnswerQuestionRequest(models.Model):
text = TextField(verbose_name='محتویات درخواست',max_length=4000, blank=True, null=True)
CreatorUserID = models.ForeignKey(Members, on_delete=models.PROTECT, null=True, blank=True)
knowledge_request = models.ForeignKey(TblQuestionRequest, on_delete=models.PROTECT, null=True, blank=True)
CreateDate = IntegerField('تاریخ ثبت', default=LibAPADateTime.get_persian_date_normalized(), null=True, blank=True)
create_hour = models.TimeField(default=datetime.datetime.now(), null=True, blank=True)
Status = IntegerField('وضعیت', default=1,choices=StatusChoices, null=True, blank=True)
def __str__(self):
return str(self.id)
and what i want is like this :
Question_Users = TblAnswerQuestionRequest.objects.filter(CreatorUserID = list_of_user_id)
CodePudding user response:
You probably want to use __in
- see https://docs.djangoproject.com/en/4.0/ref/models/querysets/#in
Question_Users = TblAnswerQuestionRequest.objects.filter(CreatorUserID_id__in = list_of_user_id)
A small note, if you look at the database table from your model, you'll see that the column for the foreign key for CreatorUserID
is going to actually be CreatorUserID_id
(or possibly creatoruserid_id
, not exactly sure how it treats uppercase letters in column names). So the ID
in the foreign key name is bit obsolete, you probably want to only do CreatorUser
or even better creator_user
, so the column name then is creator_user_id
.