logo
down
shadow

Django - Return a response and then update the DB


Django - Return a response and then update the DB

By : Erik A
Date : November 21 2020, 11:01 PM
I wish this helpful for you If you are using uwsgi with enable-threads = true then a simple solution would be:
code :
import threading

def myview(request):
    obj = MyObj()
    threading.Thread(target=save_to_db, args=(obj,)).start()
    return ...

def save_to_db(obj):
    obj.save()


Share : facebook icon twitter icon
Django: How to return a raw response

Django: How to return a raw response


By : user3709157
Date : March 29 2020, 07:55 AM
fixed the issue. Will look into that further You shouldn't return the response from urlopen method directly. Instead your view should return an instance of django's HttpResponse, where body and the headers should be set to those from the original response:
code :
from django.http import HttpResponse
import urllib2

def my_view(request):
    request = urllib2.Request(url, formData, headers)
    response = urllib2.urlopen(request)

    # set the body
    r = HttpResponse(response.read())

    # set the headers
    for header in response.info().keys():
        r[header] = response.info()[header]

    return r
is there a way to return a response first but still does something else after the response? django

is there a way to return a response first but still does something else after the response? django


By : ryslinkee
Date : March 29 2020, 07:55 AM
I hope this helps you . Well, that's more of a Python than Django question. As the comments have pointed out, you could implement some sort of asynchronous queue like Celery however this may be a bit of an overkill for your use case.
Consider using plain Python threads instead:
code :
from threading import Thread


def create_another_obj(name, field):
        another_obj = Another()
        another_obj.name = name
        another_obj.field = field
        another_obj.save()

class Res(View):
    def post(self, request):
        data = request.POST
        new_obj = Model.objects.create(name=data['name'])

        # start another thread to do some work, this is non-blocking
        # and therefore the JsonResponse will be returned while it is
        # running!
        thread = Thread(
                     target=create_another_obj,
                     args=(new_obj.name, new_obj.field),
                 )
        thread.start()

        return JsonResponse({'status': True})
How to return python script response as Http response using Django framework

How to return python script response as Http response using Django framework


By : Akshay Kumar
Date : March 29 2020, 07:55 AM
it fixes the issue I am using Python Django framework, new to it.
code :
for i, product in enumerate(products, 1):
    return HttpResponse(print(str(product)))
resp = ''
for i, product in enumerate(products, 1):
    resp += str(product) + '<br>'
return HttpResponse(resp)
Attempting to override update method in django rest framework to return entire queryset after update

Attempting to override update method in django rest framework to return entire queryset after update


By : Mohammed Hamdy
Date : March 29 2020, 07:55 AM
it fixes the issue I am attempting to override the update method for a put request in django rest framework. Instead of returning just the updated object. I want it to return the entire queryset including the updated object. For the use case I am working on, its just easier. , It should be:
code :
class SearchCityDetail(RetrieveUpdateDestroyAPIView, UpdateReturnAll):
    queryset = SearchCity.objects.all()
    serializer_class = SearchCitySerializer

    def put(self, request, *args, **kwargs):
        return self.updatereturnall(request, *args, **kwargs)
conditionally return json or html response from django django-rest-framework's view

conditionally return json or html response from django django-rest-framework's view


By : Subhabrata Chakladar
Date : March 29 2020, 07:55 AM
wish helps you How to conditionally return json or html response from django django-rest-framework's view. , This worked for me:
Related Posts Related Posts :
  • Django queryset: filter DateTimeField if datetime.now() is greater than field - 24 hours
  • Django: loop over query and return newest related object
  • Programming a fall-back for in-memory python object retrieval
  • Django Rest Framework: how to set a custom name form non_field_errors?
  • Is it better to combine model subclasses and have un-used fields, or create concrete subclasses?
  • I cannot connect django with postgresql
  • Django filter custom look up
  • Django, bundle together count queries?
  • How to index just those models\rows, where some_field=some_value?
  • after login redirect to another page
  • Empty CharField with null=True returns NoneType Django 1.8
  • Django Database Settings : Can we keep default database as empty dict? [Django 1.8]
  • Accepting input in Django ListView
  • How to handle Django connection loss with database gracefully?
  • Trying to filter based upon a value in another table
  • Django url template with a filter
  • How I can create a PointField using an api?
  • Django Rest Framework 'RelatedManager' object has no attribute
  • Django PostgreSQL JSONField db_index error
  • Problems deploying GeoDjango application on Heroku
  • Redirecting to an external website from django view
  • Adding a Django site breaks my virtual hosts on CentOS 7
  • Django-Allauth, Multiple login redirect url
  • django field is required validators
  • How to render templates dynamically for a static user choice
  • Django ORM: Get verbose_name of field via filter lookup
  • How to count number of entries, annotate it and select latest with Django ORM
  • How to generate list of response messages in Django REST Swagger?
  • 'str' object is not callable - SuccessMessageMixin
  • Repeater objects appearing on all detail views - django
  • django admin why my ForeignKey do'nt display?
  • Django Sort by backward foreign key
  • Changing default field validator in Django ModelForm
  • Django Wagtail adding classname to structblock
  • Django CMS - Aldryn Bootstrap broken grid system
  • Extend the list of validators in a subclass model
  • allauth with crispyform : signup not working
  • Having an edit form and a detail view on the same page
  • Django - Remove object from database after day
  • Django & Postgresql annotate datetime field to localtime
  • Update object value from function based view
  • How to easily add Django urls?
  • Django: get related objects of related objects and pass to template
  • Django Query same week last year
  • how to remove reloading the page?
  • Django custom user error
  • Change default GeoModelAdmin to OSMGeoAdmin
  • How to actualize Django Template with get_context_data
  • FileUploadParser doesn't get the file name
  • shadow
    Privacy Policy - Terms - Contact Us © soohba.com