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

HBMCS

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')
Willem Van Onsem

You can .filter(…) [Django-doc] with:

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

the .distinct() call [Django-doc] 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:

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', realted_name='venues'
    )

then fetching the Events is even simpler:

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

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

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I update a model field based on updating another model's field using Signals?

How to get a field value from one model to another model

Validating a Django model field based on another field's value?

Django - How to update a field for a model on another model's creation?

Get not all values but specific values of field in another model with one to one field

Filtering one model based on another model's field

Generating unique id in a model with prefix of field value of another model

Django: get the id from on model to add it to another

Backbone Collection get Model by it's ID

Get description of Id in another table in a Yii model

How to get a model's ID in Ember?

easiest way to check if an item is in `has_many` field of another model?

How do I define my model if I need an auto-incrementing ID but want to check for duplicates on another field?

How to get the values in django model field?

How to compute field with values from many2many connected model?

Laravel , geeting comma separated values as ID for another model

Check if a field's value is in another field

How to get model data to appear as a field in another model's response

Django: Accessing a model's field through a another model

Django filter data using field in another model connected via foreign key

MongoDB aggregate get distinct values of field and output list of another field

Check if a field is empty and get value from another columm

Check if a field is empty and get value from another column

How to use foreign key field's attribute for another model field

check if array values contain another array's values and get the relevant key

Elasticsearch get sum of values from a field based on another field

How to get multiple values from a model field into a form choice field?

Convert the model's fields as Dictionary in another model's field

Get mean values per sample, arranged by another column of ID's