python - How to filter object in django models? -


i have write down model store data. 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 render data this:

url(r'^dataplo/(?p<pk>\d)/$', views.county_details, name='county_details'), 

in short, have template contains country list hyperlink, when 1 click on country should produce list of data associated particular country given urls,

http://127.0.0.1:8000/dataplo/india  

where "india" keyword transferred given views , view extract object through filter method:

c_details = countrydiseases.objects.filter(country__country=pk) 

a view extract , present data 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 urld 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.

i change name of parameter (in view , in urls.py) country_name, country.pk configured return same country.id. , sake of convention change name of name field on country name rather countryname.

class country(models.model):      name = models.charfield(max_length=200)      def __unicode__(self):         return self.name 

then in view, can countrydiseases objects name field on 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 want country first, details, because may have other information stored on country model, can change countrydiseases lookup reference 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 going issue, such 'india' vs 'india', can use lookup:

  • .filter(country__name__iexact=country_name) (for in first code example) or
  • .get(name__iexact=country_name) (in second, there should ensure don't have clashes when saving objects, you're making .get() query).

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -