0

这里我使用了 4 个模型,一个是基本模型,另外三个是从基本模型派生的派生模型。我想过滤这三个模型之间的数据,其中引用 id 是常见的模型。我在下面添加代码供您参考。

模型.py

class User(models.Model):
    name = models models.CharField(max_length=20)

class JSDetails(models.Model):
    user = models.ForeignKey(User, unique=True)
    visiblity = models.BooleanField(default=True)

class JSPersonal(models.Model):
    user = models.ForeignKey(User, unique=True)
    JS = models.ForeignKey(JSDetails, unique=True)
    country = models.CharField(max_length=100)

class JSSkills(models.Model):
    user = models.ForeignKey(User, unique=True)
    JS = models.ForeignKey(JSDetails, unique=True)
    skill = models.CharField(max_length=100)

视图.py

def Result(request):
    skills = request.GET['skill']
    country = request.GET['country']
    query = Q(skill__icontains=skills )
    query1 = Q(country__icontains=country )
    query2 = Q(visiblity=True)

这里这两个关键字(skills="python", country="USA") 都匹配或匹配以上四种模型。它显示名称、技能和国家/地区等结果。请帮帮我。

4

1 回答 1

1
def Result(request):
    skills = request.GET['skill']
    country = request.GET['country']
    query_skills = Q(jsskills__skill__icontains=skills)
    query_country = Q(jspersonal__country__icontains=country)
    query_visibility = Q(jsdetails__visiblity=True)
    users = User.objects.filter(query_skills | query_country, query_visibility)
    return render_tor_response('sometemplate.html', {'users': users})

然后在模板中循环遍历users上下文中的内容。

于 2013-06-01T10:47:34.180 回答