Django Class Based View for both Create and Update

I ran into a situation where I wanted something like this. Here’s what I came up with (do note that if you’re trying to use it as an update view and it can’t find the requested object, it’ll behave as a create view rather than throwing a 404): from django.views.generic.detail import SingleObjectTemplateResponseMixin from django.views.generic.edit import … Read more

Return results from multiple models with Django REST Framework

It looks pretty close to me. I haven’t used ViewSets in DRF personally, but I think if you change your code to this you should get somewhere (sorry – not tested either of these): class TimelineViewSet(viewsets.ModelViewSet): “”” API endpoint that lists all tweet/article objects in rev-chrono. “”” def list(self, request): queryset = list(itertools.chain(Tweet.objects.all(), Article.objects.all())) serializer … Read more

Raise 404 and continue the URL chain

This is certainly view logic; all urls.py is for is for matching URL patterns, not performing validation. You can use the Http404 exception to handle this. from django.http import Http404 def detail(request, poll_id): try: p = Poll.objects.get(pk=poll_id) except Poll.DoesNotExist: raise Http404 return render_to_response(‘polls/detail.html’, {‘poll’: p}) Alternatively, you may find the get_object_or_404 or get_list_or_404 methods, which … Read more

Raw SQL queries in Django views

>>> from django.db import connection >>> cursor = connection.cursor() >>> cursor.execute(”’SELECT count(*) FROM people_person”’) 1L >>> row = cursor.fetchone() >>> print row (12L,) >>> Person.objects.all().count() 12 use WHERE clause to filter vote for yes: >>> cursor.execute(”’SELECT count(*) FROM people_person WHERE vote = “yes””’) 1L

In django do models have a default timestamp field?

No such thing by default, but adding one is super-easy. Just use the auto_now_add parameter in the DateTimeField class: created = models.DateTimeField(auto_now_add=True) You can also use auto_now for an ‘updated on’ field. Check the behavior of auto_now here. For auto_now_add here. A model with both fields will look like this: class MyModel(models.Model): created_at = models.DateTimeField(auto_now_add=True) … Read more

How can I list urlpatterns (endpoints) on Django?

If you want a list of all the urls in your project, first you need to install django-extensions You can simply install using command. pip install django-extensions For more information related to package goto django-extensions After that, add django_extensions in INSTALLED_APPS in your settings.py file like this: INSTALLED_APPS = ( … ‘django_extensions’, … ) urls.py … Read more

How to change the file name of an uploaded file in Django?

How are you uploading the file? I assume with the FileField. The documentation for FileField.upload_to says that the upload_to field, may also be a callable, such as a function, which will be called to obtain the upload path, including the filename. This callable must be able to accept two arguments, and return a Unix-style path … Read more