serialize () включен в класс django Tools_booked, но при попытке доступа к этому методу он выдает ошибку.
'QuerySet' object has no attribute 'serialize'
models.py
from django.core.serializers import serialize
class UpdateQuerySet(models.QuerySet):
def serialize(self):
print("*****Entered the serizlize inside the UpdateQuerySet models **********")
qs = self
return serialize('json', qs, fields=('auto_increment_id','user','component','booked'))
class UpdateManager(models.Manager):
def get_queryset(self):
return UpdateQuerySet(self.model, using=self._db)
class Tools_booked(models.Model):
auto_increment_id = models.AutoField(primary_key=True)
user=models.ForeignKey(Profile, on_delete=models.CASCADE)
component = models.CharField(max_length=30, blank=True)
booked = models.DateTimeField(auto_now_add=True,blank=True)
objects = UpdateManager()
def __str__(self):
return self.component
def serialize(self):
json_data = serialize("json", [self], fields=['auto_increment_id','user','component','booked'])
stuct = json.loads(json_data)
print(struct)
data = json.dump(stuct[0]['fields'])
return data
views.py
class SerializedDetialView(View):
def get(self, request, *args, **kwargs):
print("*****Entered the SerializedDetialView **********")
obj_list= Tools_booked.objects.filter(auto_increment_id=1)
json_data = obj_list.serialize()
return HttpResponse(json_data, content_type='application/json')
class SerializedListView(View):
def get(self, request, *args, **kwargs):
json_data = Tools_booked.objects.all().serialize()
return HttpResponse(json_data, content_type='application/json')
Ошибка трассировки
json_data = Tools_booked.objects.all().serialize()
AttributeError: 'QuerySet' object has no attribute 'serialize'
Но это работает.
class SerializedDetialView(View):
def get(self, request, *args, **kwargs):
obj_list= Tools_booked.objects.filter(auto_increment_id=1)
json_data = serializers.serialize("json", obj_list )
return HttpResponse(json_data, content_type='application/json')
class SerializedListView(View):
def get(self, request, *args, **kwargs):
qs = Tools_booked.objects.all()
json_data = serializers.serialize("json", qs )
return HttpResponse(json_data, content_type='application/json')
Как использовать метод serialize () внутри класса models.py, Tools_booked.