Adding Many To many with Class view

I’m Creating Need to remeber cards table from original cards table but i can’t save them in 'need to remember card table ?

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','codesnippet'],external_plugin_resources=[(
   card_type = models.CharField(choices=CARD_TYPE,max_length=100)
   need_to_remember_cards = models.ManyToManyField('Card',related_name='need_to_remember',through='NeedToRemember')

   def  __str__(self):
      return self.frontcard

   def __str__(self):
      return self.title

class NeedToRemember(models.Model):
   user = models.ForeignKey(User,on_delete=models.CASCADE)
   card = models.ForeignKey(Card,on_delete=models.CASCADE)
   created_at = models.DateField(auto_now_add=True)

class SaveToUnknowView(LoginRequiredMixin,TemplateView):
      Button its function  TO Save A Card To Unknow Cards List That  you Need to Review It later. 
   def post(self,request,pk):
      card_id= Card.objects.get(id=pk)
      deck_id= deck_id= Deck.objects.get(id=card_id.deck_id)
      if request.method == 'POST':
         if  saved_card:
            return redirect(reverse_lazy('home'))
            messages.error(self.request,'Card Already  Saved')
            return redirect(reverse_lazy('home'))  
      return render(request,self.template_name)

the error that i got when i try to save the card

What view and template are creating a link to “/save/…/…/”?

the template

<form action="{% url 'save_to_unknown'  %}"
                  {% csrf_token %}
                  <button type="submit" class='btn btn-outline-primary'>Need To Remeber</button>

i’m really so confused about that can you tell me the right way to do this,
the right way to implement with my models and add item into many to many field?

What is your url definition for “save_to_unknown”?


The specific error here is NeedToRemember matching query does not exist.

You have the line:

which is the only place in the part of the view you’ve quoted where you’re executing a query on NeedToRemember.

That means that this part of your query:
NeedToRemember.objects.get(user=self.request.user) isn’t returning any results. You don’t, at this point in your code, have a NeedToRemember object where the user field is the self.request.user object.

The many-to-many relationship is not a part of this problem. You need to ensure that a NeedToRemember object satisfying that query exists.

Note, since NeedToRemember has a foreign key to User, it’s also possible that you end up with multiple NeedToRemember objects all relating to the same user, which would cause this query for fail for the opposite reason.

I don’t have any suggestions as to how you should fix this because it’s not clear what you’re trying to model here, and whether your Models are an appropriate representation of your application’s problem domain.

if i change the user field in NeedToRemember Model to one to one field instead of foreign key …
will that work?

That will prevent the issue of getting multiple results in the query - but that change won’t, by itself, create the NeedToRemember instance. That’s something you’ll need to do at the appropriate location in your application.

Edit - no, that’s the wrong answer - no, you can’t do that. (When I wrote the above I didn’t realize that NeedToRemember was the through table of a many-to-many relationship)

1 Like

thanks i’ll try to fix it

No - please ignore my previous reply as explained above.

okay if you have this models how can you create instances in

NeedToRemember Table ?

Getting back to the original issue - you don’t need to query or reference the through table when adding entries unless you have additional fields that you need to modify in the process.

I’m back to not understanding what you’re trying to model here.

Your models are not internally consistent.

In your Card model you have a ManyToManyField to Card - which is this model. (It’s also not appropriate to add a foreign key to self without using the special reference name self.)

However, your through table is defined with FKs to User and Card. This doesn’t match your model definition.

Also, it would probably help you if you reviewed the documentation and examples at Many-to-many relationships | Django documentation | Django

so from your answer here the main problem in my models ? i need to remove the

NeedTo Remember model and just add many to many fild in Card model

No, I’m not saying that.

I’m saying that you’ve got an inconsistency in your model definitions that needs to be resolved. How you resolve that depends upon information that I don’t have.
I don’t know what you’re trying to do here, or what this ManyToMany relationship is supposed to represent.

What two tables are supposed to be related by this ManyToMany relationship? The answer to that answers what you need to change.

What i’m trying to do here … i have Deck and inside the deck i have cards … what i want to do is to save spesfic card in list ( i named it “Need To Remember”) so if i want to delete from the list it won’t effect to original card … By the way when i do it from the admin side it works perfectly!

So is this NeedToRemember list related to a Deck and not a Card?

No each deck has cards related to it… so NeedToRemember is related to Card

Please confirm the following:

  • A Deck is a model in your system.

  • You may have many instances of Deck in your system

  • A Deck contains some instances of Card. (Modeled in Django as defining a FK in Card referring to a Deck.)

  • A Deck also has a NeedToRemember list, which is a reference to a set of Card.

If all this is true, then NeedToRemember would be a M2M between Deck and Card.

I’m sorry this not clear to me, how can i implement this ?
but i think the problem not in models because it worked in the admin