Mishra
2018-08-28 19:02:43 UTC
I have Three models User, Consumer and Tasks. My consumer is one to one
with user(django inbuilt) and my consumer model is one to many with tasks
i.e one consumer can add many tasks.
My models are like this :
class Consumer(models.Model):
user=models.OneToOneField(User,on_delete=models.CASCADE)
name=models.CharField(max_length=10)
class Tasks(models.Model):
consumer=models.ForeignKey(consumer,on_delete=models.CASCADE)
title=models.CharField(max_length=20)
Now, I know how to serialize and save a nested serializer by overriding
create method. But for that I have to post json like this
{
"user":{
},
"tasks":{
}
"name":"mdkd"
}
( this might not be 100% correct but hope you get the feel)
What I am trying to achieve is, I am implementing an end point for just
posting my task like this :
data={
"title":"ndkandka"
}
(this is my task)
Now I wish to save my task and associate this task with current consumer
logged in. How can I achieve this ? I tried a lot however all I could find
is nested serializer saving by overriding the create function but nowhere I
found individual saving of the entity( like task here) and associating it
with another model (like consumer here).
with user(django inbuilt) and my consumer model is one to many with tasks
i.e one consumer can add many tasks.
My models are like this :
class Consumer(models.Model):
user=models.OneToOneField(User,on_delete=models.CASCADE)
name=models.CharField(max_length=10)
class Tasks(models.Model):
consumer=models.ForeignKey(consumer,on_delete=models.CASCADE)
title=models.CharField(max_length=20)
Now, I know how to serialize and save a nested serializer by overriding
create method. But for that I have to post json like this
{
"user":{
},
"tasks":{
}
"name":"mdkd"
}
( this might not be 100% correct but hope you get the feel)
What I am trying to achieve is, I am implementing an end point for just
posting my task like this :
data={
"title":"ndkandka"
}
(this is my task)
Now I wish to save my task and associate this task with current consumer
logged in. How can I achieve this ? I tried a lot however all I could find
is nested serializer saving by overriding the create function but nowhere I
found individual saving of the entity( like task here) and associating it
with another model (like consumer here).
--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.