Query on 2 non related models

16 views
Skip to first unread message

Ivan Martić

unread,
May 20, 2019, 8:12:56 AM5/20/19
to Django users
Hi guys, hope you can help me. 
I have 2 models and i want to make a count query on them. I need a count on how many articles i have in every category.

Models:
class Vendors(models.Model):
    id = models.AutoField(db_column='id', primary_key=True, blank=False, unique=True)
    name = models.CharField(db_column='name', max_length=150, blank=True, null=True)
    groupid = models.IntegerField(db_column='groupid', blank=True, null=True)


class VendorGroup(models.Model):
    id = models.AutoField(db_column='ID', primary_key=True, null=False, max_length=255, blank=True)
    vendor_group = models.CharField(db_column='Vendor Group', max_length=255, blank=True, null=True)

view:
def vendorsgrouping_view(request):
list_vendor = VendorGroup.objects.all().order_by('vendor_group')

context = {
'lista_vendora':lista_vendora, 
}
return render(request, "templates/vendorgroup.html", context)

I have list of vendors listed in template:
    {% for item in lista_vendora %}
        {{ item.vendor_group }}
    {% endfor %}

Prefetch, annotate, select related dont work since they are not connected. I know it is easier to give foreign key to fields but what about this?

Thanks,

Rafael E. Ferrero

unread,
May 20, 2019, 10:05:27 AM5/20/19
to django...@googlegroups.com
Hello Ivan... you say Unrelated but I see you have a relation with VendorGroup... why don't use a ForeignKey?... any way you can use the pipe charater " | " to concatenate two querysets.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f0413341-91b2-4308-afd5-ae486b09a71e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages