简体   繁体   English

如何使用 django 中的 ManyToMany 访问另一个 model 或表的属性

[英]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如何使用 ManyToMany 访问另一个 model 或表的属性

I need to get data through this method but it's not retrieved我需要通过此方法获取数据,但未检索到

---=> this is Models.py ---=> 这是 Models.py

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 --=> 这是 Views.py

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


----=> success.html ----=> 成功。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 %}

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.在 Participer model 中将 ManyToMany 更改为 ForeignKey,或者遍历模板中的所有相关对象。 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.从您给 model 字段的名称来看,我认为从 ManyToMany 切换到 ForeignKey 是您最好的选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过 Django 模型进行查询并在没有 ManyToMany 关系的情况下访问另一个 Django 模型? - How to query through a Django model and get access to another Django model without a ManyToMany relationship? Django-如何将外键选择限制为另一个模型中的ManyToMany字段 - Django - How to restrict Foreign Key choices to a ManyToMany field in another model 如何访问相关 ManyToMany 表的另一个字段? - How can I access another field of a related ManyToMany table? 如何访问存储在 Django 中多对多相关表中的数据? - How can I access to data stored in a ManyToMany related table in Django? Django使用ModelChoiceField显示模型的ManyToMany字段 - Django show ManyToMany field of model using ModelChoiceField 使用Through模型的ManyToMany的Django交集 - Django intersection of ManyToMany using Through model 如何检查 django 模型属性是否属于 ManyToMany 类型? - How to check if a django model attribute is of type ManyToMany? 如何连接 Django Model 与多对多关系? - How to Connect a Django Model with ManyToMany Relationship? manytomany model的django模板信息如何显示 - How to display in the django template information of a manytomany model 如何使用 Django 中的 ManytoMany / ForeignKey 更新 model - How to update a model with ManytoMany / ForeignKey in Django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM