Django如何创建具有独立列的模式

2024-10-04 01:34:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Django的新手,我必须创建包含以下输入的网页:位置、技术和服务。第一个网页应该选择一个位置类型,并在此基础上转到下一个页面,该页面根据位置选择技术。第三个web页面根据前面选择的技术和位置选择服务(这将是两者的交叉点),为此,我必须创建模式,其中每个位置可以有多个技术和服务,这两个技术和服务相互独立,每个技术可以有多个服务。你知道吗

eg: loc1_tech->tech1,tech2
    loc2_service->service1,service2,service3
    tech1_service->service1,service2
    tech_service2->service3,service4,service5

如果我们选择loc1,那么下一个网页显示tech1和tech2,如果我选择tech2,那么第三个网页只显示service3,这是loc1和tech2服务的交集。 我计划创建3个模式-loc\u tech,loc\u services,tech\u services in型号.py. 这是创建模式的有效方法吗?另外,如何找到第三个网页的web服务的交集?你知道吗


Tags: djangoweb网页类型service模式页面loc
1条回答
网友
1楼 · 发布于 2024-10-04 01:34:40

使用外键组合型号.py以及中的相关\u name属性视图.py也许能帮你解决问题。你知道吗

你知道吗型号.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()

注意:技术和位置将由客户端作为参数发送。他们必须得到你的同意网址.py.
当然,您必须配置网址.py接受参数、技术和位置。另外,你也可以使用

location_pk = request.GET.get('location_pk' , None)
location = Location.objects.get(pk = location_pk)

其中,request.GET.get('location_pk' , None)中的'location\u pk'由客户端发送。你知道吗

编辑:

对于发送到服务器的位置或技术的多个实例,方法会有所不同。正如您所说,我们必须使用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\u technologies将拥有不同查询集列表,由用户选择的所有位置所带来的查询集组成。如果你把它打印在控制台上,它看起来像

[[<;技术:“str-value”>;,技术:“str-value”>;,技术:“str-value”>;,…],[,技术:“str-value”>;,技术:“str-value”>;,…],…]

即:查询集列表。 现在您将有一个问题,加入querysets获取,因为querysets列表将产生混乱。see here how to join querysets 连接可以简单地做为

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.

希望这有帮助。谢谢。你知道吗

相关问题 更多 >