Advertisement
Guest User

Untitled

a guest
Jun 24th, 2019
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. class BuildingGroup(models.Model):
  2. description = models.CharField(max_length=500, null=True, blank=True)
  3. buildings = models.ManyToManyField(Building, default=None, blank=True)
  4.  
  5.  
  6.  
  7. class Building(models.Model):
  8. name = models.CharField(max_length=120, null=True, blank=True)
  9. year_of_construction = models.IntegerField(null=True, blank=True)
  10.  
  11. class DetailBuildingGroupView(StaffRequiredMixin, DetailView):
  12. model = BuildingGroup
  13. context_object_name = 'group'
  14. queryset = BuildingGroup.objects.all()
  15.  
  16. def get_object(self):
  17. id = self.kwargs.get("id")
  18. return get_object_or_404(BuildingGroup, id=id)
  19.  
  20. def get_context_data(self, **kwargs):
  21. context = super(DetailBuildingGroupView, self).get_context_data(**kwargs)
  22. bg = BuildingGroup.objects.filter(id=self.kwargs.get('id'))
  23.  
  24. arr = []
  25. for item in bg:
  26. x = item.buildings.values('name', 'net_leased_area')
  27. arr.append(x)
  28.  
  29. context['buildings'] = bg
  30.  
  31. return context
  32.  
  33. [<QuerySet [{'name': 'TestBuilding', 'net_leased_area': 1234.0}, {'name': 'Another test building', 'net_leased_area': 2242.0}, {'name': 'Crazy new item', 'net_leased_area': 12.0}]>]
  34.  
  35. data = json.loads(serializers.serialize("json", arr))
  36.  
  37. result = list(bg.values('name', 'net_leased_area'))
  38. data = (json.dumps(result))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement