【发布时间】:2015-02-17 06:23:30
【问题描述】:
我正在关注Tutorial on tasty pie implem.的美味馅饼教程
以下是models.py
#models.py
from tastypie.utils.timezone import now
from django.contrib.auth.models import User
from django.db import models
from django.utils.text import slugify
class Entry(models.Model):
user = models.ForeignKey(User)
pub_date = models.DateTimeField(default=now)
title = models.CharField(max_length=200)
slug = models.SlugField()
body = models.TextField()
def __unicode__(self):
return self.title
def save(self, *args, **kwargs):
# For automatic slug generation.
if not self.slug:
self.slug = slugify(self.title)[:50]
return super(Entry, self).save(*args, **kwargs)
这是应用文件夹 blogapp 中的 api.py
from django.contrib.auth.models import User
from tastypie import fields
from tastypie.authorization import Authorization
from tastypie.resources import ModelResource
from blogapp.models import Entry
from tastypie.authentication import BasicAuthentication
class UserResource(ModelResource):
class Meta:
queryset = User.objects.all()
resource_name = 'user'
excludes = ['email', 'password', 'is_active', 'is_staff', 'is_superuser']
# Add it here.
authentication = BasicAuthentication()
class EntryResource(ModelResource):
user = fields.ForeignKey(UserResource, 'user')
class Meta:
queryset = Entry.objects.all()
resource_name = 'entry'
我成功获得了要求用户名和密码的身份验证浏览器窗口 当我输入这个网址时。
http://x.x.x.x:xxxx/blogapp/api/v1/user/?format=json
身份验证后,它以 json 格式向我显示所有用户的数据
如何限制 json 数据以仅显示特定于经过身份验证的用户的特定信息。例如,只有“用户”是经过身份验证的“条目”
一旦通过身份验证如何断开用户连接。重新启动服务器并清除 cookie 不起作用。一旦通过身份验证,我就无法再次进入密码窗口
【问题讨论】:
标签: python django authentication tastypie