I am trying to make Django view that will give JSON responce with earliest and latest objects. But unfotunately it fails to work with this error.
'str' object has no attribute '_meta'
I have other serialization and it works.
Here is the code.
def get_calendar_limits(request):
result = serializers.serialize("json", Session.objects.aggregate(Max('date'), Min('date')), ensure_ascii=False)
return HttpResponse(result, mimetype="application/javascript")
Thanks a lot beforehand.
I get the same error when trying to serialize an object that is not derived from Django's Model
Python has "json" module. It can 'dumps' and 'loads' function. They can serialize and deserialize accordingly.
Take a look at the following:
objects= Session.objects.aggregate(Max('date'), Min('date'))
print [ type[o] for o in objects ]
result = serializers.serialize("json", objects, ensure_ascii=False)
You might want to just run the above in interactive Python as an experiment.
What type are your objects? Is that type serializable?
Related
This is the code snippet
send = mongo.db.subscription
send.find().update({'$set':{'status': "expired"}})
this is the output i get
send.find().update({'$set':{'status': "expired"}})
AttributeError: 'Cursor' object has no attribute 'update'
The syntax you want is:
send.update_many({<filter_condition>}, {'$set':{'status': "expired"}})
I'm trying to order a firestore collection by the field name, but it's giving me this error: AttributeError: 'CollectionReference' object has no attribute 'orderBy'.
My code:
participants_ref = db.collection("participants")
docs = participants_ref.orderBy("name").stream()
In case this helps, I've also printed out participants_ref and I get the following:
<google.cloud.firestore_v1.collection.CollectionReference object at 0x0000021C9EAB7F40>
According to the documentation, the method you're looking for is order_by. Be sure to switch the code language tab to "Python" to see the correct usage.
docs = participants_ref.order_by("name").stream()
Also see the python API documentation for CollectionReference.
I'm trying to pass some data about a user as JSON, and because the User object has many-to-many relationships, serializing a user as JSON seems to only include the primary key of the m-n object.
(e.g. each user has hobbies, but in the JSON it will only have the PK of the hobbies)
Anyway, I tried constructing a schema to solve this as such:
[[{user}, [hobbies]], [{user}, [hobbies]],...]
But whenever I try to serialize this (in Python it's basically an array with an object and another array in it), I get the error:
'list' object has no attribute '_meta'
Why is this happening and how can I fix it?
EDIT:
Here's the code for it:
for u in allUsers:
if searchedHobby in u.hobbies.all():
user = [u]
userHobbies = []
for hobby in u.hobbies.all():
userHobbies.append(hobby.name)
user.append(userHobbies)
response.append(user)
data = serializers.serialize('json', response)
As seen in the django github repository the serialize method expects a queryset.
What you could do is to do a json.dumps(response) and return that in the HttpResponse.
I have started using pymodm in my project (https://github.com/ypriverol/biocontainers-backend/blob/master/biocontainers/common/models.py). All CRUD operations work very well.
However, when I try to retrieve the list of objects for a specific MongoModel using the objects call:
tool_versions_dic = MongoToolVersion.objects.all()
I got this error:
AttributeError: type object 'MongoToolVersion' has no attribute 'objects'
I am running this exact example:
http://www.django-rest-framework.org/api-guide/serializers
But when I get to this part
serializer = CommentSerializer(data=data)
serializer.is_valid()
# True
serializer.object
# <Comment object at 0x10633b2d0>
Instead of a 'Comment' object I am getting a 'dict' with the values.
Is this a bug? Or am I missing something?
Iam using: djangorestframework-2.3.12 and django1.6.1
Thanks!
This was actually my bad. I copy-pasted the code and the function 'restore_object' was left outside the CommentSerializer class. The default 'restore_object' deserializes to a dict.
Very sorry