2
我有一個家長,名稱和兒童模型:過濾對象
models.py
class Parent(models.Model):
title = models.CharField(max_length=250)
address = models.CharField(max_length=250)
class Name(models.Model):
title = models.CharField(max_length=250)
class Kid(models.Model):
family = models.ForeignKey(Parent)
name = models.ForeignKey(Name)
age = models.IntegerField()
city = models.CharField(max_length=250)
,我必須顯示僅當父對象兒童對象視圖功能有關家長都在詞典:
views.py
def index(request):
patterns = [
{'name__title': 'samy', 'age__lt': 15, 'city': 'paris'},
{'name__title': 'sally', 'age__gt': 20, 'city': 'london'}
]
filter_q = reduce(operator.or_, map(lambda p: Q(**p), patterns))
qs = Kid.objects.filter(filter_q).values_list('id', 'family_id')
family_ids = set()
child_ids = list()
for child_id, family_id in qs:
family_ids.add(family_id)
child_ids.append(child_id)
incomplete_family_ids = set(Kid.objects.exclude(id__in=child_ids).filter(family_id__in=family_ids).values_list('family_id', flat=True).distinct())
complete_family_ids = family_ids - incomplete_family_ids
parents = Parent.objects.filter(id__in=complete_family_ids)
template = 'index.html'
context = {'parents': parents}
return render(request, template, context)
如果我想更換圖案辭典配車型的對象,如:
class Pattern(models.Model):
title = models.CharField(max_length=250)
class PatternItems(models.Model):
name = models.ForeignKey(Name)
age = models.integer()
city = models.CharField(max_length=250)
pattern = models.ForeignKey(Pattern)
所以不是字典。我想知道是否可以從模式模型中選擇一個模式來顯示父對象,如果它們全部相關的話Kid對象在使用模式對象的模式項中。
整個想法是使過濾器動態並允許用戶自己製作過濾器。
幫助?
我在哪裏指定模式名稱? – DjangoGuy
@DjangoGuy在上面的代碼中,通過將PatternItem.name設置爲現有的Name實例來指定名稱。或者-simpler-你可以改變你的PatternItem.name字段爲CharField並使用'Q(name__title = pt_item.name)'代替。 –