So, I am doing a project where there are posts of 3 categories (movies(model name="List"), music(model name="MusicList"), books(model name="BookList")) each category is a model. and each model has its own genre set model (Genre, MusicGenre, BookGenre).
There is a profile model which has - favourite_music_genre, favourite_movie_genre & favourite_book_genre.
So now in my all(homepage) function in views I want to have some codes that will compare current logged in users favourite_music_genre, favourite_movie_genre & favourite_book_genre and show only the List, MusicList & BookList of the matched genres.
For example if a users selected fav_genre is Action & Comedy. It will only show the lists which has Action & Comedy genre.
Im new to python/django. Been scratching my head for past 4/6 days. couldn't figure out the way.
In my Models.py
class MusicList(models.Model):
title = models.CharField(max_length=120)
genre = models.ManyToManyField('MusicGenre')
creator = models.ForeignKey(User,on_delete=models.SET_NULL,blank=True, null=True)
posted = models.DateTimeField(auto_now_add=True)
content = RichTextField(null=True,default=' ')
type = models.CharField(max_length=10,default="Music")
slug = models.SlugField(max_length= 300,null=True, blank = True, unique=True)
def __str__(self):
return f'{self.title}|{self.creator}'
def save(self, *args, **kwargs):
self.slug = slugify(self.title str(self.posted))
super(MusicList,self).save(*args, **kwargs)
class MusicGenre(models.Model):
name = models.CharField(max_length=150)
def __str__(self):
return self.name
class BookList(models.Model):
title = models.CharField(max_length=120)
creator = models.ForeignKey(User,on_delete=models.SET_NULL,blank=True, null=True)
genre = models.ManyToManyField('BookGenre')
posted = models.DateTimeField(auto_now_add=True)
spoiler_choices = [(False, 'No'),(True, 'Yes')]
spoiler = models.BooleanField(default=False,null=True, choices = spoiler_choices)
content = RichTextField(null=True,default=' ')
type = models.CharField(max_length=10,default="book")
slug = models.SlugField(max_length= 300,null=True, blank = True, unique=True)
def __str__(self):
return f'{self.title}|{self.creator}'
def save(self, *args, **kwargs):
self.slug = slugify(self.title str(self.posted))
super(BookList,self).save(*args, **kwargs)
class BookGenre(models.Model):
name = models.CharField(max_length=150)
def __str__(self):
return self.name
class List(models.Model):
title = models.CharField(max_length=120)
genre = models.ManyToManyField('Genre')
creator = models.ForeignKey(User,on_delete=models.SET_NULL,blank=True, null=True)
posted = models.DateTimeField(auto_now_add=True)
content = RichTextField(null=True,default=' ')
type = models.CharField(max_length=10,default="Movie")
spoiler_choices = [(False, 'No'),(True, 'Yes')]
spoiler = models.BooleanField(default=False,null=True, choices = spoiler_choices)
slug = models.SlugField(max_length= 300,null=True, blank = True, unique=True)
def __str__(self):
return f'{self.title}|{self.creator}'
def save(self, *args, **kwargs):
self.slug = slugify(self.title str(self.posted))
super(List,self).save(*args, **kwargs)
class Genre(models.Model):
name = models.CharField(max_length=150)
def __str__(self):
return self.name
class Profile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, blank=True)
bio = models.TextField(max_length=500, blank=True)
location = models.CharField(max_length=30, blank=True)
birth_date = models.DateField(null=True, blank=True)
profile_pic = models.ImageField(blank=True, default='', upload_to='profiles_pics')
fav_music_genre = models.ManyToManyField('MusicGenre')
fav_Book_genre = models.ManyToManyField('BookGenre')
fav_movie_genre = models.ManyToManyField('Genre')
def __str__(self):
return self.user.username " | bio : " self.bio
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
Profile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
CodePudding user response:
For the books, you could try something like
BookList.objects.filter(genre__id__in=current_profile.fav_Book_genre.values_list("id", flat=True))
If it works you'll be able to do the same for movies and musics