How to make a field within a class which propose all the Ids of instances from that class

27 views
Skip to first unread message

Stéphane Faure

unread,
Feb 20, 2022, 9:34:35 AM2/20/22
to Django users
Hello everybody,

I have a class Scene. I would like to add to that model a list of choices (from 0 to n choices). Each choice would consist in an integer representing the Id of one of the existing scene instances. Several choices can lead to the same scene instance.
I am not sure at all on how to implement that. Any help, or advice will be greatly appreciated.

Here is Scene model:

class Scene(models.Model): 

    title = models.CharField(max_length=30)
    description = models.TextField()
    aventure = models.ForeignKey(Aventure, on_delete=models.CASCADE)
    author = models.ForeignKey(User, on_delete=models.CASCADE)
    choices = T0 BE DONE

    def __str__(self):
    return self.title


Sebastián Bevc Costa

unread,
Feb 27, 2022, 10:33:00 AM2/27/22
to Django users
A choice to the Scene model can be done with a foreign key to self: `models.ForeignKey("self", ...)`. I'm not sure what this part means though: "Several choices can lead to the same scene instance."
Reply all
Reply to author
Forward
0 new messages