Django-如何使用独立列创建模式

时间:2017-07-16 03:31:31

标签: python django

我是Django的新手,我必须创建网页,其中包含以下输入:位置,技术和服务。第一个网页应该选择一个位置类型,并根据它进入下一页,根据位置选择技术。第三个网页基于之前选择的技术和位置选择服务(它将是两者的交集)。为此我必须创建模式,其中每个位置可以具有多种技术和服务,其中两者彼此独立,和每项技术多项服务。

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的服务交集。 我打算在models.py中创建3个schemas-loc_tech,loc_services,tech_services。这是解决它并创建模式的有效方法吗?另外,我如何找到第三个网页的Web服务的交集?

1 个答案:

答案 0 :(得分:0)

使用外键的组合是views.py中的models.py和related_name属性可能会帮助您解决问题。

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()

注意: tech_pk和location_pk将由您的客户端作为参数发送。他们必须受到你的urls.py的影响 当然,您必须配置urls.py以接受参数,tech_pk和location_pk。另外,您也可以使用,

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

其中' location_pk' request.GET.get('location_pk' , None)中的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. 由您的客户端发送。

修改

对于发送到服务器的多个位置或技术实例,方法会有所不同。如你所说,我们必须使用filter()而不是get()。

获取技术,

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.

现在,result_technologies将拥有不同查询集列表列表,由查询集组成,由用户选择的所有位置提供。如果您在控制台中打印它,它看起来像

[[<技术:" str值"> ,技术:" str value"> ,技术:" str value"> ,...],[,技术:" str值"> ,技术:" str value"> ,...],....]

即:查询集列表的列表。 现在,您将遇到加入查询集获取的问题,因为查询集列表的列表会产生混淆。see here how to join querysets 加入可以简单地完成,

CREATE TABLE #TBL_Refunds (CurrencyID int, DateValue DATETIME, Refunds decimal(18,2))

CREATE TABLE #TBL_Fees (CurrencyID int, DateValue DATETIME, Fees decimal(18,2)) 

CREATE TABLE #TBL_Deposits (CurrencyID int, DateValue DATETIME, Deposits decimal(18,2)) 

CREATE TABLE #TBL_Cancellations (CurrencyID int, DateValue DATETIME, Cancellations decimal(18,2))        

希望这会有所帮助。感谢。