How models.Manager is working behind the scene and calling different serialize methods?

38 views
Skip to first unread message

django-newbie

unread,
May 28, 2020, 9:43:34 PM5/28/20
to Django users
Hi,
I am new to django and going through a tutorial and now confused with one particular implementation.

models.py
import json
from django.core.serializers import serialize

from django.db import models
from django.conf import settings

# Create your models here.


def upload_update_image(instance, filename):
return "updates/{owner}/{filename}".format(owner=instance.owner, filename=filename)

class UpdateQuerySet(models.QuerySet):

def serialize(self):
print("UpdateQuerySet serialize")
print(self.values("owner", "content", "image"))
list_values = list(self.values("owner", "content", "image"))
return json.dumps(list_values)


class UpdateManager(models.Manager):

def get_queryset(self):
print('get_queryset method')
return UpdateQuerySet(self.model, using=self._db)


class BasicUserUpdate(models.Model):
owner = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
content = models.TextField(blank=True,null=True)
image = models.ImageField(blank=True,null=True,upload_to=upload_update_image,)
updated = models.DateTimeField(auto_now=True)
timestamp = models.DateTimeField(auto_now_add=True)

def __str__(self):
return self.content or ''

objects = UpdateManager()


def serialize(self):
print("BasicUserUpdate serialize")
try:
image = self.image.url
except:
image = ""
data = {
"content": self.content,
"owner": self.owner.id,
"image": image
}
data = json.dumps(data)
return data
 
views.py
from basic.models import BasicUserUpdate
from django.views import View
from django.http import HttpResponse


class BasicUserUpdateListView(View):
def get(self, request, *args, **kwargs):
qs = BasicUserUpdate.objects.all()
json_data = qs.serialize()
return HttpResponse(json_data, content_type='application/json')

class BasicUserUpdateDetailView(View):

def get(self, request,id, *args, **kwargs):
qs = BasicUserUpdate.objects.get(id=id)
json_data = qs.serialize()
return HttpResponse(json_data, content_type='application/json')

]

At run time calling ListView, get below print messages
get_queryset method
UpdateQuerySet serialize

At run time calling DetailView, get below print messages
get_queryset method
BasicUserUpdate serialize


How Django is identifying which serialize method to be called?

Sencer Hamarat

unread,
May 29, 2020, 6:04:39 AM5/29/20
to django...@googlegroups.com
I believe, the method "get_queryset" name is explains what you want to know.

If you filter or fetch every thing with django query, it returns queryset.
The .get() query on the other hand, returns a single object.

Saygılarımla,
Sencer HAMARAT



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/3fdc04cb-5da9-4d6a-8935-3ebf9409196b%40googlegroups.com.

django-newbie

unread,
May 29, 2020, 10:26:17 AM5/29/20
to Django users
Ohk, I got it now..
Thanks for clarifying this.

My further doubts in continuation of this are.

1. For confirmation, ModelName.objects.all() calls get_queryset() method, while ModelName.objects.get calls get() method internally.

2. In above code to override default serialize method of BasicUserUpdate for get_queryset() method,
We have to do 2 things.
1. Inherit default models.Manager and override get_queryset() method.
2. Inherit  models.QuerySet and override default serialize method.

Honestly it seems overkill for this problem.
Can u please suggest alternative straight forward way to do it in model only.
To unsubscribe from this group and stop receiving emails from it, send an email to django...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages