Allow saving object without needing to add foreign key attribute to class Meta field #9138
Unanswered
AmayaKuro
asked this question in
Ideas & Suggestions
Replies: 1 comment
-
There seem to be another way that DRF allow us to exactly like that by passing foreign object in save(): data = {
# Doesn't need this anymore
# "conversation": conversation_id,
"response_id": response_id,
"log": log
}
serializer = ResponseSerializer(data=data)
if serializer.is_valid():
# Syntax: serializer.save(foreignKeyField = anObjectOfThatForeignModel)
serializer.save(conversation = conversation_id) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
PROBLEM:
Hello everyone, I came across
django.db.utils.IntegrityError: NOT NULL constraint failed: conversation_id
when I try to save an object via Serializer that doesn't containconversation_id
, which is a foreign key toConversation
, infields
variable of class Meta.models.py
serializers.py:
views.py
IDEA:
Is it better to allow user to provide Foreign key for saving object and not specify Foreign key in field variable?
Ex:
And:
or forcing user to give key in the context:
Using this, user can get rid of some overhead cost or just make JSON prettier.
Thanks for your attention~
Beta Was this translation helpful? Give feedback.
All reactions