Generic view object_detail on a m2m relationship

13 views
Skip to first unread message

Delacroy Systems

unread,
Jan 4, 2010, 1:29:55 AM1/4/10
to Django users
I am trying to use the generic view, object_detail on a m2m
relationship. I have multiple businesses that offer multiple services.
I would like to show the services offered by each business. At the
moment, I see all the services - not just the services that a
particular business is offering.

In models.py:
class Business(models.Model):
business = models.CharField(max_length=100)

class Service(models.Model):
service = models.CharField(max_length=100)
providers = models.ManyToManyField(Business, through =
"BusinessService")

class BusinessService(models.Model):
business = models.ForeignKey(Business)
service = models.ForeignKey(Service)

In urls.py:
def get_service():
return Service.objects.all()

businessservice_list = {
#'queryset' : BusinessService.objects.all(),
'queryset' : Business.objects.all(),
'extra_context': {'service_list': get_service}

...skip some detail...
(r'^showservice/(?P<object_id>\d+)/$', list_detail.object_detail,
businessservice_list),

In business_detail.html:
{% block content %}
<h2>Business Services</h2>
{% if object %}
<h3>{{ object.business }}</h3>
{{ service_list }}
{% endif %}
{% endblock content%}

Delacroy Systems

unread,
Jan 4, 2010, 12:55:49 PM1/4/10
to Django users
I see that using object_detail is the 'incorrect' generic view to show
details. I am exploring other options, including writing a custom view
to give me what I want.

Delacroy Systems

unread,
Jan 4, 2010, 1:55:39 PM1/4/10
to Django users
This is how I did it in views.py:

def businessshowservice_view(request, business_id):
business = Business.objects.get(pk=business_id)
services = Business.objects.get(pk=business_id).service_set.all()
return object_list(request, queryset=services,
extra_context={'business' : business}
)

(now just have to figure out a way to get my stress back!)

邓超

unread,
Jan 4, 2010, 7:47:56 PM1/4/10
to django...@googlegroups.com
Try to make a loop in the template, I guess that may can work

2010/1/5 Delacroy Systems <weba...@delacroy.co.za>
--

You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.





--
Deng Chao
Reply all
Reply to author
Forward
0 new messages