Created
August 7, 2017 04:57
-
-
Save dj-shin/662bd88abeb0a635bd2f2f50ee8502e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Resource(models.Model): | |
title = models.CharField(max_length=256) | |
url = models.URLField() | |
tags = models.ManyToManyField(Tag) | |
def __str__(self): | |
return f'{self.title}' | |
def __iter__(self): | |
yield 'id', self.id | |
yield 'title', self.title | |
yield 'url', self.url | |
yield 'tags', [tag.name for tag in self.tags.all()] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http.response import JsonResponse | |
from network.models import Resource, Connection, ResourceSerializer | |
def network(request): | |
serializer = ResourceSerializer() | |
nodes = Resource.objects.all().prefetch_related('tags') | |
links = Connection.objects.all() | |
return JsonResponse({ | |
'nodes': [dict(node) for node in nodes], | |
'links': serializer.serialize(links), | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment