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.

On Friday, May 29, 2020 at 3:34:39 PM UTC+5:30, Sencer Hamarat wrote:
>
> I believe, the method "get_query*set*" 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 <[email protected] 
> <javascript:>> 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 [email protected] <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/django-users/3fdc04cb-5da9-4d6a-8935-3ebf9409196b%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/django-users/3fdc04cb-5da9-4d6a-8935-3ebf9409196b%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/560f2715-eae2-4dd7-9172-f71acd5368d7%40googlegroups.com.

Reply via email to