Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to access to attrbuite of another model or table using ManyToMany in django

Please, I need your help. How can I access to attribut of another model or table using ManyToMany

I need to get data through this method but it’s not retrieved

—=> this is Models.py

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

class Mission(models.Model):
        nom=models.CharField(max_length=50,null=False,blank=False)
        description=models.CharField(max_length=150,null=False,blank=False)  
        date_publier=models.DateTimeField()  

class Participer(models.Model):
    id_benevole =  models.ManyToManyField(User)
    id_mission =  models.ManyToManyField(Mission)
    est_participer = models.BooleanField(default=False)

class  User(AbstractUser):
    est_association = models.BooleanField(default=False)
    est_benevolat = models.BooleanField(default=False)
    username = models.CharField(max_length=30,unique=True)
    email = models.EmailField()
    password1 = models.CharField(max_length=20,null=True)
    password2 = models.CharField(max_length=20,null=True)

class ProfileBenevole(models.Model):
   user = models.OneToOneField(User,related_name="benevole", on_delete = models.CASCADE, 
    primary_key = True)
    photo_profile = models.ImageField( upload_to='uploads/images',null=True,blank=True)
    nomComplet = models.CharField(max_length=50,null=True)

–=> this is Views.py

def demande_participer(request):
  participers=Participer.objects.all()
return render(request,'Association/success.html', {'participers':participers},print(participers))


—-=> success.html

{% extends 'home/base.html'%}
{% block content %}{% load static %}
<div class=" mt-4 pb-5">
    <h1 style="text-align:center;">Confirmation de Participation </h1>
    <div class="container">
    <div class="card mt-5 pb-5">
       {% for parti in participers %}
      
       {{parti.mission_id.nom}}
       <br>
       {{parti.id_benevole.username}}
       <br>
       {{parti.est_participer}}
       {% endfor%}
       
     </div>
    
    <a class="btn btn-secondary mt-2 " href="{% url 'benevole' %}">Retour </a>
    </div>
</div>
{% endblock content %}

>Solution :

Since you have a ManyToMany relationship, every Participer can have multiple Missions and Users. It therefor makes no sense to get an attribute of one of the (potentially) multiple related objects.

Either change the ManyToMany to a ForeignKey in your Participer model, or loop through all related objects in your template.
Something like (I did not test this code):

{% for parti in participers %}
    {% for benevole in parti.id_benevole.all %}
        {{ benevole.username }}
    {% endfor %}
{% endfor %}

Judging by the names you give your model fields, I think switching from ManyToMany to ForeignKey is your best option here.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading