jax
jax

Reputation: 4197

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

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:

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.

Upvotes: 2

Views: 14206

Answers (1)

nimasmi
nimasmi

Reputation: 4138

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. And for the sake of convention I would change the name of the name field on the country to name rather than 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:

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:

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:

  • .filter(country__name__iexact=country_name) (for in the first code example) or
  • .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).

Upvotes: 2

Related Questions