I really like the idea of how to create those urls.  Once I have
extracted the name and type from the url, do I retrieve my Pet object
like the following:

current_pet = Pet.objects.get(name__iexact=name, type__iexact=type)

If my table has 1000s of pets, isn't the above a bit slow when
compared to the retrieving a pet by id?  How is this kind of situation
handle in some of the Django web application out there?

On May 11, 11:00 pm, Mike Ramirez <gufym...@gmail.com> wrote:
> On Monday 11 May 2009 07:35:37 pm Alex Gaynor wrote:
>
>
>
> > On Mon, May 11, 2009 at 10:22 PM, Thierry <lamthie...@gmail.com> wrote:
> > > Let's say I have a pet table:
>
> > > TABLE: Pet
> > > id     name        type
> > > ------------------------------
> > > 1      Pluto        dog
> > > 2      Foo          cat
> > > 3      Goo          fish
>
> > > I can access the pet page of each of the above by the following url:
>
> > > Pluto: localhost/pets/1/
> > > Foo:   localhost/pets/2/
> > > Goo:  localhost/pets/3/
>
> > > I want the url for the pet page to look like the following:
>
> > > Pluto: localhost/pets/pluto-dog/
> > > Foo:   localhost/pets/foo-cat/
> > > Goo:  localhost/pets/goo-fish/
>
> > > In a PHP web application that I have been working on before, there is
> > > an additional field in the Pet table which holds the url for each
> > > pet.  Is that how it should be done in Django too?
>
> > Since the urls just contain 2 fields that are both on the model I'd just
> > hook up the view at
>
> > r'^(?P<type>\w+)-(?P<name>\w+)/$'
>
> After adding this to my patterns in urls.py
>
> url(r'^(?P<type>\w+)-(?P<name>\w+)/$', 'myproj.myview.myfun',
> name="pet_detail"),
>
> Then I would use the permalink decorator  by adding get_absolute_url() to your
> model for the pets.  
>
> http://docs.djangoproject.com/en/dev/ref/models/instances/#get-absolu...http://docs.djangoproject.com/en/dev/ref/models/instances/#the-permal...
>
> @models.permalink
> def get_absolute_url(self):
>         return ('pet_detail', (), {'type': self.type, 'name':self.name})
>
> Then when using the model, I can do something like:
>
> pet = Pets.object.get(pk=1)
>
> pet.get_absolute_url()
>
> or in a template {{ pet.get_absolute_url }}  
>
> To retrieve the url for the corresponding row.
>
> use the string method lower() to make the type and name lower case.  i.e.
> self.type.lower(), self.name.lower() in get_absolute_url.
>
> > and then in the view you can filter by type and name individually.
> > Alex
>
> Mike
>
> --
> "I say we take off; nuke the site from orbit.  It's the only way to be sure."
> - Corporal Hicks, in "Aliens"
>
>  signature.asc
> < 1KViewDownload
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to