I have an object Prodcut, User, Lesson, Producs_Access [this is the task ive been given]
Products have foreignkey for Lessons
Products_Access has manytomany field for user and foreign key to user
and I need to write an api that "returns lists of lessons, that are assigned to Products that User is allowed to visit" [visit permissions should be managed in products_access[
the only way to make such a dictionary/list that I know is to make a json string, but how do I turn it into a response object? If that's not the solution, what would you recommend?
class Lesson(models.Model):
name = models.CharField(max_length=200, default=None, null=True, blank=False)
link_to_video = models.CharField(max_length=700, default=None, blank=True) #I assumed not all lessons need videos
view_length_sec = models.IntegerField(default=0, null=True, blank=False)
viewed = models.ManyToManyField(User, null=True, blank=False, related_name='lessons')
class Product(models.Model):
name = models.CharField(max_length=200, default=None, null=True, blank=False) #blank=False means it's required
owner = models.ForeignKey(User, null=True, blank=False, related_name='products', on_delete=models.SET_NULL)
lesson = models.ForeignKey(Lesson, null=True, blank=True, related_name='products', on_delete=models.SET_NULL)
class Products_Access(models.Model):
products = models.ManyToManyField(Product, null=True, blank=False, related_name='allowed_products')
user = models.ForeignKey(User, null=True, blank=False, on_delete=models.SET_NULL, related_name='allowed_products')```
also, one little note: there are some other requirements in my task like "list of lessons for spicific product, that is accessable by the user", thats why I think making a dictionary/list and transforming it into a json string is the solution.