Skip to content

Instantly share code, notes, and snippets.

@anddam
Last active May 26, 2017 05:21
Show Gist options
  • Save anddam/8fe269e78751c944526f7db6a8960c86 to your computer and use it in GitHub Desktop.
Save anddam/8fe269e78751c944526f7db6a8960c86 to your computer and use it in GitHub Desktop.
from django.db import models
from django.contrib.auth.models import AbstractUser
from django.conf import settings
class User(AbstractUser):
"""Custom user model for future extension"""
class BalanceType(models.Model):
"""Defines the available types a Balance instance can use"""
name = models.CharField(max_length=settings.CHAR_FIELD_MAX_LENGTH)
def __str__(self):
return "%s" % self.name
class Balance(models.Model):
"""Store generic balance for an user, carry its own type"""
type = models.ForeignKey('BalanceType', on_delete=models.CASCADE)
value = models.IntegerField()
user = models.ForeignKey('User', on_delete=models.CASCADE)
def __str__(self):
return "'%s': %s=%d " % (self.user, self.type, self.value)
from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model
from rest_framework import serializers
class UserSerializer(serializers.HyperlinkedModelSerializer):
"""Serialize users data, return username, email, URL and groups"""
class Meta:
model = get_user_model()
fields = ('url', 'username', 'email', 'groups')
extra_kwargs = {'url': {'view_name': 'authenticator:user-detail'},
'groups': {'view_name': 'authenticator:group-list', 'lookup_field': 'name'} ,
}
class GroupSerializer(serializers.HyperlinkedModelSerializer):
"""Serialize users data, return username, email, URL and groups"""
class Meta:
model = Group
fields = ('url', 'name')
extra_kwargs = {'url': {'view_name': 'authenticator:group-detail'}}
(tas) ada@fse ~/development/tsa-as/src $ fg
[1]+ running: ./manage.py runserver 0.0.0.0:8000 (17193)
Internal Server Error: /api/users/
Traceback (most recent call last):
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 882, in _resolve_lookup
current = current[bit]
TypeError: 'BoundField' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/relations.py", line 375, in to_representation
url = self.get_url(value, self.view_name, request, format)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/relations.py", line 313, in get_url
return self.reverse(view_name, kwargs=kwargs, request=request, format=format)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/reverse.py", line 50, in reverse
url = _reverse(viewname, args, kwargs, request, format, **extra)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/reverse.py", line 63, in _reverse
url = django_reverse(viewname, args=args, kwargs=kwargs, **extra)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/urls/base.py", line 91, in reverse
return force_text(iri_to_uri(resolver._reverse_with_prefix(view, prefix, *args, **kwargs)))
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/urls/resolvers.py", line 497, in _reverse_with_prefix
raise NoReverseMatch(msg)
django.urls.exceptions.NoReverseMatch: Reverse for 'group-list' with keyword arguments '{'pk': 1}' not found. 2 pattern(s) tried: ['api/groups\\.(?P<format>[a-z0-9]+)/?$', 'api/groups/$']
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/core/handlers/exception.py", line 41, in inner
response = get_response(request)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/core/handlers/base.py", line 217, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/core/handlers/base.py", line 215, in _get_response
response = response.render()
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/response.py", line 107, in render
self.content = self.rendered_content
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/response.py", line 72, in rendered_content
ret = renderer.render(self.data, accepted_media_type, context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/renderers.py", line 703, in render
context = self.get_context(data, accepted_media_type, renderer_context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/renderers.py", line 676, in get_context
'post_form': self.get_rendered_html_form(data, view, 'POST', request),
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/renderers.py", line 509, in get_rendered_html_form
return self.render_form_for_serializer(serializer)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/renderers.py", line 519, in render_form_for_serializer
{'style': {'template_pack': 'rest_framework/horizontal'}}
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/renderers.py", line 371, in render
return template_render(template, context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/compat.py", line 340, in template_render
return template.render(context, request=request)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/defaulttags.py", line 216, in render
nodelist.append(node.render_annotated(context))
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/defaulttags.py", line 322, in render
return nodelist.render(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/library.py", line 203, in render
output = self.func(*resolved_args, **resolved_kwargs)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/templatetags/rest_framework.py", line 91, in render_field
return renderer.render_field(field, style)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/renderers.py", line 350, in render_field
return template_render(template, context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/compat.py", line 340, in template_render
return template.render(context, request=request)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/backends/django.py", line 66, in render
return self.template.render(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 207, in render
return self._render(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 199, in _render
return self.nodelist.render(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 990, in render
bit = node.render_annotated(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 957, in render_annotated
return self.render(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 1040, in render
output = self.filter_expression.resolve(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 708, in resolve
obj = self.var.resolve(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 849, in resolve
value = self._resolve_lookup(context)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/django/template/base.py", line 890, in _resolve_lookup
current = getattr(current, bit)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/utils/serializer_helpers.py", line 70, in __getattr__
return getattr(self._field, attr_name)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/relations.py", line 523, in choices
return self.get_choices()
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/relations.py", line 519, in get_choices
return self.child_relation.get_choices(cutoff)
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/relations.py", line 193, in get_choices
for item in queryset
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/relations.py", line 193, in <listcomp>
for item in queryset
File "/home/ada/.virtualenvs/tas/lib/python3.5/site-packages/rest_framework/relations.py", line 390, in to_representation
raise ImproperlyConfigured(msg % self.view_name)
django.core.exceptions.ImproperlyConfigured: Could not resolve URL for hyperlinked relationship using view name "authenticator:group-list". You may have failed to include the related model in your API, or incorrectly configured the `lookup_field` attribute on this field.
[25/May/2017 16:50:56] "GET /api/users/ HTTP/1.1" 500 295085
from django.conf.urls import include, url
from rest_framework import routers
from pprint import pprint
from . import views
app_name = 'authenticator'
router = routers.DefaultRouter()
router.register(r'users', views.UserViewSet)
router.register(r'groups', views.GroupViewSet)
pprint(router.get_urls())
urlpatterns = [
url(r'^$', views.BalanceView.as_view(), name='root'),
url(r'^index$', views.BalanceView.as_view(), name='index'),
url(r'^api/', include(router.urls)),
url(r'^api-auth/', include('rest_framework.urls', namespace='rest_framework')),
]
from django.shortcuts import render
from django.views.generic import ListView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.models import Group
from django.contrib.auth import get_user_model
from rest_framework import viewsets
from .models import Balance
from .serializers import UserSerializer, GroupSerializer
# Front-end views
class BalanceView(LoginRequiredMixin, ListView):
"""Displlay balances for current user, require authentication"""
model = Balance
# API views
class UserViewSet(viewsets.ModelViewSet):
"""Allow users to be viewed or edited"""
User = get_user_model()
queryset = User.objects.all().order_by('-date_joined')
serializer_class = UserSerializer
class GroupViewSet(viewsets.ModelViewSet):
"""Allow groups to be viewed or edited"""
queryset = Group.objects.all()
serializer_class = GroupSerializer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment