How to POST ManyToMany Data with Json?How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How do I sort a dictionary by value?How to make a chain of function decorators?Why can't Python parse this JSON data?How do I list all files of a directory?Django-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to implement update_or_create inside create method of ModelSerializer
Applicants clearly not having the skills they advertise
Why does the UK have more political parties than the US?
Have powerful mythological heroes ever run away or been deeply afraid?
What is a simple, physical situation where complex numbers emerge naturally?
If Sweden was to magically float away, at what altitude would it be visible from the southern hemisphere?
Future enhancements for the finite element method
The term for the person/group a political party aligns themselves with to appear concerned about the general public
How much current can Baofeng UV-5R provide on +V pin?
Rotated Position of Integers
Can The Malloreon be read without first reading The Belgariad?
If a problem only occurs randomly once in every N times on average, how many tests do I have to perform to be certain that it's now fixed?
What is a natural deduction proof from ~(A↔B) to ~(A→B)?
Can you use a concentration spell while using Mantle of Majesty?
What if you don't bring your credit card or debit for incidentals?
What does it mean by "d-ism of Leibniz" and "dotage of Newton" in simple English?
How do I get a list of only the files (not the directories) from a package?
Constructing a CCY Gate
Explain Ant-Man's "not it" scene from Avengers: Endgame
Why is Colorado so different politically from nearby states?
Why were the Night's Watch required to be celibate?
Is American Express widely accepted in France?
Does Peach's float negate shorthop knockback multipliers?
Modern approach to radio buttons
Recording the inputs of a command and producing a list of them later on
How to POST ManyToMany Data with Json?
How to merge two dictionaries in a single expression?How do I check if a list is empty?How do I check whether a file exists without exceptions?How can I safely create a nested directory?How do I sort a dictionary by value?How to make a chain of function decorators?Why can't Python parse this JSON data?How do I list all files of a directory?Django-Rest-Framework - How to serialize queryset from an unrelated model as nested serializerHow to implement update_or_create inside create method of ModelSerializer
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I have a data like this.
enter image description here
model.py
from django.db import models
class Diet(models.Model):
date = models.CharField(max_length=20)
building = models.CharField(max_length=40)
location = models.CharField(max_length=40)
restaurants = models.ManyToManyField('Restaurant')
def __str__(self):
return self.building
class Restaurant(models.Model):
name = models.CharField(max_length=40)
menus = models.ManyToManyField('DietMenu')
def __str__(self):
return self.name
class DietMenu(models.Model):
name = models.CharField(max_length=256)
price = models.CharField(max_length=20)
def __str__(self):
return self.name
api.py
from .models import Diet
from .models import Restaurant
from .models import DietMenu
from rest_framework import serializers, viewsets
class DietMenuSerializer(serializers.ModelSerializer):
class Meta:
model = DietMenu
fields = ('name', 'price')
class DietMenuViewSet(viewsets.ModelViewSet):
queryset = DietMenu.objects.all()
serializer_class = DietMenuSerializer
class RestaurantSerializer(serializers.ModelSerializer):
menus = DietMenuSerializer(many=True, read_only=True)
class Meta:
model = Restaurant
fields = ('name', 'menus')
class RestaurantViewSet(viewsets.ModelViewSet):
queryset = Restaurant.objects.all()
serializer_class = RestaurantSerializer
class DietSerializer(serializers.ModelSerializer):
restaurants = RestaurantSerializer(many=True, read_only=True)
class Meta:
model = Diet
fields = ('date', 'building', 'location', 'restaurants')
class DietViewSet(viewsets.ModelViewSet):
queryset = Diet.objects.all()
serializer_class = DietSerializer
And I get a json of the data like this.
enter image description here
import requests
class DjangoJson(object):
def __init__(self):
self.url = "url"
def set_diet_date(self):
self.url = "http://localhost:8000/api/v1/diet/?format=json"
def get(self):
self.set_diet_date()
request = requests.get(self.url)
document = request.json()
return document
def post(self, param):
requests.post(self.url, json=param)
if __name__ == "__main__":
json = DjangoJson()
doc = json.get()[0]
json.post(doc)
Then I POST this json by using the requests library. But the restaurant field is empty.
enter image description here
Is there an way to POST ManyToMany data with Json?
Thanks.
python django-rest-framework
add a comment |
I have a data like this.
enter image description here
model.py
from django.db import models
class Diet(models.Model):
date = models.CharField(max_length=20)
building = models.CharField(max_length=40)
location = models.CharField(max_length=40)
restaurants = models.ManyToManyField('Restaurant')
def __str__(self):
return self.building
class Restaurant(models.Model):
name = models.CharField(max_length=40)
menus = models.ManyToManyField('DietMenu')
def __str__(self):
return self.name
class DietMenu(models.Model):
name = models.CharField(max_length=256)
price = models.CharField(max_length=20)
def __str__(self):
return self.name
api.py
from .models import Diet
from .models import Restaurant
from .models import DietMenu
from rest_framework import serializers, viewsets
class DietMenuSerializer(serializers.ModelSerializer):
class Meta:
model = DietMenu
fields = ('name', 'price')
class DietMenuViewSet(viewsets.ModelViewSet):
queryset = DietMenu.objects.all()
serializer_class = DietMenuSerializer
class RestaurantSerializer(serializers.ModelSerializer):
menus = DietMenuSerializer(many=True, read_only=True)
class Meta:
model = Restaurant
fields = ('name', 'menus')
class RestaurantViewSet(viewsets.ModelViewSet):
queryset = Restaurant.objects.all()
serializer_class = RestaurantSerializer
class DietSerializer(serializers.ModelSerializer):
restaurants = RestaurantSerializer(many=True, read_only=True)
class Meta:
model = Diet
fields = ('date', 'building', 'location', 'restaurants')
class DietViewSet(viewsets.ModelViewSet):
queryset = Diet.objects.all()
serializer_class = DietSerializer
And I get a json of the data like this.
enter image description here
import requests
class DjangoJson(object):
def __init__(self):
self.url = "url"
def set_diet_date(self):
self.url = "http://localhost:8000/api/v1/diet/?format=json"
def get(self):
self.set_diet_date()
request = requests.get(self.url)
document = request.json()
return document
def post(self, param):
requests.post(self.url, json=param)
if __name__ == "__main__":
json = DjangoJson()
doc = json.get()[0]
json.post(doc)
Then I POST this json by using the requests library. But the restaurant field is empty.
enter image description here
Is there an way to POST ManyToMany data with Json?
Thanks.
python django-rest-framework
django-rest-framework.org/api-guide/relations/… django-rest-framework.org/api-guide/serializers/…
– mariodev
Mar 24 at 16:59
Sorry for late. It helps me. read_only is problem. Thanks.
– hyrama
Mar 29 at 15:45
add a comment |
I have a data like this.
enter image description here
model.py
from django.db import models
class Diet(models.Model):
date = models.CharField(max_length=20)
building = models.CharField(max_length=40)
location = models.CharField(max_length=40)
restaurants = models.ManyToManyField('Restaurant')
def __str__(self):
return self.building
class Restaurant(models.Model):
name = models.CharField(max_length=40)
menus = models.ManyToManyField('DietMenu')
def __str__(self):
return self.name
class DietMenu(models.Model):
name = models.CharField(max_length=256)
price = models.CharField(max_length=20)
def __str__(self):
return self.name
api.py
from .models import Diet
from .models import Restaurant
from .models import DietMenu
from rest_framework import serializers, viewsets
class DietMenuSerializer(serializers.ModelSerializer):
class Meta:
model = DietMenu
fields = ('name', 'price')
class DietMenuViewSet(viewsets.ModelViewSet):
queryset = DietMenu.objects.all()
serializer_class = DietMenuSerializer
class RestaurantSerializer(serializers.ModelSerializer):
menus = DietMenuSerializer(many=True, read_only=True)
class Meta:
model = Restaurant
fields = ('name', 'menus')
class RestaurantViewSet(viewsets.ModelViewSet):
queryset = Restaurant.objects.all()
serializer_class = RestaurantSerializer
class DietSerializer(serializers.ModelSerializer):
restaurants = RestaurantSerializer(many=True, read_only=True)
class Meta:
model = Diet
fields = ('date', 'building', 'location', 'restaurants')
class DietViewSet(viewsets.ModelViewSet):
queryset = Diet.objects.all()
serializer_class = DietSerializer
And I get a json of the data like this.
enter image description here
import requests
class DjangoJson(object):
def __init__(self):
self.url = "url"
def set_diet_date(self):
self.url = "http://localhost:8000/api/v1/diet/?format=json"
def get(self):
self.set_diet_date()
request = requests.get(self.url)
document = request.json()
return document
def post(self, param):
requests.post(self.url, json=param)
if __name__ == "__main__":
json = DjangoJson()
doc = json.get()[0]
json.post(doc)
Then I POST this json by using the requests library. But the restaurant field is empty.
enter image description here
Is there an way to POST ManyToMany data with Json?
Thanks.
python django-rest-framework
I have a data like this.
enter image description here
model.py
from django.db import models
class Diet(models.Model):
date = models.CharField(max_length=20)
building = models.CharField(max_length=40)
location = models.CharField(max_length=40)
restaurants = models.ManyToManyField('Restaurant')
def __str__(self):
return self.building
class Restaurant(models.Model):
name = models.CharField(max_length=40)
menus = models.ManyToManyField('DietMenu')
def __str__(self):
return self.name
class DietMenu(models.Model):
name = models.CharField(max_length=256)
price = models.CharField(max_length=20)
def __str__(self):
return self.name
api.py
from .models import Diet
from .models import Restaurant
from .models import DietMenu
from rest_framework import serializers, viewsets
class DietMenuSerializer(serializers.ModelSerializer):
class Meta:
model = DietMenu
fields = ('name', 'price')
class DietMenuViewSet(viewsets.ModelViewSet):
queryset = DietMenu.objects.all()
serializer_class = DietMenuSerializer
class RestaurantSerializer(serializers.ModelSerializer):
menus = DietMenuSerializer(many=True, read_only=True)
class Meta:
model = Restaurant
fields = ('name', 'menus')
class RestaurantViewSet(viewsets.ModelViewSet):
queryset = Restaurant.objects.all()
serializer_class = RestaurantSerializer
class DietSerializer(serializers.ModelSerializer):
restaurants = RestaurantSerializer(many=True, read_only=True)
class Meta:
model = Diet
fields = ('date', 'building', 'location', 'restaurants')
class DietViewSet(viewsets.ModelViewSet):
queryset = Diet.objects.all()
serializer_class = DietSerializer
And I get a json of the data like this.
enter image description here
import requests
class DjangoJson(object):
def __init__(self):
self.url = "url"
def set_diet_date(self):
self.url = "http://localhost:8000/api/v1/diet/?format=json"
def get(self):
self.set_diet_date()
request = requests.get(self.url)
document = request.json()
return document
def post(self, param):
requests.post(self.url, json=param)
if __name__ == "__main__":
json = DjangoJson()
doc = json.get()[0]
json.post(doc)
Then I POST this json by using the requests library. But the restaurant field is empty.
enter image description here
Is there an way to POST ManyToMany data with Json?
Thanks.
python django-rest-framework
python django-rest-framework
asked Mar 24 at 10:44
hyramahyrama
112
112
django-rest-framework.org/api-guide/relations/… django-rest-framework.org/api-guide/serializers/…
– mariodev
Mar 24 at 16:59
Sorry for late. It helps me. read_only is problem. Thanks.
– hyrama
Mar 29 at 15:45
add a comment |
django-rest-framework.org/api-guide/relations/… django-rest-framework.org/api-guide/serializers/…
– mariodev
Mar 24 at 16:59
Sorry for late. It helps me. read_only is problem. Thanks.
– hyrama
Mar 29 at 15:45
django-rest-framework.org/api-guide/relations/… django-rest-framework.org/api-guide/serializers/…
– mariodev
Mar 24 at 16:59
django-rest-framework.org/api-guide/relations/… django-rest-framework.org/api-guide/serializers/…
– mariodev
Mar 24 at 16:59
Sorry for late. It helps me. read_only is problem. Thanks.
– hyrama
Mar 29 at 15:45
Sorry for late. It helps me. read_only is problem. Thanks.
– hyrama
Mar 29 at 15:45
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322966%2fhow-to-post-manytomany-data-with-json%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55322966%2fhow-to-post-manytomany-data-with-json%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
django-rest-framework.org/api-guide/relations/… django-rest-framework.org/api-guide/serializers/…
– mariodev
Mar 24 at 16:59
Sorry for late. It helps me. read_only is problem. Thanks.
– hyrama
Mar 29 at 15:45