0

I am trying to create two user types in django 1.3. I am subclassing the AUTH_PROFILE_MODULE with the following models.py:

class Member(models.Model):
    ROLE_CHOICES = (
        (0, 'Guide'),
        (1, 'Operator'),
    )
    user = models.ForeignKey(User, unique=True)
    location = models.CharField(max_length=60)
    role = models.IntegerField(choices=ROLE_CHOICES)


class Guide(Member):
    bio = models.TextField(blank=True)
    experience = models.TextField(blank=True)
    image = models.ImageField(blank=True, upload_to='images')
    fileupload = models.FileField(blank=True, upload_to='files')

    def __unicode__(self):
        return self.user.username

    def get_absolute_url(self):
        return '/profiles/guides/%s' % self.user.username


class Operator(Member):
    bio = models.TextField(blank=True)
    image = models.ImageField(blank=True, upload_to='images')

    def __unicode__(self):
        return self.user.username

    def get_absolute_url(self):
        return '/profiles/operators/%s' % self.user.username

I am using generic class based views and can get the ListView to work for the Guide and Operator models I cannot get the DetailView to work. My views.py is as follows:

class GuideDetailView(DetailView):
    model = Guide
    context_object_name = 'guide'
    template_name = 'members/guide_detail.html'


class GuideListView(ListView):
    model = Guide
    context_object_name = 'guides'
    template_name = 'members/guide_list.html'

Any idea what might be missing?

thesteve
  • 2,413
  • 6
  • 26
  • 28

1 Answers1

1

Either provide a queryset:

class GuideDetailView(DetailView):
    queryset = Guide.objects.all()

or override the get Method of DetailView:

class GuideDetailView(DetailView):
    def get(self):
        return "Everything you want, maybe: Guide.object.get(id=1)"

Given this in your urls.py:

url(r'^(?P<my_id>\d)/$', GuideDetailView.as_view(),),

You need to override get, like this:

class GuideDetailView(DetailView):
    def get(self, request, **kwargs):
        # lookup Guide Id in your database and assign it object
        self.object = Guide.objects.get(pk=kwargs.get('my_id'))
        # add object to your context_data, so that you can access via your template
        context = self.get_context_data(object=self.object)
        return self.render_to_response(context)
jazz
  • 2,371
  • 19
  • 23
  • I added the queryset = Guide.objects.all() as you suggested but still get an error that DetailView requires either a slug or pk. Any idea how I can utilize the username being passed in via the url for the slug value. – thesteve May 27 '11 at 23:08
  • You have to add a argument in your urlconf, like: url(r'^$', GuideDetailView.as_view(), {'pk':1}), – jazz May 27 '11 at 23:18
  • I have edited my post, you should be able to solve your problem overriding the get method. – jazz May 28 '11 at 07:58
  • Thanks overriding the get method did it. – thesteve May 28 '11 at 21:58