Friday, May 29, 2020

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

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



On Fri, May 29, 2020 at 4:43 AM django-newbie <arjitk.gupta@gmail.com> wrote:
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?

--
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+unsubscribe@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.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CACp8TZjA1okZy2-0pAk5f4A6uCxThxHnobtDsnOG%2BJ8_LXk%2BBA%40mail.gmail.com.

No comments:

Post a Comment