首页 > 解决方案 > Django ManyToMany 关系的详细信息

问题描述

我已经很好地阅读了有关 ManyToMany 归档的 Django 文档。尤其是这里的那一位。我很清楚可以用它做的事情。例如,如果我有一个 Category 模型和一个 Startup 模型,其中一个类别可以有许多初创公司,而一个初创公司可以属于许多类别,那么 ManyToMany 关系在这里很有用。我可以在我的模板上列出所有类别,每个类别都是可点击的,并导致属于该类别的另一个模板上的所有初创公司的列表。现在,有了这个,我想更进一步。转到特定类别的详细信息页面,这是属于它的初创公司的列表,我希望此详细信息页面中的每个项目也指向另一个详细信息页面,我可以在其中显示有关特定初创公司的更多信息。

我现在的主要问题是如何实现它。我已经处理了许多与 ManyToMany 字段及其查询相关的问题,尤其是在这里 ,但它们似乎都在讨论如何将相关对象访问到一个类别,例如在详细信息页面中,并且到此为止。我想从详细信息页面转到另一个详细信息页面。

from django.db import models

我的类别模型:

class Category(models.Model):
    name = models.Charfield(max_length=100)

    def __str__(self):
        return self.name

    def get_startups(self):
        return Startup.objects.filter(category=self)

我的创业模型:

class Startup(models.Model):
    name = models.CharField(max_length=100)
    founder_name = models.CharField(max_length=100)
    short_description = models.CharField(max_length=225)
    concept = models.TextField(help_text='how does this startup operate?')
    category = models.ManyToManyField(Category)
    website = models.URLField(max_length=225)
    logo = models.ImageField()


    def __str__(self):
        return self.name

    @property
    def logo_url(self):
        if self.logo and hasattr(self.logo, 'url'):
            return self.logo.url

在我的 views.py 文件中:

class CategoryView(ListView):
    template_name = 'myapp/startup-category.html'
    context_object_name = 'category_list'

    def get_queryset(self): 
        return Category.objects.all()


class DetailView(DetailView):
    model=Category
    template_name = 'myapp/startup-list.html'

在我的模板/myapp/startup-category 中:

{% if category_list %}
<ul>
    {% for category in category-list %}
        <li><a href="{% url 'detail' category.pk %}">{{category.name}}</a></li>
    {% endfor %}
</ul>
{% endif %}

在 templates/myapp/startup-list.html 中:

{% for startup in category.get_startups %}
    <tr>
        {% if startup.logo %}
        <td><img src="{{ startup.logo_url|default_if_none:'#' }}" style="height: 50px; width: 50px;"></td>
        {% endif %}
        <td>{{startup.name}}</td>
        <td> {{startup.short_description}}</td>
        <td>
            <button type="button" ><a href="">View startup</a>
            </button></td>
    </tr>
{% endfor %}

在 myapp.urls 中:

from django.urls import path
from . import views as core_views

path('startup_categories/', core_views.CategoryView.as_view(), name='startups'),
path('startup_categories/<int:pk>/', core_views.DetailView.as_view(), name='detail'),

这工作正常。我得到一个类别列表;当我单击一个类别时,它会将我带到一个详细信息页面,在该页面中,我以表格形式获取与该类别相关的初创公司列表。在每个启动元素上,都有一个用于查看启动详细信息的按钮。如何实现启动详细信息的视图?

先感谢您!

标签: djangodjango-templatesdjango-viewsmanytomanyfield

解决方案


定义一个类似的网址:

 path('/startup/<int:startup_id>', views.view_startup, name='view-startup'),

在 forms.py 中:(如果您没有 forms.py,请在同一个应用程序中创建它)

 from .models import Startup
 from django.forms import ModelForm

 class StartupForm(ModelForm):
      class Meta:
           model = Startup
           fields = ['name', 'founder_name'] #Put name of all the fields that you want to display in template here

在views.py

 from .forms import StartupForm
 def view_startup(request, startup_id):
     startup = get_object_or_404(Startup, pk=startup_id)
     startup_details_form = StartupForm(instance = startup)

     return render(request, "startup_details.html", {"form":startup_details_form})

在 startup_details.html

  {% for field in form %}
       {{ field.label_tag }} : {{ field.value }}
  {% endfor %}

现在在您的 startuplist.html 中,您可以简单地输入

  <button type="button" ><a href="{% url 'view-startup' startup.id %}">View startup</a>

这只是给你一个基本的想法。在 url 中使用 ID 可能不是一个好习惯。您可以在启动模型中创建另一个包含 url 的 slug 字段,然后您可以修改相关文件以实际使用 slug 而不是 ID


推荐阅读