Commit 8bf30ec9 authored by gushitong's avatar gushitong

update README.md

parent e4025d59
...@@ -27,12 +27,12 @@ class Question(models.Model): ...@@ -27,12 +27,12 @@ class Question(models.Model):
class QuestionSerializer(serializers.GmModelSerializer): class QuestionSerializer(serializers.GmModelSerializer):
user = serializers.WeakRelField( user = serializers.WeakRelField(validators = [
validators = [serializers.BlacklistValidator] serializers.BlacklistValidator
) ])
content = serializers.TextField( content = serializers.TextField(validators = [
validators = [serializers.FilterWordValidator] serializers.FilterWordValidator
) ])
class Meta: class Meta:
model = Question model = Question
...@@ -40,3 +40,22 @@ class QuestionSerializer(serializers.GmModelSerializer): ...@@ -40,3 +40,22 @@ class QuestionSerializer(serializers.GmModelSerializer):
fields = '__all__' fields = '__all__'
``` ```
```python
>>> from answer.models import Question
>>> from answer.serializers.question import QuestionSerializer
>>> q = Question.objects.first()
>>> q.user
{u'city_id': None, u'user_rights_level': u'1', u'topic_count': 0, u'city_name': None, u'portrait': u'http://pic.gmei.com/user/icon7@2x.png-thumb', u'country_tag_id': None, u'nickname': u'18*******4*', u'id': 2500, u'membership_level': u'0', u'level_icon': u'http://hera.s.gmei.com/2017/06/12/d2068222ba', u'constellation': u'', u'membership_icon': u'', u'vote_count': u'0', u'city_tag_id': None, u'constellation_icon': u'', u'person_id': u'55729f2e5aa111e5a2a200163e00488a', u'province_tag_id': None}
>>> qs = Question.objects.all()[:2]
>>>
>>> QuestionSerializer(qs, many=True).data
[OrderedDict([(u'id', 1), ('user', u"{u'city_id': None, u'user_rights_level': u'1', u'topic_count': u'0', u'city_name': None, u'portrait': u'http://pic.gmei.com/user/icon7@2x.png-thumb', u'country_tag_id': None, u'nickname': u'18*******4*', u'id': 2500, u'membership_level': u'0', u'level_icon': u'http://hera.s.gmei.com/2017/06/12/d2068222ba', u'constellation': u'', u'membership_icon': u'', u'vote_count': u'0', u'city_tag_id': None, u'constellation_icon': u'', u'person_id': u'55729f2e5aa111e5a2a200163e00488a', u'province_tag_id': None}"), ('title', u'auto question title'), ('content', u'auto question XuraDFEiam'), ('cover_url', None), ('is_online', True), ('is_recommend', False), ('like_num', 0), ('create_time', '2017-06-27T17:55:41.404296'), ('update_time', '2017-07-04T16:55:59.389776'), ('is_spam', False), ('problem_id', None), ('city_id', None)]), OrderedDict([(u'id', 2), ('user', u"{u'city_id': u'beijing', u'user_rights_level': u'1', u'topic_count': 0, u'city_name': u'\\u5317\\u4eac', u'portrait': u'http://pic.gmei.com/userextra/2016/09/08/165c678aa2-thumb', u'country_tag_id': 259, u'nickname': u'xxxxxxx', u'id': 1, u'membership_level': u'1', u'level_icon': u'', u'constellation': u'', u'membership_icon': u'', u'vote_count': u'0', u'city_tag_id': 328, u'constellation_icon': u'', u'person_id': u'e03b55b85a9911e5a2a200163e00488a', u'province_tag_id': 264}"), ('title', u'auto question title'), ('content', u'auto question XuraDFEiam'), ('cover_url', None), ('is_online', True), ('is_recommend', False), ('like_num', 0), ('create_time', '2017-06-27T17:55:41.404296'), ('update_time', '2017-06-27T17:55:41.423131'), ('is_spam', False), ('problem_id', None), ('city_id', None)])]
>>> QuestionSerializer(qs, many=True, lazy=True).data
[OrderedDict([(u'id', 1), ('user', 2500L), ('title', u'auto question title'), ('content', u'auto question XuraDFEiam'), ('cover_url', None), ('is_online', True), ('is_recommend', False), ('like_num', 0), ('create_time', '2017-06-27T17:55:41.404296'), ('update_time', '2017-07-04T16:55:59.389776'), ('is_spam', False), ('problem_id', None), ('city_id', None)]), OrderedDict([(u'id', 2), ('user', 1L), ('title', u'auto question title'), ('content', u'auto question XuraDFEiam'), ('cover_url', None), ('is_online', True), ('is_recommend', False), ('like_num', 0), ('create_time', '2017-06-27T17:55:41.404296'), ('update_time', '2017-06-27T17:55:41.423131'), ('is_spam', False), ('problem_id', None), ('city_id', None)])]
>>>
>>> serializer = QuestionSerializer(data={"user": 1000, "title": "title", "content": "content"})
>>> serializer.is_valid()
True
>>> serializer.save()
```
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment