Filter a queryset must be limited to one result using slicing

I’m attempting to query items based on if they are in a group or not.

I currently have a user profile model which has a manytomany field with usergroup. I wan to query the profile to see if the user is a part of a group, and then do a query to get the items that are associated with the group.

this is my current view

@login_required
def group_item_list(request, id=None):
    user = request.user.profile.id
    group = Profile.objects.get(user=user)
    test = group.group.all()
    items = WorkorderItem.objects.filter(assigned_group_id = test).exclude(completed=1).order_by("-workorder")
   
    context = {
        'items':items,
    }
    return render(request, "dashboard/partials/design_item_list.html", context)

I get the error: The QuerySet value for an exact lookup must be limited to one result using slicing.

So, if I slice the QuerySet and do the following, it works

@login_required
def group_item_list(request, id=None):
    user = request.user.profile.id
    group = Profile.objects.get(user=user)
    test = group.group.all()[0]
    items = WorkorderItem.objects.filter(assigned_group_id = test).exclude(completed=1).order_by("-workorder")
   
    context = {
        'items':items,
    }
    return render(request, "dashboard/partials/design_item_list.html", context)

How do I loop through all of the items in my list to get all of the results for the different groups?

It’ll help a lot if you posted the models involved here: Profile, UserGroup?, and WorkorderItem. It’s a lot easier to answer questions like this if we’re able to use the field names you’re familiar with.

Here are the models

class Profile(models.Model):
    user = models.OneToOneField(User, null=True, on_delete=models.CASCADE)
    name = models.CharField(max_length=200, null=True)
    phone = models.CharField(max_length=200, blank=True, null=True)
    email = models.CharField(max_length=200, blank=True, null=True)
    date_created = models.DateTimeField(auto_now_add=True, null=True)
    group = models.ManyToManyField(UserGroup)

    def __str__(self):
        return self.user.username
class UserGroup(models.Model):
    name = models.CharField('Name', max_length=100, null = True)

    def __str__(self):
        return self.name
class WorkorderItem(models.Model):
    workorder = models.ForeignKey(Workorder, blank=False, null=True, on_delete=models.SET_NULL)
    workorder_hr = models.CharField('Workorder Human Readable', max_length=100, blank=False, null=False)
    item_category = models.ForeignKey(Category, blank=True, null=True, on_delete=models.SET_NULL)
    item_subcategory = models.ForeignKey(SubCategory, blank=True, null=True, on_delete=models.SET_NULL)
    setprice_category = models.ForeignKey(SetPriceItem, blank=True, null=True, on_delete=models.SET_NULL)
    setprice_item = models.ForeignKey(SetPriceItemPrice, blank=True, null=True, on_delete=models.SET_NULL) 
    #item_subcategory = models.CharField('Subcategory', max_length=100, blank=True, null=True)
    pricesheet_modified = models.BooleanField('Pricesheet Modified', blank=True, null=True, default=False)
    design_type = models.ForeignKey(DesignType, blank=True, null=True, on_delete=models.SET_NULL)
    postage_type = models.ForeignKey(PostageType, blank=True, null=True, on_delete=models.SET_NULL)
    description = models.CharField('Description', max_length=100, blank=False, null=False)
    item_order = models.PositiveSmallIntegerField('Display Order', blank=True, null=True)
    quantity = models.DecimalField('Quantity', max_digits=6, decimal_places=2, blank=True, null=True)
    unit_price = models.DecimalField('Unit Price', max_digits=10, decimal_places=4, blank=True, null=True)
    total_price = models.DecimalField('Total Price', max_digits=8, decimal_places=2, blank=True, null=True)
    override_price = models.DecimalField('Override Price', max_digits=8, decimal_places=2, blank=True, null=True)
    absolute_price = models.DecimalField('Absolute Price', max_digits=8, decimal_places=2, blank=True, null=True)
    last_item_order = models.CharField('Original Item Order', max_length=100, blank=True, null=True)
    last_item_price = models.CharField('Original Item Price', max_length=100, blank=True, null=True)
    billable = models.BooleanField(default=True)
    notes = models.TextField('Notes:', blank=True, null=False)
    show_notes = models.BooleanField(default=False)
    internal_company = models.CharField('Internal Company', choices=[('LK Design', 'LK Design'), ('Krueger Printing', 'Krueger Printing')], max_length=100, blank=False, null=False)
    tax_exempt = models.BooleanField(default=False)
    tax_amount = models.DecimalField('Tax Amount', max_digits=8, decimal_places=2, blank=True, null=True)
    total_with_tax = models.DecimalField('Total', max_digits=8, decimal_places=2, blank=True, null=True)
    parent_item = models.PositiveSmallIntegerField('Parent Item', blank=True, null=True)
    added_to_parent = models.BooleanField(blank=False, null=False, default=False)
    parent = models.BooleanField(blank=False, null=False, default=False)
    child = models.BooleanField(blank=False, null=False, default=False)
    job_status = models.ForeignKey(JobStatus, blank=True, null=True, on_delete=models.SET_NULL)
    completed = models.BooleanField(blank=False, null=False, default=False)
    assigned_user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)
    test_user = models.ForeignKey(Profile, blank=True, null=True, on_delete=models.SET_NULL)
    assigned_group = models.ForeignKey(UserGroup, blank=True, null=True, on_delete=models.SET_NULL)
    created = models.DateTimeField(auto_now_add=True, blank=False, null=False)
    updated = models.DateTimeField(auto_now = True, blank=False, null=False)

    def __str__(self):
        return self.workorder.workorder

Rephrasing to confirm understanding:

You want to find all WorkorderItem related to any UserGroup related to a specific user.

Is that correct?

If so, then you should be able to do something like:
items = WorkorderItem.objects.filter(assigned_group__profile__user=request.user)

(I suggest you try this in the Django shell first.)

You can apply an order_by function to this if you want them sorted, and you may need to add a distinct function as well.

That worked exactly how I was intending. Could you please explain the __profile__user or direct me to the documentation on that?

Thank you

It’s documented at Lookups that span relationships