Related records on same page with DetailView

13 views
Skip to first unread message

David Crandell

unread,
Jun 24, 2021, 1:30:49 AM6/24/21
to Django users
Hello, I just don't stop having problems LOL.

All I want to do is display related Subcats on the detail page under the Category detail.

Seems like the exact same thing as the Books/Publisher demo but I just can't get it to work. It keeps saying my field doesn't exist, or it pulls all the records from the entire table for every entry in the Category table. This seems like it should be so simple. I'm really starting to get discouraged here.

This is my VIEW. Below are my models.

class CatDetailView(DetailView):
model = Category
template_name = 'ohnet/category_detail.html'

def get_context_data(self, **kwargs):
context = super(CatDetailView, self).get_context_data(**kwargs)
context['subcats'] = SubCat.objects.filter(category_id=self.get_object())
return context

MODEL

class Category(models.Model):
category_name = models.CharField(max_length=200, default=None)
date_entered = models.DateTimeField(auto_now_add=True)
customer_id = models.SmallIntegerField(default=0)
is_active = models.BooleanField(default=True)
scr_pending = models.BooleanField(default=False)
scr_pending_date = models.DateTimeField(default=None, null=True)
header_id = models.SmallIntegerField(default=0)
last_script_added = models.DateTimeField(default=None)
category_type = models.SmallIntegerField(default=0)
writer = models.SmallIntegerField(models.ForeignKey(Emp, related_name='+', on_delete=models.CASCADE), default=0)
nickname = models.CharField(max_length=50, default=None, null=True)

def __str__(self):
return self.category_name

def get_absolute_path(self):
return reverse('cat-det', args=[self.id])


class SubCat(models.Model):
category = models.ForeignKey(Category, related_name='+', on_delete=models.CASCADE)
subcat = models.CharField(max_length=200, default=None)
scope_users = models.CharField(max_length=200, default=None, null=True)
scope_loc_reg = models.CharField(max_length=200, default=None, null=True)
is_featured = models.BooleanField(default=False)
date_entered = models.DateTimeField(auto_now_add=True)

def __str__(self):
return self.subcat
Reply all
Reply to author
Forward
0 new messages