Cannot set values on a ManyToManyField which specifies an intermediary model












0















How can I set values on to a ManyToManyField which specifies an intermediary model?



In the models.py



class BookUser(models.Model):
email = models.EmailField()

class Book(models.Model):
author_id= models.CharField(max_length=255)
send_to = models.ManyToManyField(BookUser, through='BookUserRelationship')
book_id = models.AutoField(primary_key=True)
file_size = models.CharField(null=True)

class BookUserRelationship(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
user = models.ForeignKey(BookUser, on_delete=models.CASCADE)
shared_date = models.DateTimeField(auto_now_add=True,null=True,blank=True)


Tried to update in serializers.py



class BookSerializer(serializers.ModelSerializer):
send_to = BookUserSerializer(many=True, read_only=True)

class Meta():
model = Book
fields = ('book_id', 'author_id','file_size','send_to')

class BookUserSerializer(serializers.ModelSerializer):
model = BookUser
fields = ('email')


In the views.py for listing the books by passing the book_id as query params



class BookListView(generics.ListCreateAPIView):
serializer_class = serializers.BookSerializer

def get(self, request, *args, **kwargs):
user = self.request.user
book_id = self.request.query_params.get('book_id', None)
if book_id:
book = models.Book.objects.filter(book_id=book_id)
return Response(serializers.BookSerializer(book[0]).data)









share|improve this question

























  • This is too broad. You need to show your models, and what you tried.

    – Daniel Roseman
    Nov 19 '18 at 9:46











  • I am very new in the Django drf programming, can you please help me with the problem? Edited the question and added the models.

    – kkr
    Nov 19 '18 at 10:09











  • Thanks but you also need to show how you are trying to set the values.

    – Daniel Roseman
    Nov 19 '18 at 10:11











  • Can you please help me with solution

    – kkr
    Nov 19 '18 at 11:08
















0















How can I set values on to a ManyToManyField which specifies an intermediary model?



In the models.py



class BookUser(models.Model):
email = models.EmailField()

class Book(models.Model):
author_id= models.CharField(max_length=255)
send_to = models.ManyToManyField(BookUser, through='BookUserRelationship')
book_id = models.AutoField(primary_key=True)
file_size = models.CharField(null=True)

class BookUserRelationship(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
user = models.ForeignKey(BookUser, on_delete=models.CASCADE)
shared_date = models.DateTimeField(auto_now_add=True,null=True,blank=True)


Tried to update in serializers.py



class BookSerializer(serializers.ModelSerializer):
send_to = BookUserSerializer(many=True, read_only=True)

class Meta():
model = Book
fields = ('book_id', 'author_id','file_size','send_to')

class BookUserSerializer(serializers.ModelSerializer):
model = BookUser
fields = ('email')


In the views.py for listing the books by passing the book_id as query params



class BookListView(generics.ListCreateAPIView):
serializer_class = serializers.BookSerializer

def get(self, request, *args, **kwargs):
user = self.request.user
book_id = self.request.query_params.get('book_id', None)
if book_id:
book = models.Book.objects.filter(book_id=book_id)
return Response(serializers.BookSerializer(book[0]).data)









share|improve this question

























  • This is too broad. You need to show your models, and what you tried.

    – Daniel Roseman
    Nov 19 '18 at 9:46











  • I am very new in the Django drf programming, can you please help me with the problem? Edited the question and added the models.

    – kkr
    Nov 19 '18 at 10:09











  • Thanks but you also need to show how you are trying to set the values.

    – Daniel Roseman
    Nov 19 '18 at 10:11











  • Can you please help me with solution

    – kkr
    Nov 19 '18 at 11:08














0












0








0








How can I set values on to a ManyToManyField which specifies an intermediary model?



In the models.py



class BookUser(models.Model):
email = models.EmailField()

class Book(models.Model):
author_id= models.CharField(max_length=255)
send_to = models.ManyToManyField(BookUser, through='BookUserRelationship')
book_id = models.AutoField(primary_key=True)
file_size = models.CharField(null=True)

class BookUserRelationship(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
user = models.ForeignKey(BookUser, on_delete=models.CASCADE)
shared_date = models.DateTimeField(auto_now_add=True,null=True,blank=True)


Tried to update in serializers.py



class BookSerializer(serializers.ModelSerializer):
send_to = BookUserSerializer(many=True, read_only=True)

class Meta():
model = Book
fields = ('book_id', 'author_id','file_size','send_to')

class BookUserSerializer(serializers.ModelSerializer):
model = BookUser
fields = ('email')


In the views.py for listing the books by passing the book_id as query params



class BookListView(generics.ListCreateAPIView):
serializer_class = serializers.BookSerializer

def get(self, request, *args, **kwargs):
user = self.request.user
book_id = self.request.query_params.get('book_id', None)
if book_id:
book = models.Book.objects.filter(book_id=book_id)
return Response(serializers.BookSerializer(book[0]).data)









share|improve this question
















How can I set values on to a ManyToManyField which specifies an intermediary model?



In the models.py



class BookUser(models.Model):
email = models.EmailField()

class Book(models.Model):
author_id= models.CharField(max_length=255)
send_to = models.ManyToManyField(BookUser, through='BookUserRelationship')
book_id = models.AutoField(primary_key=True)
file_size = models.CharField(null=True)

class BookUserRelationship(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
user = models.ForeignKey(BookUser, on_delete=models.CASCADE)
shared_date = models.DateTimeField(auto_now_add=True,null=True,blank=True)


Tried to update in serializers.py



class BookSerializer(serializers.ModelSerializer):
send_to = BookUserSerializer(many=True, read_only=True)

class Meta():
model = Book
fields = ('book_id', 'author_id','file_size','send_to')

class BookUserSerializer(serializers.ModelSerializer):
model = BookUser
fields = ('email')


In the views.py for listing the books by passing the book_id as query params



class BookListView(generics.ListCreateAPIView):
serializer_class = serializers.BookSerializer

def get(self, request, *args, **kwargs):
user = self.request.user
book_id = self.request.query_params.get('book_id', None)
if book_id:
book = models.Book.objects.filter(book_id=book_id)
return Response(serializers.BookSerializer(book[0]).data)






django django-rest-framework many-to-many






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 5:13







kkr

















asked Nov 19 '18 at 9:20









kkrkkr

206




206













  • This is too broad. You need to show your models, and what you tried.

    – Daniel Roseman
    Nov 19 '18 at 9:46











  • I am very new in the Django drf programming, can you please help me with the problem? Edited the question and added the models.

    – kkr
    Nov 19 '18 at 10:09











  • Thanks but you also need to show how you are trying to set the values.

    – Daniel Roseman
    Nov 19 '18 at 10:11











  • Can you please help me with solution

    – kkr
    Nov 19 '18 at 11:08



















  • This is too broad. You need to show your models, and what you tried.

    – Daniel Roseman
    Nov 19 '18 at 9:46











  • I am very new in the Django drf programming, can you please help me with the problem? Edited the question and added the models.

    – kkr
    Nov 19 '18 at 10:09











  • Thanks but you also need to show how you are trying to set the values.

    – Daniel Roseman
    Nov 19 '18 at 10:11











  • Can you please help me with solution

    – kkr
    Nov 19 '18 at 11:08

















This is too broad. You need to show your models, and what you tried.

– Daniel Roseman
Nov 19 '18 at 9:46





This is too broad. You need to show your models, and what you tried.

– Daniel Roseman
Nov 19 '18 at 9:46













I am very new in the Django drf programming, can you please help me with the problem? Edited the question and added the models.

– kkr
Nov 19 '18 at 10:09





I am very new in the Django drf programming, can you please help me with the problem? Edited the question and added the models.

– kkr
Nov 19 '18 at 10:09













Thanks but you also need to show how you are trying to set the values.

– Daniel Roseman
Nov 19 '18 at 10:11





Thanks but you also need to show how you are trying to set the values.

– Daniel Roseman
Nov 19 '18 at 10:11













Can you please help me with solution

– kkr
Nov 19 '18 at 11:08





Can you please help me with solution

– kkr
Nov 19 '18 at 11:08












1 Answer
1






active

oldest

votes


















0














You don't need to do anything at all. You have already set the relevant data in your for loop, by creating the BookUserRelationship instances. That is the many-to-many relationship; you should just remove the instance.send_to.set(emails) line.






share|improve this answer
























  • Thankyou, But while taking the response I am not getting it. Instead of that error 'str' object has no attribute 'values' is coming.

    – kkr
    Nov 19 '18 at 11:13











  • Where is that error happening? It doesn't seem to have anything to do with this code. Please edit the question to show the full traceback.

    – Daniel Roseman
    Nov 19 '18 at 11:15













  • Can you please have a look into this problem. The send_to field is having the value None

    – kkr
    Nov 19 '18 at 11:57











  • Can you show your BookUserSerializer?

    – Daniel Roseman
    Nov 19 '18 at 12:24











  • added BookUserSerializer also, Can you please help? Thanks in advance.

    – kkr
    Nov 20 '18 at 8:45











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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371534%2fcannot-set-values-on-a-manytomanyfield-which-specifies-an-intermediary-model%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You don't need to do anything at all. You have already set the relevant data in your for loop, by creating the BookUserRelationship instances. That is the many-to-many relationship; you should just remove the instance.send_to.set(emails) line.






share|improve this answer
























  • Thankyou, But while taking the response I am not getting it. Instead of that error 'str' object has no attribute 'values' is coming.

    – kkr
    Nov 19 '18 at 11:13











  • Where is that error happening? It doesn't seem to have anything to do with this code. Please edit the question to show the full traceback.

    – Daniel Roseman
    Nov 19 '18 at 11:15













  • Can you please have a look into this problem. The send_to field is having the value None

    – kkr
    Nov 19 '18 at 11:57











  • Can you show your BookUserSerializer?

    – Daniel Roseman
    Nov 19 '18 at 12:24











  • added BookUserSerializer also, Can you please help? Thanks in advance.

    – kkr
    Nov 20 '18 at 8:45
















0














You don't need to do anything at all. You have already set the relevant data in your for loop, by creating the BookUserRelationship instances. That is the many-to-many relationship; you should just remove the instance.send_to.set(emails) line.






share|improve this answer
























  • Thankyou, But while taking the response I am not getting it. Instead of that error 'str' object has no attribute 'values' is coming.

    – kkr
    Nov 19 '18 at 11:13











  • Where is that error happening? It doesn't seem to have anything to do with this code. Please edit the question to show the full traceback.

    – Daniel Roseman
    Nov 19 '18 at 11:15













  • Can you please have a look into this problem. The send_to field is having the value None

    – kkr
    Nov 19 '18 at 11:57











  • Can you show your BookUserSerializer?

    – Daniel Roseman
    Nov 19 '18 at 12:24











  • added BookUserSerializer also, Can you please help? Thanks in advance.

    – kkr
    Nov 20 '18 at 8:45














0












0








0







You don't need to do anything at all. You have already set the relevant data in your for loop, by creating the BookUserRelationship instances. That is the many-to-many relationship; you should just remove the instance.send_to.set(emails) line.






share|improve this answer













You don't need to do anything at all. You have already set the relevant data in your for loop, by creating the BookUserRelationship instances. That is the many-to-many relationship; you should just remove the instance.send_to.set(emails) line.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 19 '18 at 11:09









Daniel RosemanDaniel Roseman

446k41577633




446k41577633













  • Thankyou, But while taking the response I am not getting it. Instead of that error 'str' object has no attribute 'values' is coming.

    – kkr
    Nov 19 '18 at 11:13











  • Where is that error happening? It doesn't seem to have anything to do with this code. Please edit the question to show the full traceback.

    – Daniel Roseman
    Nov 19 '18 at 11:15













  • Can you please have a look into this problem. The send_to field is having the value None

    – kkr
    Nov 19 '18 at 11:57











  • Can you show your BookUserSerializer?

    – Daniel Roseman
    Nov 19 '18 at 12:24











  • added BookUserSerializer also, Can you please help? Thanks in advance.

    – kkr
    Nov 20 '18 at 8:45



















  • Thankyou, But while taking the response I am not getting it. Instead of that error 'str' object has no attribute 'values' is coming.

    – kkr
    Nov 19 '18 at 11:13











  • Where is that error happening? It doesn't seem to have anything to do with this code. Please edit the question to show the full traceback.

    – Daniel Roseman
    Nov 19 '18 at 11:15













  • Can you please have a look into this problem. The send_to field is having the value None

    – kkr
    Nov 19 '18 at 11:57











  • Can you show your BookUserSerializer?

    – Daniel Roseman
    Nov 19 '18 at 12:24











  • added BookUserSerializer also, Can you please help? Thanks in advance.

    – kkr
    Nov 20 '18 at 8:45

















Thankyou, But while taking the response I am not getting it. Instead of that error 'str' object has no attribute 'values' is coming.

– kkr
Nov 19 '18 at 11:13





Thankyou, But while taking the response I am not getting it. Instead of that error 'str' object has no attribute 'values' is coming.

– kkr
Nov 19 '18 at 11:13













Where is that error happening? It doesn't seem to have anything to do with this code. Please edit the question to show the full traceback.

– Daniel Roseman
Nov 19 '18 at 11:15







Where is that error happening? It doesn't seem to have anything to do with this code. Please edit the question to show the full traceback.

– Daniel Roseman
Nov 19 '18 at 11:15















Can you please have a look into this problem. The send_to field is having the value None

– kkr
Nov 19 '18 at 11:57





Can you please have a look into this problem. The send_to field is having the value None

– kkr
Nov 19 '18 at 11:57













Can you show your BookUserSerializer?

– Daniel Roseman
Nov 19 '18 at 12:24





Can you show your BookUserSerializer?

– Daniel Roseman
Nov 19 '18 at 12:24













added BookUserSerializer also, Can you please help? Thanks in advance.

– kkr
Nov 20 '18 at 8:45





added BookUserSerializer also, Can you please help? Thanks in advance.

– kkr
Nov 20 '18 at 8:45


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53371534%2fcannot-set-values-on-a-manytomanyfield-which-specifies-an-intermediary-model%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

How to send String Array data to Server using php in android

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Is anime1.com a legal site for watching anime?