Я использую Django .2.2.4. Представьте себе это.
q2:<QuerySet [<KnowHow: one>, <KnowHow: onetwo>, <KnowHow: onethree>, <KnowHow: onetwothree>]>
q4:<QuerySet [<KnowHow: one>, <KnowHow: onethree>, <KnowHow: four>]>
Я хочу отсортировать q2
, что q4
идет ко дну.
q4 записи могут быть не включены в q2
.
Итак, я собираюсь сделать такой запрос.
<QuerySet [<KnowHow: onetwo>, <KnowHow: onetwothree>, <KnowHow: one>, <KnowHow: onethree>]>
Я пробовал как views.py, но произошла такая ошибка.
psycopg2.errors.SyntaxError: each UNION query must have the same number of columns
LINE 1: ...git_taggeditem"."tag_id") >= 1) UNION ALL (SELECT "portal_kn...
Портал PS - это название моего приложения.
Если у вас есть какие-либо вопросы, пожалуйста, прокомментируйте.
models.py
from django.db import models
from django.urls import reverse
from taggit.managers import TaggableManager
class KnowHow(models.Model):
BASIC_TAGS =(
('1','one'),
('2','two'),
('3','three'),
('4','four'),
('5','five'),
('6','six'),
)
CATEGORY =(
('1','Type2'),
('2','Type1'),
)
author = models.ForeignKey('auth.User',on_delete=models.CASCADE)
category = models.CharField(max_length=1,choices=CATEGORY,default='1')
title = models.CharField(max_length=200)
text = models.TextField(blank=True,default=' ')
# delault=' ':import system will give a error if text column is null
file = models.FileField(blank=True,upload_to='explicit_knowhows')
basic_tag = models.CharField(max_length=1,choices=BASIC_TAGS,default='1')
free_tags = TaggableManager(blank=True)
def __str__(self):
return self.title
def get_absolute_url(self):
return reverse('portal:index')
class Feedback(models.Model):
EFFECT =(
('1','great'),
('2','maybe good'),
('3','bad'),
)
NOVEL =(
('1','I didn't know that'),
('2','I know, but I forgot'),
('3','I know this.'),
)
kh = models.ForeignKey(KnowHow, on_delete=models.PROTECT)
user = models.ForeignKey('auth.User',on_delete=models.CASCADE)
basic_tag = models.IntegerField(default='1')
free_tags = TaggableManager(blank=True)
time = models.DateTimeField(default=timezone.now)
efficacy = models.CharField(max_length=1,choices=EFFECT,default='1')
novelty = models.CharField(max_length=1,choices=NOVEL,default='1')
def __str__(self):
return self.time.strftime("%Y/%m/%d %H:%M:%S"
views.py
def get_queryset(self):
if 'form_value' in self.request.session:
form_value = self.request.session['form_value']
basic_tag = form_value[0]
free_tags = [x.strip() for x in form_value[1].split(',')]
q1=KnowHow.objects.filter(basic_tag=basic_tag).distinct()
q2=q1.filter(free_tags__name__in=free_tags).annotate(num_tags=Count('free_tags')).filter(num_tags__gte=len(free_tags))
print(q2)
q3=q2.exclude(feedback__user=self.request.user.id)
print(q3)
q4=KnowHow.objects.filter(feedback__user=self.request.user.id)
print(q4)
q5=q3.union(q4)
print(q5)
return q5
else:
return KnowHow.objects.none()