gpt4 book ai didi

python - Django 复杂查询与过滤器连接表

转载 作者:太空宇宙 更新时间:2023-11-03 17:19:56 24 4
gpt4 key购买 nike

我需要一些指导。我正在观看所有 Django 视频并阅读有关模型和查询的说明,但我对联接缺乏一些了解。

以下是我的模型系列。我正在尝试编写一个查询,该查询返回与给定单位的“指挥官”职位相关联的成员名称的名称。简而言之,我正在努力寻找一名部队指挥官。

但是,要到达那里,我需要桥接 Member->MemberUnit->ClassPosition->Position。

这是我所拥有的:

class Member(models.Model):
id = models.AutoField(primary_key=True)
member_id = models.CharField(unique=True, max_length=30)
first_name = models.CharField(max_length=30)
middle_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
email = models.CharField(max_length=50)
home_phone = PhoneNumberField(blank=True)
cell_number = PhoneNumberField(blank=True)
fax_number = PhoneNumberField(blank=True)
Cell_phone = models.CharField(max_length=20)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
User = models.ForeignKey(User, blank=True, null=True)

def __str__(self): # __unicode__ on Python 2
return self.first_name + " " + self.last_name

class Position(models.Model):
Position_name = models.CharField(max_length=30, unique=True)
Position_description = models.CharField(max_length=5000)
Max_count = models.IntegerField()

def __str__(self):
return self.Position_name

class ClassPosition(models.Model):
unit_class = models.ForeignKey(Class)
unit_position = models.ForeignKey(Position)
Date_effective = models.DateField()

def __str__(self):
return "Class: " + self.unit_class.class_name + "; Position: " + self.unit_position.Position_name

class MemberUnit(models.Model):
Member = models.ForeignKey(Member)
Unit = models.ForeignKey(Unit)
Position = models.ForeignKey(ClassPosition)

def __str__(self):
return self.Unit.Unit_name + " " + self.Position.unit_position.Position_name + " ( " + self.Member.first_name + " " + self.Member.last_name + " )"

class Unit (models.Model):
HType = (
('HQ', 'IFT Headquarters'),
('JTF', 'Joint Task Force'),
('GEO', 'Geographic Theater'),
('SHIP', 'Federation Vessel Chapter')
)
Unit_name = models.CharField(max_length=30)
Hull_number = models.CharField(max_length=30)
Hull_type = models.CharField(max_length=4, choices=HType)
Parent_unit = models.ForeignKey("self", blank=True, null=True)
Class = models.ForeignKey(Class)
email = models.CharField(max_length=50, default='')
Member = models.ManyToManyField(Member, through='MemberUnit')
Address = models.CharField(max_length=500, default='')
City = models.CharField(max_length=50, default='')
State = models.CharField(max_length=2, default='')
Zip = models.IntegerField()

def __str__(self):
return self.Unit_name

class Class(models.Model):
class_type = models.CharField(max_length=30)
class_name = models.CharField(max_length=20, unique=True)
class_description = models.CharField(max_length=5000)

def __str__(self):
return self.class_name

如果我可以获得此类查询的示例,我相信我可以使用此示例来进行类似的查询。如果我能为此获得任何帮助,我将不胜感激。

谢谢。

最佳答案

我想这可能就是您正在寻找的情况。尝试这个;未测试;

unit_object = unit object which you already have;
member_unit = MemberUnit.objects.filter(unit=unit_object, Position__unit_position=Position.objects.get(Position_name='Commanding Officer'))

这里你可以做一个for循环并获取所有成员的名字;

for members in member_unit:
print members.Member.first_name

或者您可以获取成员名称列表,例如;

member_names = MemberUnit.objects.filter(unit=unit_object, Position__unit_position=Position.objects.get(Position_name='Commanding Officer')).values_list('Member__first_name', flat=True)

关于python - Django 复杂查询与过滤器连接表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33273105/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com