Django RESTful 使用tastypie

安装方法参照:http://readthedocs.org/docs/django-tastypie/en/latest/tutorial.html#installation

转载请标注:power by gmszone.

依赖条件:

  • Python 2.4+
  • Django 1.0+ (tested on Django 1.1+)
  • mimeparse 0.1.3+ (http://code.google.com/p/mimeparse/)
    • Older versions will work, but their behavior on JSON/JSONP is a touch wonky.
  • dateutil (http://labix.org/python-dateutil)
  • OPTIONAL - lxml (http://lxml.de/) if using the XML serializer
  • OPTIONAL - pyyaml (http://pyyaml.org/) if using the YAML serializer
  • OPTIONAL - uuid (present in 2.5+, downloadable from http://pypi.python.org/pypi/uuid/) if using the ApiKey authentication

Windows下除了lxml以外其他都可以用easy_install或者pip方式安装(如果要采用xml格式的话,就需要安装lxml)

即:

easy_install tastypie
或者
pip install tastypie

lxml在windows的安装比较烦琐。需要用到cygwin以及gcc,会要求安装libxml2以及libxslt。

因此可以直接采用下载exe的方法:

http://pypi.python.org/simple/lxml/


models.py

from tastypie.utils.timezone import now
from django.contrib.auth.models import User
from django.db import models
from django.template.defaultfilters 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)
其他的可以参见http://readthedocs.org/docs/django-tastypie/en/latest/tutorial.html#installation的文档。

最后运行结果如图所示:


Django RESTful 使用tastypie_第1张图片


你可能感兴趣的:(django,import,yaml,behavior,slug)