I’ve looked through many of the other posts, but I’m not quite finding the answer I need.
I have two models:
class Album_Section_Entry(models.Model):
album_id = ForeignKey(Album, on_delete=models.CASCADE, null=True, blank=True)
album_section_id = ForeignKey(Album_Section, on_delete=models.CASCADE, null=True, blank=True)
label_number = models.CharField(max_length=10, null=False, blank=False, default='0001')
entry_description = models.CharField(max_length=100, null=False, blank=False, default='Unspecified')
date_created = models.DateField(auto_now_add=True)
notes = models.TextField()
dropbox_shared_link = models.URLField(null=False, blank=True, default='', max_length=500)
dropbox_filename = models.CharField(max_length=250, null=False, blank=False, default='')
base_filename = models.CharField(max_length=250, null=False, blank=False, default='')
sort_position = models.IntegerField(null=False, blank=False, default=100000)
id = models.UUIDField(default=uuid.uuid4, editable=False,
unique=True, primary_key=True)
and
class Album_Section_Entry_Likes(models.Model):
album_section_entry_id = ForeignKey(Album_Section_Entry, on_delete=models.CASCADE, null=True, blank=True)
user = models.ForeignKey(User, on_delete=models.CASCADE, null=True, blank=True)
liked_date = models.DateField(auto_now_add=True)
id = models.UUIDField(default=uuid.uuid4, editable=False,
unique=True, primary_key=True)
What I am trying to do is to produce a list of Album_Section_Entry objects while also showing any related Album_Section_Entry_Likes as a left join. The outer query should return all albums for the specific id and include any related entries from the “likes” table.
I’m pretty sure I could do this with straight SQL, as this query works..
select *
from albums_album_section_entry
LEFT JOIN albums_album_section_entry_likes on album_section_entry_id_id = albums_album_section_entry.id
where albums_album_section_entry.id='abcde12345' and user_id = 1;
however, I’m trying to get this working using the ORM.
Thanks!