Django复杂QuerySet ManyToManyField与其他ManyToManyField

问题描述 投票:0回答:1

对不起这个标题,我不知道如何正确命名。我在获取与其他ManyToManyField相关的ManyToManyField的查询集时遇到问题。所以看起来像这样,有模型公司有ManyToManyField与Person和Person模型得到ManyToManyField和Position,因为它背后的逻辑是1公司可以有很多人,1个人可以有少量职位,可以雇用少数公司(这很明显我认为)。我通过这种方式获取Person in Company的查询集

{% for team in brand.team.all %}
<p>{{ team.first_name }} {{ team.last_name }}</p>

<img class="img-thumbnail" src="/media/{{ team.photo }}">
<p>{{ team.position }} </p>
<p>{{ team.about }} </p>
{% endfor %}

我得到了我想要的东西,比较这个模板看起来像这个enter image description here

但我没有得到人的位置,只有company.Position.None,我不知道如何得到这个。从文档到目前为止我知道它适用于单个ManyToManyField,但我找不到类似于我的情况的例子,我不知道我应该如何得到(人的位置)

以下是我的文件

models.朋友

from django.db import models
...

TYPES = (
        ('Startup', 'Startup'),
        ... )

CITIES = (
         ('Warszawa', 'Warszawa'),
         ... )

STACK = (
        ('PHP', 'PHP'),
        ... )

STUDENTS = (
		('No', 'No'),
		('Yes', 'Yes')
	)
STACK_ICONS = (
        ('/static/icons/stack/php.png', 'PHP'),
        ('/static/icons/stack/javascript.png', 'JavaScript'),
        ...
    )

POSITIONS = (
        ('CEO', 'Chief Executive Officer'),
        ('CTO', 'Chief Technology Officer'),
        ...
    )

# object position with relationship many to many to person

class Position(models.Model):
    position = models.CharField(max_length=50, choices=POSITIONS)

    def __str__(self):
        return self.position

# object person relation many to one (many persons to one company)

class Person(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)
    about = models.TextField(max_length=500, default=None)
    position = models.ManyToManyField('Position')
    photo = models.ImageField(blank=True, null=True, default=None)

    def __str__(self):
        return "%s %s" % (self.first_name, self.last_name)

# object company

class Company(models.Model):
    # field person with relation many to one (many persons to 1 company)
    team = models.ManyToManyField('Person')
    name = models.CharField(max_length=100, blank=False)
    technologies = models.ManyToManyField('Stack')
    website = models.TextField(max_length=150, blank=True, null=True, validators=[URLValidator()])
    ...

    def save(self, *args, **kwargs):
        self.slug = slugify(self.name)
        super(Company, self).save(*args, **kwargs)

    def publish(self):
        self.published_date = timezone.now()
        self.save()
        
    def __str__(self):
        return self.name

# object stack relation manytomany with Company

class Stack(models.Model):
    name = models.CharField(max_length=30, choices=STACK)
    icon = models.CharField(max_length=80, choices=STACK_ICONS)

    def __str__(self):
        return self.name

views.朋友

from django.shortcuts import render, get_object_or_404, redirect
...

def brands(request, slug):
    brand = get_object_or_404(Company, slug=slug)
    return render(request, 'company/comp_view.html', {'brand': brand})

def stacks(request):
    stack = get_object_or_404(Stack)
    return render(request, 'company/comp_view.html', {'stack': stack})

def positions(request):
    position = get_object_or_404(Position)
    return render(request, 'company/comp_view.html', {'position': position})

...

comp_view.html

{% extends 'company/base.html' %}

    <div class="col col-md-1"></div>
    <div class="col col-md-5 card-section">
        <div class="team ">
            <div class="card-title">
                <span>Team</span>
            </div>
            <div class="row text-center">
                <div class="col col-md-4">
                    {% for team in brand.team.all %}
                    <p>{{ team.first_name }} {{ team.last_name }}</p>                        
                    <img class="img-thumbnail" src="/media/{{ team.photo }}">
                    <p>{{ team.position }}</p>
                    <p>{{ team.about }} </p>
                </div>
                {% endfor %}
            </div>
        </div>

{% endblock %}
django many-to-many django-orm
1个回答
1
投票

没有“单个ManyToManyField”这样的东西。你有一个m2m关系,你需要像对团队成员一样迭代它。

{% for position in team.position.all %}
  {{ position.name }}
{% endfor %}
© www.soinside.com 2019 - 2024. All rights reserved.