Django过滤来自不同模型的数据

时间:2021-06-18 20:27:00

I have two models User, Details and Skills. In Skills table can store "n" number of skills for particular user. At the time of filtering it display unique user details but keyskills not displayed. I use one to one field in Skills models but multiple user is not accepted so I use foriegn key for that.For example..

我有两个模型User,Details和Skills。在技​​能表中可以为特定用户存储“n”个技能。在过滤时,它显示唯一的用户详细信息,但不显示键功能。我在Skills模型中使用一对一字段,但不接受多个用户,因此我使用foriegn键。例如..

User Table

id | name
---|-----
1  | abc


Details table

id | user_id | city
---|---------|-----
1  |   1     | NY


Skills Table

id | user_id | skill
---|---------|-----
1  |   1     | C
2  |   1     | c++
3  |   1     | java

I want to filter based on skill or city. If it match any one should display whole details. I add models and views Please share you ideas

我想根据技能或城市进行过滤。如果它匹配任何一个应该显示整个细节。我添加模型和视图请分享您的想法

Models

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


class Details(models.Model):
    user = models.OneToOneField(User)
    city = models.CharField(max_length=100, blank=True, null=True)

class Skills(models.Model):
    user = models.ForeignKey(User)
    skill = models.CharField(max_length=100, blank=True, null=True)

views

search = request.GET['keywords']
searchlist = search.split(",")
list_result= []

for search in searchlist:
    for res in User.objects.filter(Q(Skills__skill__icontains=search) | Q(Details__city__icontains=search)):
        if not res in list_result:
            list_result.append(res)
users = list_result

return render_to_response('Result.html',{'details':users},context_instance=RequestContext(request))

templates

{% for d in details %}
{{ d.name }}
{{ d.Skills.skill }}
{{ d.Details.city }}
{% endfor %}

By giving above code its works everything fine except Skills not get displayed.

通过给出上面的代码,它的工作一切都很好,除了没有显示技能。

1 个解决方案

#1


1  

The Skills model is a ForeignKey field which means you have to access it via its _set property.

技能模型是ForeignKey字段,这意味着您必须通过其_set属性访问它。

In your case it's skills_set. So your correct template syntax would be

在你的情况下,它是skill_set。所以你的模板语法正确

{% for user in details %}
    name: {{ user.name }}<br/>
    {% for skill in user.skills_set.all %}
    skill: {{ skill.skill }}<br/>
    {% endfor %}
   city: {{ user.details.city }}
{% endfor %}

This differs from your OneToOneField for Details model.

这与您的OneToOneField for Details模型不同。

Further more, your QuerySet is faulty. You shouldn't try and Q on models but the actual fields. So your current queryset should be like this:

此外,您的QuerySet有问题。你不应该在模型上尝试Q而是实际的字段。所以你当前的查询集应该是这样的:

User.objects.filter(Q(skills__skill__icontains=search) | Q(details__city__icontains=search))

Apart from that, you should really not label your things as what they really are. You've named users to details which is really confusing as you're traversing the Users object. Also you could switch out your entire view code to this

除此之外,你真的不应该把你的东西标记为真实的东西。您已将用户命名为详细信息,因为您在遍历Users对象时会感到困惑。您也可以将整个视图代码切换为此

users = User.objects.filter(Q(skills__skill__icontains=search) | Q(details__city__icontains=search))

it will have the same effect.

它会产生同样的效果。

#1


1  

The Skills model is a ForeignKey field which means you have to access it via its _set property.

技能模型是ForeignKey字段,这意味着您必须通过其_set属性访问它。

In your case it's skills_set. So your correct template syntax would be

在你的情况下,它是skill_set。所以你的模板语法正确

{% for user in details %}
    name: {{ user.name }}<br/>
    {% for skill in user.skills_set.all %}
    skill: {{ skill.skill }}<br/>
    {% endfor %}
   city: {{ user.details.city }}
{% endfor %}

This differs from your OneToOneField for Details model.

这与您的OneToOneField for Details模型不同。

Further more, your QuerySet is faulty. You shouldn't try and Q on models but the actual fields. So your current queryset should be like this:

此外,您的QuerySet有问题。你不应该在模型上尝试Q而是实际的字段。所以你当前的查询集应该是这样的:

User.objects.filter(Q(skills__skill__icontains=search) | Q(details__city__icontains=search))

Apart from that, you should really not label your things as what they really are. You've named users to details which is really confusing as you're traversing the Users object. Also you could switch out your entire view code to this

除此之外,你真的不应该把你的东西标记为真实的东西。您已将用户命名为详细信息,因为您在遍历Users对象时会感到困惑。您也可以将整个视图代码切换为此

users = User.objects.filter(Q(skills__skill__icontains=search) | Q(details__city__icontains=search))

it will have the same effect.

它会产生同样的效果。