I'm currently trying to carry over object relationship functionality from a php backend to a python backend. In the php application you were able to have a call like so:
~api/v1/cats/5b40b6eb-538f-a015-9196-1cc63cf8d7ae
That would return a result like this:
{
id: "5b40b6eb-538f-a015-9196-1cc63cf8d7ae",
name: "mittens",
hatId: "0845de76-9d68-cfee-0ebb-473ccddf16bc",
}
The functionality that I am trying to replicate is that if you added an include param to the call with the name of foreign object like this:
~api/v1/cats/5b40b6eb-538f-a015-9196-1cc63cf8d7ae?include=hat
Then it would return the original object including the foreign key object
{
id: "5b40b6eb-538f-a015-9196-1cc63cf8d7ae",
name: "mittens",
hatId: "0845de76-9d68-cfee-0ebb-473ccddf16bc",
hat: {
id: "0845de76-9d68-cfee-0ebb-473ccddf16bc",
name: 'top hat',
}
}
All that I've come across so far is select_related, HyperlinkedRelatedField, and prefetch_related; none of these seem to be able to get the above result when implemented.
I would strongly recommend using Django Rest Framework, and specifically look at nested relationships.
The example given in the DRF docs matches what you're looking to do almost exactly (though note, it's a one-to-many field, while your example is a one-to-one field):
>>> album = Album.objects.create(album_name="The Grey Album", artist='Danger Mouse')
>>> Track.objects.create(album=album, order=1, title='Public Service Announcement', duration=245)
<Track: Track object>
>>> Track.objects.create(album=album, order=2, title='What More Can I Say', duration=264)
<Track: Track object>
>>> Track.objects.create(album=album, order=3, title='Encore', duration=159)
<Track: Track object>
>>> serializer = AlbumSerializer(instance=album)
>>> serializer.data
{
'album_name': 'The Grey Album',
'artist': 'Danger Mouse',
'tracks': [
{'order': 1, 'title': 'Public Service Announcement', 'duration': 245},
{'order': 2, 'title': 'What More Can I Say', 'duration': 264},
{'order': 3, 'title': 'Encore', 'duration': 159},
...
],
}
Related
I wanted to add new keys to an existing object in a MongoDB docuemnt, I am trying to update the specific abject with update query but I don't see new keys in database.
I have a object like this:
{'_id': 'patent_1023',
'raw': {'id': 'CN-109897889-A',
'title': 'A kind of LAMP(ring mediated isothermal amplification) product visible detection method',
'assignee': '北京天恩泽基因科技有限公司',
'inventor/author': '徐堤',
'priority_date': '2019-04-17',
'filing/creation_date': '2019-04-17',
'publication_date': '2019-06-18',
'grant_date': None,
'result_link': 'https://patents.google.com/patent/CN109897889A/en', 'representative_figure_link': None
},
'source': 'Google Patent'}
I added two new keys in raw and want to update only 'raw' with new keys 'abstract' and 'description'
Here is what I have done.
d = client.find_one({'_id': {'$in': ids}})
d['raw'].update(missing_data) # missing_data contain new keys to be added in raw.
here = client.find_one_and_update({'_id': d['_id']}, {'$set': {"raw": d['raw']}})
Both update_one and update_many will work with this:
missing_data = {'abstract':'a book', 'description':'a fun book'};
ids = [ 'patent_1023', 'X'];
rc=db.foo.update_one(
{'_id': {'$in': ids}},
# Use pipeline form of update to exploit richer agg framework
# function like $mergeObjects. Below we are saying "take the
# incoming raw object, overlay the missing_data object on top of
# it, and then set that back into raw and save":
[ {'$set': {
'raw': {'$mergeObjects': [ '$$ROOT.raw', missing_data ] }
}}
]
)
I am connecting to Google AdWords via API v201802 and most recent Python3 googleads module. The connection works fine and I can retrieve data for Campaigns or Ad Groups but the fields selector seems not to work. I'd like to request only a few fields, but I receive all available fields. Am I overlooking something?
from googleads import adwords
adwords_client = adwords.AdWordsClient.LoadFromStorage()
ad_group_service = adwords_client.GetService('AdGroupService', version='v201802')
selector = {
'fields': ['Id', 'Name', 'Status', 'CampaignId'],
'paging': {
'startIndex': '0',
'numberResults': '500'
}
}
page = ad_group_service.get(selector)
print(page)
Result:
{
'totalNumEntries': 138,
'Page.Type': 'AdGroupPage',
'entries': [
{
'id': 44831117552,
'campaignId': 888843682,
'campaignName': None,
'name': '001_0001_BMM_xxx',
'status': 'ENABLED',
'settings': [],
'labels': [],
'forwardCompatibilityMap': [],
'biddingStrategyConfiguration': None,
'contentBidCriterionTypeGroup': None,
'baseCampaignId': None,
'baseAdGroupId': None,
'trackingUrlTemplate': None,
'finalUrlSuffix': None,
'urlCustomParameters': None,
'adGroupType': None,
'adGroupAdRotationMode': None
},
...
] }
Of course I can filter out the unneeded fields when processing the response, but I wonder why the fields selector is not working...
I just found an answer to this in adwords-api google groups:
Hi Kevin,
The API will always include the fields your are requesting in the
response, but it might also add other fields that will be grouped
together to some of your original fields.
Best,
David Torres - AdWords API Team
I want to do bulk images POST to the server.
And the same image will be upload to multiple records in Django 1.11 and Django REST 3.6.1
for image in images:
data = {
'type': image_type,
'attribute': {
'dealer_id': int(dealer_id),
},
'defaults': {
'created_user': user,
'updated_user': user,
'image': image,
}
}
for shop_id in shops:
DisplayImage.objects.update_or_create(**data)
First image does not raises the error. During debugging I had try query that DisplayImage instance, but it returns empty queryset. It is not a problem because ORM may be lazy-commit.
The error I have is I/O Operation on closed file
Update:
To keep attention from the community. I will keep adding information while I am working on this until I give up.
data == copy_data
Out[2]: False
data
Out[3]:
{'attribute': {'dealer_id': 2},
'created_user': <SimpleLazyObject: <User: admin>>,
'image': <InMemoryUploadedFile: コーティング_車内清掃_室内清掃+除菌抗菌消臭_内容.png (image/png)>,
'type': 1,
'updated_user': <SimpleLazyObject: <User: admin>>}
copy_data
Out[4]:
{'attribute': {'dealer_id': 2},
'created_user': <User: admin>,
'image': <InMemoryUploadedFile: コーティング_車内清掃_室内清掃+除菌抗菌消臭_内容.png (image/png)>,
'type': 1,
'updated_user': <User: admin>}
I had tried copy.deepcopy(data) which is a dict that I want to create an instance of DisplayImage, but when the first one went to the database the next one got closed!
I am ware that it must be different object that is the reason why I check the value by data == copy_data and it returns False
I use Falcon framework and neomodel in order to communicate with neo4j database.
I have some nodes in DB and I try to return information about them via API (get methon) as a JSON object.
In order to retrive information I use the code people = Person.nodes
I iterate throu people:
for p in people:
print(p)
and I get:
{'name': 'John', 'id': 0, 'uid': '584d9b0517584b8194f222052bf177ff'}
{'name': 'Paul', 'id': 1, 'uid': 'f5763c01704e449885f846e87e1fcb6d'}
When I do json.dumps() on single entity I get an error:
TypeError: <Person: {'name': 'John', 'id': 0, 'uid': '584d9b0517584b8194f222052bf177ff'}> is not JSON serializable
How can I convert neomodel object into json object?
Using json.dumps(p.__properties__) does the trick. Using p.__dict__ tries to encode the neomodel property classes, which will throw an error.
It seems like every p in your people is an object.
Try something like json.dumps(p.__dict__). If it's a common neomodel node object then this should work.
A bit of an old question but this is how I work with this..
Creating a function on the class so I can control what data to return. With __properies__ instead of the .to_json funtion you will get all properties.
class Player(StructuredNode):
mid = IntegerProperty(unique_index=True)
f_name = StringProperty()
l_name = StringProperty()
email = StringProperty()
team = RelationshipFrom('Team', 'PLAYER', model=PlayerRel)
def to_json(self):
return {
"id": self.mid,
"firstName": self.f_name,
"lastName": self.l_name,
"email": self.email,
"fullName": self.f_name + ' ' + self.l_name
}
Then I have a node with has several Players connected and I just do this to return an array of players that can be serialized:
...
team = Team.nodes.get(team_id=id)
return ([player.to_json() for player in team.players])
I want to save an array of objects passed from javascript through ajax to me database. This is my view code:
data2 = json.loads(request.raw_get_data)
for i in data2:
print(key)
obj = ShoppingCart(quantity = i.quantity , user_id = 3, datetime = datetime.now(), product_id = i.pk)
obj.save()
return render_to_response("HTML.html",RequestContext(request))
After the first line, i get this in my dictionary:
[{'model': 'Phase_2.product', 'fields': {'name': 'Bata', 'category': 2, 'quantity': 1, 'subcategory': 1, 'count': 2, 'price': 50}, 'imageSource': None, 'pk': 1}]
(Only one object in the array right now)
I want to be able access individual fields like quantity, id, etc in order to save the data to my database. When i debug this code, it gives a name error on 'i'. I also tried accessing the fields like this: data2[0].quantity but it gives this error: {AttributeError}dict object has no attribute quantity.
Edited code:
for i in data2:
name = i["fields"]["name"]
obj = ShoppingCart(quantity = i["fields"]["quantity"] , user_id = 3, datetime = datetime.now(), product_id = i["fields"]["pk"])
obj.save()
It might help you to visualise the returned dict with proper formatting:
[
{
'model': 'Phase_2.product',
'fields': {
'name': 'Bata',
'category': 2,
'quantity': 1,
'subcategory': 1,
'count': 2,
'price': 50
},
'imageSource': None,
'pk': 1
}
]
The most likely reason for your error is that you are trying to access values of of the inner 'fields' dictionary as if they belong to the outer i dictionary.
i.e.
# Incorrect
i["quantity"]
# Gives KeyError
# Correct
i["fields"]["quantity"]
Edit
You have the same problem in your update:
# Incorrect
i["fields"]["pk"]
# Correct
i["pk"]
The "pk" field is in the outer dictionary, not the inner "fields" dictionary.
You may try:
i['fields']['quantity']
The json.loads() returns you a dictionary, which should be accessed by key.