我是Django的新手,我必须创建new页面,它需要以下输入:位置、技术和服务。第一个网页应该选择一个位置类型,并在此基础上进入下一个页面,根据位置选择该技术。第三个web页面基于前面选择的技术和位置选择服务(这将是两个.For的交集),我必须创建模式,其中每个位置都可以有多个技术和服务,其中两者都是相互独立的,每种技术都有多个服务。
eg: loc1_tech->tech1,tech2
loc2_service->service1,service2,service3
tech1_service->service1,service2
tech_service2->service3,service4,service5如果选择loc1,则下一个网页显示tech1和tech2,如果选择tech2,则第三个web页面只显示service3,这是loc1和tech2服务的交集。我计划创建3种模式-- loc_tech、loc_services、tech_services in models.py。这是解决这个问题和创建模式的有效方法吗?另外,如何为第三个网页找到web服务的交叉点?
发布于 2017-07-16 04:40:40
使用外键的组合是models.py和related_name属性在views.py中可能会帮助您解决问题。
models.py应该类似于:
from django.utils.translation import ugettext_lazy as _
class Location(models.Model):
name = models.CharField(_('Location Name') , max_length = 32)
#other fileds you may want to put.
class Technology(models.Model):
name = models.CharField(_('Technology Name') , max_length = 32)
location = models.ForeignKey(Location , related_name="technologies")
#other fileds you may want to put.
class Service(models.Model):
name = models.CharField(_('Service Name') , max_length = 32)
location = models.ForeignKey(Technology , related_name="services")
#other fileds you may want to put.现在有意见:
def get_technologies(request , location_pk=None):
location = Location.objects.get(pk = location_pk)
technologies = location.technologies.all()
#technologies queryset gives you the all the technologies in the location chosen.
#return a HttpResponse()
def get_services(request , technology_pk=None):
technology = Technology.objects.get(pk = technology_pk)
services = technology.services.all()
#services queryset gives you the all the services in the technology(which comes fromt he location chosen earlier) chosen.
#return a HttpResponse()注意:、technology_pk和location_pk将由客户端作为参数发送。他们必须被你的urls.py控告。
当然,您必须配置urls.py以接受参数technology_pk和location_pk。另外,你也可以用,
location_pk = request.GET.get('location_pk' , None)
location = Location.objects.get(pk = location_pk)客户端在request.GET.get('location_pk' , None)中发送‘request.GET.get('location_pk' , None)’的位置。
编辑:
对于发送到服务器的多个位置或技术实例,方法可能有所不同。正如您所说的,我们必须使用filter()而不是get()。
为了获得技术,
def get_technologies(request):
locations = request.POST.getlist('location[]')
#Above, you have to obtain the list of locations that are selected by the user. Request method can be GET as well.
locations_selected_queryset = Location.objects.filter(pk__in = locations)
#For the above statement to work, the list: "locations" should contain the "pk" values of all the locations selected by the user.
result_technologies = list()
for location in location_selected_queryset:
technologies_for_one_location = location.technologies.all()
result_technologies.append(technologies_for_one_location)
#Change the variable names best suited to you. I tried to name them to give more understanding.现在,result_technologies将有不同查询集()列表的列表,该列表由用户选择的所有位置所带来的查询集组成。如果你把它打印在控制台上,它看起来就像
[<技术:"str值“>,.,技术:"str值”>,技术:"str值“>,……]
查询集列表。现在,您将遇到加入查询集获取的问题,因为查询集列表会造成混乱。
from itertools import chain
#In your views.py, inside the function get_technologies()
def get_technologies(request):
#...
#...
for location in location_queryset:
technologies_for_one_location = location.technologies.all()
result_technologies.append(technologies_for_one_location)
final_result = list(chain(*result_technologies))
#final_result will be a single list with all the querysets you want.希望这能有所帮助。谢谢。
https://stackoverflow.com/questions/45124701
复制相似问题