I’m trying to save card from card model in another model has many to many field here is my code
models.py
class NeedToReview(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE,default=1)
cards= models.ManyToManyField("Card",related_name='review_cards')
class Deck(models.Model):
user = models.ForeignKey(User,on_delete=models.CASCADE)
topic = models.CharField(max_length=55,null=False,blank=False)
description= models.CharField(max_length=300,null=True, blank=True)
pub_date=models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.topic
class Card(models.Model):
deck= models.ForeignKey(Deck,on_delete=models.CASCADE)
frontcard= models.CharField(max_length=250)
# backcard = RichTextField(blank=True,null=True)
backcard= RichTextUploadingField(blank=True,null=True ,extra_plugins=['resize'],external_plugin_resources=[(
'resize',
'/static/ckeditor_plugins/resize/',
'plugin.js'
)])
card_type = models.CharField(choices=CARD_TYPE,max_length=100)
pub_date=models.DateTimeField(auto_now_add=True)
# need_to_review=models.ManyToManyField(NeedToReview,related_name='need_to_review')
def __str__(self):
return self.frontcard
views.py
class SaveToUnknowView(LoginRequiredMixin,TemplateView):
template_name='base/deck_ifo.html'
def post(self,request,pk):
#x= UnknowCards.objects.all()
card_id= get_object_or_404(Card,id=pk)
if request.method == 'POST':
# Save To Unknow Card
saved_card=NeedToReview.objects.get_or_create(user=self.request.user)
saved_card.add(cards=card_id)
return redirect(reverse('home'))
return render(request,self.template_name)
Are you getting an error message?
What is happening in your view?
What response are you seeing in your browser?
in this code the error is ‘tuple’ object has no attribute ‘add’
but i tried tried many ways but it dosen’t work
i just write the logic in the view function to add the card object in the other model!!
When posting requests for assistance here, it’s helpful to provide as much detail as possible about any errors you are receiving. This includes posting the complete traceback from the error.
i updated the views code here
views.py
class SaveToUnknowView(LoginRequiredMixin,TemplateView):
template_name='base/deck_ifo.html'
def post(self,request,pk):
#x= UnknowCards.objects.all()
card_id= get_object_or_404(Card,id=pk)
if request.method == 'POST':
# Save To Unknow Card
saved_card=NeedToReview.objects.create(user=self.request.user,cards=card_id)
saved_card.save()
return redirect(reverse('home'))
return render(request,self.template_name)
and i got this error
‘Direct assignment to the forward side of a many-to-many set is prohibited. Use cards.set() instead’
As the error says, you can’t directly assign to the cards
attribute, since a ManyToMany assignments are actually instances created in the “join table” created to manage those relationships. You were more correct in your earlier attempt where you used .add
to add that entry.
KenWhitesell:
your earlier attem
i know but it also didn’t work it gives me an error and , can you tell me how to make it work i really need help
But you also made other changes.
You need to use the add
method with the other changes you’ve made. Your error earlier was not caused by the add
.
okay i got back to the previous code can you tell me where exactly the bug to fix it and thanks so much for your time )
Please post your current view, and the complete traceback you’re receiving.
class SaveToUnknowView(LoginRequiredMixin,TemplateView):
template_name='base/deck_ifo.html'
def post(self,request,pk):
#x= UnknowCards.objects.all()
card_id= get_object_or_404(Card,id=pk)
if request.method == 'POST':
# Save To Unknow Card
saved_card=NeedToReview.objects.create(user=self.request.user)
saved_card.add(cards=ycard_id)
return redirect(reverse('home'))
return render(request,self.template_name)
and the traceback
Review the docs and examples at Many-to-many relationships | Django documentation | Django to see how to use add
in a many-to-many relationship.
1 Like
i fount the solution and finally it works here it’s
template_name='base/deck_ifo.html'
def post(self,request,pk):
card_id= Card.objects.get(id=pk)
if request.method == 'POST':
print(card_id)
# Save To Unknow Cards
saved_card=NeedToReview.objects.get(user=self.request.user).cards.add(card_id)
return redirect(reverse('home'))
return render(request,self.template_name)
thanks for helping me