Thanks Daniel. Yeah it's just a way of getting the county so I'll try your second option. Could you possibly point me to the docs that explain the double underscore?
On Mar 7, 7:39 pm, Daniel Roseman <roseman.dan...@googlemail.com> wrote: > On Mar 7, 5:42 pm, Jamie Pittock <smallb...@gmail.com> wrote: > > > > > > > Hi, > > > I have an app with countries and counties. The simplified model is: > > > class Country(models.Model): > > name = models.CharField(max_length=50) > > slug = models.SlugField() > > > def __unicode__(self): > > return self.name > > > class County(models.Model): > > name = models.CharField(max_length=50) > > slug = models.SlugField() > > country = models.ForeignKey(Country) > > > def __unicode__(self): > > return self.name > > > The url for a county would be of the format domain.com/country/ > > country/. > > > On the corresponding view for that url I'm currently doing this... > > > def view_county(request, country_slug, county_slug): > > country = get_object_or_404(Country, slug=country_slug) > > county = get_object_or_404(County, slug=county_slug, > > country=country.id) > > > 1. First checking to see if a country exists with that slug > > 2. Then checking to see if a county exists with that slug and also > > with the country_id of the country found in step #1 > > > Can that be merged into one step, or is there a better way completely > > of doing it? > > It depends - do you actually need the 'country' object in the rest of > your view, or are you simply using it as a way to get the county? If > the latter, you could just do: > county = get_object_or_404(Country, slug=county_slug, > country__slug=country_slug) > Note the double underscore in the country__slug argument. > > -- > DR. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---