0đź‘Ť
If the game resource look like:
class GameResource(ModelResource):
activities = fields.ToManyField(ActivityResource, 'activities', full=True)
Following the note in tastypie documentation:
Tastypie encourages “round-trippable” data, which means the data you can GET should be able to be POST/PUT’d back to recreate the same object.
If you’re ever in question about what you should send, do a GET on another object & see what Tastypie thinks it should look like.
You will be able to create all in one batch.
3đź‘Ť
use related name for the fields which accepts the creation of related objects
RelatedField.related_name
Used to help automatically populate reverse relations when creating data. Defaults to None.
In order for this option to work correctly, there must be a field on the other Resource with this as an attribute/instance_name. Usually this just means adding a reflecting ToOneField pointing back.
Example:
class EntryResource(ModelResource): authors = fields.ToManyField('path.to.api.resources.AuthorResource', 'author_set', related_name='entry')
class Meta:
queryset = Entry.objects.all()
resource_name = 'entry'
class AuthorResource(ModelResource):
entry = fields.ToOneField(EntryResource, 'entry')
class Meta:
queryset = Author.objects.all()
resource_name = 'author'
Use of related_name do the task. it maps the objects of related fields and automatically populates the relations when creating data.
- [Django]-Django-rest returning http 301 status code when calling http delete without trailing slash
- [Django]-Custom metrics from celery workers into prometheus
2đź‘Ť
See the documentation section entitled Bulk Operations. It begins:
As an optimization, it is possible to do many creations, updates, and deletions to a collection in a single request by sending a PATCH to the list endpoint.
And here’s their example:
curl --dump-header - -H "Content-Type: application/json" -X PATCH --data '{"objects": [{"body": "Surprise! Another post!.", "pub_date": "2012-02-16T00:46:38", "slug": "yet-another-post", "title": "Yet Another Post"}], "deleted_objects": ["http://localhost:8000/api/v1/entry/4/"]}' http://localhost:8000/api/v1/entry/
- [Django]-How do you add similar posts to a detail page using django
- [Django]-"from . import views": Unresolved import
- [Django]-How to save django FileField to user folder?
- [Django]-How to redirect to a dynamic url in django
- [Django]-Django expression DateTime and Integer seconds
0đź‘Ť
You can create resource for activity and use fields.ToManyField:
https://django-tastypie.readthedocs.org/en/latest/resources.html#reverse-relationships
This will add urls to activity resources. To fully inline data for activities just pass (full=True, full_list=True) as arguments to ToManyField:
https://django-tastypie.readthedocs.org/en/latest/fields.html#tastypie.fields.RelatedField.full_list
- [Django]-Issues with Celery configuration on AWS Elastic Beanstalk – "No config updates to processes"
- [Django]-How to display a quote in django template
- [Django]-Upgrading from Django 1.4 to Django 1.7 – will it work?
- [Django]-Django-paypal does not receive ipn signal
- [Django]-Django-pipeline throwing ValueError: the file could not be found