英文:
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
详细信息?
目前我尝试了以下代码,但返回了 noniterable
和 non 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(…)</code> <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 <sup>[Django-doc]</sup>
prevents returning the same Venue
multiple times.
For the Venues
s you can access the .dateevent_set.all()
to retrieve all the related dateEvent
s.
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 Event
s 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论