Django 在两个不同应用程序之间使用 ManyToManyField

Django usage of ManyToManyField between 2 different applications

假设我们有 2 个应用程序,游戏和流媒体。两个应用程序都有自己的视图、模型、模板等。在应用程序流媒体中,我们有模型 主播

有字段

游戏 = models.ManyToManyField('games.Game')

我们正在从应用程序游戏中导入模型游戏。现在在迁移之后,添加一些内容我们可以创建对象 Streamer 并将他正在玩的游戏分配给他。但是当需要通过添加到模板标签

来显示所选Streamer正在玩的所有游戏时

{{ streamer.games }}

我们正在获取模板字符串

games.Game.None

所以有些事情导致了它,我们还不知道到底是什么。我们已经为游戏、流媒体、模板、网址创建了视图,一切正常,因为我们正在获取诸如流媒体的昵称或流媒体链接到 YouTube/Twitch 等数据,但如果我们希望在我们的应用程序中允许流媒体显示分配的游戏我们应该从主播和游戏中导入视图和模型吗?或者我们应该在 app streamers/views.py 中更改任何视图并放置从应用游戏导入的游戏吗?我知道如何在具有模型和关系 ManyToMany 的单个应用程序中执行此操作,因为我已经有机会(在 Whosebug 的帮助下)执行类似的操作。但是知道我正在做项目(用于学习)并且我决定更好地分离单个应用程序但我不确定如何做到这一点。如果这两个模型都在 1 个应用程序中并且将编写视图,我可以像这样使用

    {% for game in streamer.games.all %}
    {{ games.title }}
    {% endfor %}

但这对我来说是个问题,因为它们是 2 个不同的应用程序(我读到将它们分开真的更好,就像实践一样)

streamers/models.py

from django.db import models
from games.models import Game, Genre
from shots.utils import get_unique_slug
from django.utils import timezone

...

# model for streamer
class Streamer(models.Model):
    nick = models.CharField(max_length=30)
    twitch = models.CharField(max_length=70)
    games = models.ManyToManyField('games.Game')
        ...

streamers/views.py

from django.shortcuts import render, get_object_or_404, redirect
from .models import Streamer
from django.utils import timezone
from django.contrib.auth.decorators import login_required
from .forms import StreamerForm

...

# defining page for single streamer
def streamer_detail(request, slug):
    streamer = get_object_or_404(Streamer, slug=slug)
    return render(request, 'streamers/streamer_detail.html', {'streamer': streamer})
...

streamers/templates/streamer_detail.html

{% block content %}
    <div class="post">
        {% if streamer.published_date %}
            <div class="date">
                {{ streamer.published_date }}
            </div>
        {% endif %}
        {% if user.is_authenticated %}
        <a href="{% url 'streamer_edit' slug=streamer.slug %}">Edit streamer</a>
        {% endif %}
        <h2>{{ streamer.nick }}</h2>
        <a href="https://{{streamer.youtube}}">My YouTube Channel</a>
        <a href="https://{{ streamer.twitch }}">My Twitch Channel</a>
        <p>I'm streaming on {{ streamer.platform }}</p>
        Games that I'm playing
        {{ streamer.games }}
    </div>
{% endblock %}

games/models.py

from django.db import models
from django.utils import timezone
from shots.utils import get_unique_slug

# model for game
class Game(models.Model):
    title = models.CharField(max_length=70)
    genres = models.ManyToManyField('Genre', blank=True)
        ...
# model for genre
...

games/views.py

from django.shortcuts import render, get_object_or_404, redirect
from .models import Game
from django.utils import timezone
from django.contrib.auth.decorators import login_required
from .forms import GameForm

# defining page with newest games
...

# defining page for single games
def game_detail(request, slug):
    game = get_object_or_404(Game, slug=slug)
    return render(request, 'games/game.html', {'game': game})

...

好吧,看起来我们不需要将任何内容从 1 个应用的视图导入到第二个应用的视图。要访问这些查询集,我们只需要使用正确的循环,在我的例子中,有 1 个字母太多

游戏s.title

所以当我将其更改为 game.title 时一切正常

    {% for game in streamer.games.all %}
    {{ game.title }}
    {% endfor %}