检查一个ID是否存在于另一个模型的字段中,并获取相关联的数值。

huangapple go评论75阅读模式
英文:

Check if an id is in another model's field, and get the connected values

问题

给定这两个模型:

class Event(models.Model):
    title = models.CharField(max_length=200)

class DateEvent(models.Model):
    venue = models.ForeignKey(Venue, on_delete=models.CASCADE, null=True, blank=True)
    event = models.ForeignKey('Event', on_delete=models.CASCADE)

class Venue(models.Model):
    venue_name = models.CharField(max_length=50)

如何筛选仅出现在 DateEvent 中的场馆?以及如何获取与特定场馆相关联的 Event 详细信息?

目前我尝试了以下代码,但返回了 noniterablenon exists 错误:

venues = list(Venue.objects.filter(dateevent__venue__icontains=id).values('venue_name', 'dateevent__event__id'))

注意:我修正了 DateEvent 类名的大小写,并且修正了 icontains 的使用方式。

英文:

Given these two models:

class Event(models.Model):
    title = models.CharField(max_length=200)

class dateEvent(models.Model):
    venue = models.ForeignKey(Venue, on_delete=models.CASCADE,null=True, blank=True)
    event = models.ForeignKey('Event', on_delete=models.CASCADE)

class Venue(models.Model):
    venue_name = models.CharField(max_length=50)

How can I filter only the venues that appear in a dateEvent? And how can I get the Event details linked to a specific venue?

Right now I attempted this, which returns noniterable and non exists error:

venues = list(Venue.objects.filter(dateevent__venue.icontains=id).values('venue_name', 'dateevent__event.id')

答案1

得分: 2

你可以使用以下代码进行过滤:

Venue.objects.filter(dateevent__isnull=False).prefetch_related('dateevent_set').distinct()

distinct() 调用可防止多次返回相同的 Venue

对于 Venue,你可以访问 .dateevent_set.all() 来检索所有相关的 dateEvent

由于 dateEvent 充当多对多关系,你可以通过以下方式跨越此模型:

class Event(models.Model):
    title = models.CharField(max_length=200)

class dateEvent(models.Model):
    venue = models.ForeignKey(
        Venue, on_delete=models.CASCADE, null=True, blank=True
    )
    event = models.ForeignKey('Event', on_delete=models.CASCADE)

class Venue(models.Model):
    venue_name = models.CharField(max_length=50)
    events = models.ManyToManyField(
        Event, through='dateEvent', related_name='venues'
    )

然后,获取 Event 更加简单:

qs = Venue.objects.filter(dateevent__isnull=False).prefetch_related('events').distinct()

for venue in qs:
    print(venue.venue_name)
    for event in venue.events.all():
        print(f' - {event.title}')
英文:

You can <code>.filter(&hellip;)</code>&nbsp;<sup>[Django-doc]</sup> with:

<pre><code>Venue.objects.filter(<b>dateevent__isnull=False</b>).prefetch_related('dateevent_set')<b>.distinct()</b></code></pre>

the .distinct() call&nbsp;<sup>[Django-doc]</sup>
prevents returning the same Venue multiple times.

For the Venuess you can access the .dateevent_set.all() to retrieve all the related dateEvents.

Since the dateEvent acts as a many-to-many relation, you can span one over this model with:

<pre><code>class Event(models.Model):
title = models.CharField(max_length=200)

class dateEvent(models.Model):
venue = models.ForeignKey(
Venue, on_delete=models.CASCADE, null=True, blank=True
)
event = models.ForeignKey('Event', on_delete=models.CASCADE)

class Venue(models.Model):
venue_name = models.CharField(max_length=50)
<b>events</b> = models.ManyToManyField(
Event, through='dateEvent', realted_name='venues'
)</code></pre>

then fetching the Events is even simpler:

<pre><code>qs = Venue.objects.filter(dateevent__isnull=False).prefetch_related(
'events'
).distinct()</code></pre>

you then enumerate over the .events.all() of the Venue, so:

<pre><code>for venue in qs:
print(venue.venue_name)
for event in <b>venue.events.all()</b>:
print(f' - {event.title}')</code></pre>

huangapple
  • 本文由 发表于 2023年5月28日 01:03:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定