Issue
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!
Solution
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
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.