Combine 2 Query sets and display Django

Question:

Im new to django

I am trying to combing two query sets for example, I have different farms. and in those farms they have respective blocks.

I would like to output the farm as a heading and list the blocks of each farm underneath it.

Example:

Farm 1
Block 1
Block 2
Blaock 3

Farm 2
Block 1
Block 2
Block 3

What I currently in have in views:

def irrigation(request):   

 obj3 = Farms.objects.all().values("id", "farm_name")
 obj2 = Blocks.objects.all()

 obj = obj2 | obj3

 context = {"object": obj}      

 return render(request, "irrigation.html", context)

in html:

 {% for farms in object %}
 <tr>
 <td>{{ farms.farm_name }} {{ farms.id }}</td>
 <td><a href="/ifarm/{{ farms.id }}"> Edit </a>                         
 </tr>
 {% endfor %}

In models

class Blocks(models.Model):
 farm_id = models.CharField(max_length=100)    
 block_name = models.CharField(max_length=255, null=True)
 block_size = models.CharField(max_length=255, null=True)
 block_concurrent = models.CharField(max_length=255, null=True)
 block_full_bloom = models.CharField(max_length=255, null=True)
 block_harvest_start = models.CharField(max_length=255, null=True)
 block_harvest_complete_date = models.CharField(max_length=255, null=True)
 block_log1 = models.CharField(max_length=255, null=True)
 block_log2 = models.CharField(max_length=255, null=True)
 block_log3 = models.CharField(max_length=255, null=True)
 block_crop_class = models.CharField(max_length=255, null=True)
 block_crop_type = models.CharField(max_length=255, null=True)
 block_crop_subtype = models.CharField(max_length=255, null=True)
 block_planted_date = models.CharField(max_length=255, null=True)
 block_plant_height = models.CharField(max_length=255, null=True)
 block_root_system = models.CharField(max_length=255, null=True)

class Farms(models.Model):
 farm_name = models.CharField(max_length=100)
 user_id = models.IntegerField(default='1')
 user_groups = models.JSONField(null=True)

Please help!

Asked By: swatch360

||

Answers:

I found a solution using a foreign key.

Updated models:

class Blocks(models.Model):
 #farm_id = models.CharField(max_length=100)  
 farm = models.ForeignKey(Farms, on_delete=models.CASCADE, default=None)   
 block_name = models.CharField(max_length=255, null=True)
 block_size = models.CharField(max_length=255, null=True)
 block_concurrent = models.CharField(max_length=255, null=True)
 block_full_bloom = models.CharField(max_length=255, null=True)
 block_harvest_start = models.CharField(max_length=255, null=True)
 block_harvest_complete_date = models.CharField(max_length=255, null=True)
 block_log1 = models.CharField(max_length=255, null=True)
 block_log2 = models.CharField(max_length=255, null=True)
 block_log3 = models.CharField(max_length=255, null=True)
 block_crop_class = models.CharField(max_length=255, null=True)
 block_crop_type = models.CharField(max_length=255, null=True)
 block_crop_subtype = models.CharField(max_length=255, null=True)
 block_planted_date = models.CharField(max_length=255, null=True)
 block_plant_height = models.CharField(max_length=255, null=True)
 block_root_system = models.CharField(max_length=255, null=True)

Notice the line:

farm = models.ForeignKey(Farms, on_delete=models.CASCADE, default=None) 

Farms stayed the same:

class Farms(models.Model):
 farm_name = models.CharField(max_length=100)
 user_id = models.IntegerField(default='1')
 user_groups = models.JSONField(null=True)  

Then I ran the commands:

python manage.py makemigrations
python manage.py migrate

In views:

def irrigation(request):
 obj = Blocks.objects.all()
 context = {"object": obj}    
 return render(request, "irrigation.htm", context)

To Output in html:

{% for blocks in object %}
{{ blocks.block_name }} 
{% endfor %}
Answered By: swatch360
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.