简体   繁体   English

如何在Django模型中过滤对象?

[英]How to filter object in django models?

I have write down A model to store data. 我写下了一个存储数据的模型。 like this. 像这样。

class Country(models.Model):

    Countryname = models.CharField(max_length=200)

    def __unicode__(self):
        return self.Countryname


class CountryDiseases(models.Model):

    country = models.ForeignKey(Country,on_delete=models.CASCADE)
    pmid = models.CharField(default= 'No Data',max_length=254)
    serogroup = models.CharField(default= 'No Data', max_length=254)
    serotype = models.CharField(default= 'No Data', max_length=254)
    biotype = models.CharField(default= 'No Data',max_length=254)
    collection_year = models.CharField(default= 'No Data',max_length=254)
    NoOfStrains = models.CharField(default= 'No Data', max_length=254)


    def __unicode__(self):
        return self.NoOfStrains

A url to render data like this: 呈现如下数据的URL:

url(r'^DATAPLO/(?P<pk>\d)/$', views.County_Details, name='County_Details'),

In short, i have a template that contains country list with a hyperlink, when some one click on a country it should produce a list of all the data associated with the particular country like the given urls, 简而言之,我有一个模板,其中包含带有超链接的国家/地区列表,当某人单击某个国家/地区时,它应会生成与该特定国家/地区相关的所有数据的列表,例如给定的url,

http://127.0.0.1:8000/DATAPLO/india 

where "India" keyword will transferred to the given views and view will extract all the object through filter method: 其中“印度”关键字将转移到给定的视图,视图将通过过滤器方法提取所有对象:

C_Details = CountryDiseases.objects.filter(country__country=pk)

A view to extract and present data like this: 提取和呈现数据的视图如下:

def County_Details(request,pk):
     C_Details = CountryDiseases.objects.filter(country__country=pk)
    #C_Details = CountryDiseases.objects.filter(country='india')
    return render(request, 'DATAPLO/Country_Details.html', {'C_Details': C_Details})

it produces the urld like this: 它产生这样的URL:

http://127.0.0.1:8000/DATAPLO/india 
http://127.0.0.1:8000/DATAPLO/USA 
http://127.0.0.1:8000/DATAPLO/canada 

but data has not been extracted. 但尚未提取数据。

I would probably change the name of the parameter (in the view and in the urls.py) to something like country_name , as country.pk is usually configured to return the same as country.id . 我可能会将参数的名称(在视图和urls.py中)更改为country_name ,因为country.pk通常配置为返回与country.id相同的名称。 And for the sake of convention I would change the name of the name field on the country to name rather than Countryname . 而对于公约的缘故,我将在全国更改名称字段的名称来name ,而不是Countryname

class Country(models.Model):

    name = models.CharField(max_length=200)

    def __unicode__(self):
        return self.name

Then in your view, you can look up the CountryDiseases objects by the name field on the related Country model: 然后,在您的视图中,您可以通过相关国家(地区)模型上的name字段来查找CountryDiseases对象:

def County_Details(request, country_name):
    C_Details = CountryDiseases.objects.filter(country__name=country_name)
    return render(request, 'DATAPLO/Country_Details.html', {'C_Details': C_Details})

Or if you want to look up the country first, as well as the details, because you may have other information stored on the country model, then you can change the CountryDiseases lookup to reference the Country object directly: 或者,如果您想先查找国家/地区以及详细信息,因为您可能在国家/地区模型中存储了其他信息,则可以更改CountryDiseases查找以直接引用Country对象:

def County_Details(request, country_name):
    country = Country.objects.get(name=country_name)
    C_Details = CountryDiseases.objects.filter(country=country)
    return render(request, 'DATAPLO/Country_Details.html', {'C_Details': C_Details})

PS if capitalisation is going to be an issue, such as with 'india' vs 'India', you can use the lookup: PS,如果大写成为问题,例如“印度”与“印度”,则可以使用查找:

  • .filter(country__name__iexact=country_name) (for in the first code example) or .filter(country__name__iexact=country_name) (在第一个代码示例中)
  • .get(name__iexact=country_name) (in the second, but there also you should ensure that you don't have clashes when saving the objects, as you're making a .get() query). .get(name__iexact=country_name) (在第二个中,但是您还应该确保在保存对象时不会发生冲突,因为您正在执行.get()查询)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM