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?