Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

join two querysets django

# you need to have a foreign key relationship defined in the models.py first 
class OrderModelName(models.Model):
  	order_id = models.IntegerField(primary_key=True,  db_column='order_id')
	item_id = models.ForeignKey(ItemModelName, db_column='item_id', on_delete=models.RESTRICT, null=True, blank=True)
# Then in views.py you can pull in foreign fields in your queryset using 
# foreignkeyfield__fieldonforeigntable Example:
def your_view(request):
	data = OrderModelName.objects.all().values(order_id, item_id__item_name)
    
 
PREVIOUS NEXT
Tagged: #join #querysets #django
ADD COMMENT
Topic
Name
3+7 =