0
例如,我們有實例模型,我們想要做的事象下面這樣:檢查查詢是否是不存在的或具有特定值
ex = Example.objects.filter(Q(participant__tries=0) | Q(participant__tries=*THERE IS NO EXAMPLE RECORD*), courses=course)
我的意思是在數據庫中沒有記錄,不存在併爲空。還有一個查詢集從不返回任何對象。
我們應該在兩顆星之間取代什麼?
附錄:
我自己的模型
class Questions(models.Model):
course = models.ForeignKey(Course, blank=False, db_index=True)
question_author = models.ForeignKey(Author, blank=False)
number = models.IntegerField(default=0, editable=False, help_text="<b>***Please Don't change it as much as possible!</b>")
question_details = models.CharField(max_length=100, blank=False, default='')
members = models.ManyToManyField(User, through='QuestionsParticipant')
question_type = models.CharField(choices=question_types, max_length=1, default='1')
own_score = models.PositiveIntegerField(default=10)
approved = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
class QuestionsParticipant(models.Model):
question = models.ForeignKey(Questions)
participant = models.ForeignKey(User)
tries = models.PositiveIntegerField(blank=False, default=0)
flag = models.BooleanField(default=False)
flag_reason = models.CharField(max_length=50, blank=True, default='')
success = models.BooleanField(default=False)
timestamp = models.DateTimeField(auto_now_add=True)
在我自己的看法:
question = Questions.objects.filter(Q(questionsparticipant__tries=0) | Q(questionsparticipant__tries__isnull=True),
Q(questionsparticipant__participant_id=request.user.id)
| Q(questionsparticipant__participant_id__isnull=True),
course=courses, approved=True).order_by("number")[0]
我的意思是在數據庫中沒有記錄,沒有和沒有記錄。還有一個查詢集從不返回任何對象。 – altruistic
我相信'isnull'在這種情況下仍然有效。 – Ming
(由於生成的'INNER JOIN'的行爲。) – Ming