1

I've been working on Django-rest-framework for past few months. I did not quite understood how the json request can be used.Can you please help me, I got stuck here for months.

I have a third-party html snippet on another website and lets assume it sends this json data

[{"idcomment":1,"isFlagged":false,"isDeleted":false,"isApproved":true,"createdAt":"2015-11-22T12:39:33Z","numReports":0,"isEdited":false,"message":"xcvvzvc","isSpam":false,"isHighlighted":false,"ip_address":"","is_public":true,"tree_path":"0000000001","tone":"Neutral","forum":1,"parent":null,"topic":1,"last_child":null,"user":1}][{"idcomment":1,"isFlagged":false,"isDeleted":false,"isApproved":true,"createdAt":"2015-11-22T12:39:33Z","numReports":0,"isEdited":false,"message":"xcvvzvc","isSpam":false,"isHighlighted":false,"ip_address":"","is_public":true,"tree_path":"0000000001","tone":"Neutral","forum":1,"parent":null,"topic":1,"last_child":null,"user":1}]

My question: How can I use this json request data and do some verification like if the comment belongs to the correct topic.

I couldn't find any examples where its done. So I figured its possible in Flask by using something like this.

mod.route("/create/", methods=["POST"])
def create():
    json = getJson(request)
    check_required(json, ['date', 'thread', 'message', 'user', 'forum'])
    uid = id_by_email(json['user'])
    fid = id_by_sname(json['forum'])
    if uid < 0 or fid < 0:
        return send_resp(json)
    if 'parent' in json:
        parent = json['parent']
    else:
        parent = None
        json['parent'] = None
    if 'isApproved' in json:
        approved = json['isApproved']
    else:
        approved = 0
        json['isApproved'] = 0
    if 'isHighlighted' in json:
        highlighted = json['isHighlighted']
    else:
        highlighted = 0
        json['isHighlighted'] = 0
    if 'isEdited' in json:
        edited = json['isEdited']
    else:
        edited = 0
        json['isEdited'] = 0
    if 'isSpam' in json:
        spam = json['isSpam']
    else:
        spam = 0
        json['isSpam'] = 0
    if 'isDeleted' in json:
        deleted = json['isDeleted']
    else:
        deleted = 0
        json['isDeleted'] = 0
    db.insert("""INSERT INTO posts (date,thread_id,message,user_id,forum_id,parent,approved,highlighted,edited,spam,deleted) 
                values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)""", (
        json['date'], json['thread'], json['message'], uid, fid, parent, approved, highlighted, edited, spam, deleted))

    pid = db.query("SELECT LAST_INSERT_ID() as id")[0]['id']
    json['id'] = pid
    return send_resp(json)

What is the alternative for something like this in django-rest-framework.

I am quite new, so please explain in simple language. I don't need any code, just searching how can I use json requests I get to the server.

It's a no-brainer I read the documentation several times

Disclaimer: I have also gonna all over through the django-rest-framework code

2
  • django rest framework has a very thorough documentation, which includes a detailed tutorial and working examples that fully cover what you want. Did you read that? Commented Nov 22, 2015 at 13:14
  • @spectras Yeah. Several times Commented Nov 22, 2015 at 15:07

1 Answer 1

1

This job is handled by your Serializer Class :

The first thing we need to get started on our Web API is to provide a way of serializing and deserializing the snippet instances into representations such as json. We can do this by declaring serializers that work very similar to Django's forms.

A serializer class is very similar to a Django Form class, and includes similar validation flags on the various fields, such as required, max_length and default.

If your serializer inherit of serializer.ModelSerializer then it use your model do validate your data like form class that inherit from form.ModelForm. And yes, you don't re-write any code to validate your data, and it seems to be magical. (DRY concept) But, of-course, you can define and override models validators in your serializer class.

I recommend you to re-read Django-Rest-Framework and specially Serializer part. It explain all serializers class that DRF provides with use cases.

Sign up to request clarification or add additional context in comments.

3 Comments

So I just need to do the verification thing in models.py?If it is as easy as it sounds I'm gonna party so hard. Thank you. I'll try
Yes, in many case, that's enough. Thanks to Django-Rest-Framework, with several lines of code, you can do powerful things. Just like Django. :) DRF's documentation is well written then read it, you will find everything you need :)
Then, practice now with tutorials, don't hesitate to post other questions to ask some help. :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.