Using json to copy data (Django)

2

I thought better, and I think I'll use json.

def entry_detail_json(request, pk):
    data = Entry.objects.filter(pk=pk)
    s = serializers.serialize("json", data)
    return HttpResponse(s)

But being on the page

http://localhost:8000/entry/2/

How do I refer to the page

http://localhost:8000/entry/json/2/

And assign the values in the following function to copy the data?

I got the result as follows:

<input name="new_proposal" type="submit" class="btn btn-primary" value="{{ entry.id }}" 

views.py

def create_proposal(request, employee_pk=1, **kwargs):
    f = None
    if request.method == 'GET':
        f = request.GET['new_proposal']
    if f:
        employee = Employee.objects.get(pk=employee_pk)  # TODO
        nlp = NumLastProposal.objects.get(pk=1)  # sempre pk=1
        # entry = Entry.objects.get(pk=kwargs.get('pk', None))
        entry = Entry.objects.get(pk=f)
        obj = Proposal(
            num_prop=nlp.num_last_prop + 1,
            type_prop='R',
            category=entry.category,
            description=entry.description,
            work=entry.work,
            person=entry.person,
            employee=employee,
            seller=entry.seller,
        )
        obj.save()
        # Define que foi dado entrada
        entry.is_entry = True
        entry.save()
        # Incrementa o número do último orçamento
        nlp.num_last_prop += 1
        nlp.save()
        print('Orçamento criado com sucesso')
    return redirect('proposal_list')

It's working, but I know it's not the best way to handle it.

And in the end I ended up not using JSON. How would I do to use JSON instead of doing as I did?

[{"pk": 2, "fields": {"work": 2, "description": "Ih1vwUcIYwc0ce", "created": "2015-07-31T19:41:04.408Z", "priority": "u", "category": 1, "person": 45, "seller": 2, "is_entry": true, "modified": "2015-07-31T21:11:59.165Z"}, "model": "core.entry"}]
    
asked by anonymous 30.07.2015 / 22:29

1 answer

0

As I understand it, you need to send the value entry.id (in this case 2) to the "entry-detail-json" view which, in your case, you are sending by GET parameter, right?

You can give the url a name and send the "entry.id" as a url parameter.

For example, imagine that you have the url that will process json:

 url(r'^entry-detail-json/(?P<pk>\d*)', 
     'link.para.entry_detail_json',  
      name='entry-detail-json')

Note the

... "name='entry-detail-json'"... 

In order to access this url in the template where you have entry.id, just write:

<a href="{% url 'entry-detail-json' pk=entry.id %}">Json entry</a>

I think in your case, you really need this line:

{% url 'nome_do_url' nome_do_parametro=valor_do_parametro %}

In this case the value of "entry.id" will be injected into the view as the name "pk":

def entry_detail_json(request, pk):
    data = Entry.objects.filter(pk=pk)
    s = serializers.serialize("json", data)
    return HttpResponse(s)

You can also call the url from another view:

return redirect('entry-detail-json', pk=entry.id)

You can even pass more than one parameter, making this url a kind of json output

url:

url(r'^json-output/(?P<obj_type>\w*)/(?P<pk>\d*)',
    'link.para.json_output', 
     name='json-ouput')

view:

import json

def json_output(request, obj_type=None, pk=None):

    if obj_type is None or pk is None:
        return HttpResponseBadRequest

    if obj_type == "entry" :
        data = Entry.objects.filter(pk=pk)
        return HttpResponse(json.dumps(data)), mimetype='application/json')
    elif obj_type == "other_object"
        data = OtherObject.objects.filter(pk=pk)
        return HttpResponse(json.dumps(data)), mimetype='application/json')
    else:
        return HttpResponseBadRequest

template:

<a href="{% url 'json-output' obj_type="entry" pk=entry.id %}">Json entry</a>
<a href="{% url 'json-output' obj_type="other_object" pk=other.id %}">Json Object</a>

I hope this is what you are looking for.

Greetings

    
08.08.2015 / 02:06