Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class BuildingGroup(models.Model):
- description = models.CharField(max_length=500, null=True, blank=True)
- buildings = models.ManyToManyField(Building, default=None, blank=True)
- class Building(models.Model):
- name = models.CharField(max_length=120, null=True, blank=True)
- year_of_construction = models.IntegerField(null=True, blank=True)
- class DetailBuildingGroupView(StaffRequiredMixin, DetailView):
- model = BuildingGroup
- context_object_name = 'group'
- queryset = BuildingGroup.objects.all()
- def get_object(self):
- id = self.kwargs.get("id")
- return get_object_or_404(BuildingGroup, id=id)
- def get_context_data(self, **kwargs):
- context = super(DetailBuildingGroupView, self).get_context_data(**kwargs)
- bg = BuildingGroup.objects.filter(id=self.kwargs.get('id'))
- arr = []
- for item in bg:
- x = item.buildings.values('name', 'net_leased_area')
- arr.append(x)
- context['buildings'] = bg
- return context
- [<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}]>]
- data = json.loads(serializers.serialize("json", arr))
- result = list(bg.values('name', 'net_leased_area'))
- data = (json.dumps(result))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement