0

I have the following urls:

browse/college
browse/department
browse/subjects

I have Tag model where College, Department and Subject are all subclasses on Tag. All the urls will call a single view.

I want to create a view called browse_specfic_tag(request, model_name)

I was thinking of converting model name to model using get_model and do something like,

TagModel = get_model(model_name..)

but I am going to tie url name with model which might not be a good thing to do if I decided to rename either of them.

Any better way to do this?

Community
  • 1
  • 1
Nikhil
  • 2,168
  • 6
  • 33
  • 51

1 Answers1

1
>>> import this
The Zen of Python, by Tim Peters

Beautiful is better than ugly.
Explicit is better than implicit.
…

The proper way of solving this is passing an extra option to the view. See related documentation entry.

Example:

url('^college/$', 'tag_view', {'model': College})

def tag_view(request, model):
    records = model.objects.filter(…)

Furthermore, actions should not be included in URL's name. URLs should identify resources. Therefore I would skip the browse part of the URL.

Krzysztof Szularz
  • 5,151
  • 24
  • 35