Home > OS >  How to refer to class instances from the class itself
How to refer to class instances from the class itself

Time:02-22

I have a class 'Scene'. From this class I can have several choices (0..n). They will be presented to the User of Scene object with a simple sentence (String). Each of the choices must point to a Scene instance (the next_scene). So I only need to associate this choice with a scene instance Id.
How can I implement that in Django Models, and/or in django model.Admin. Example of a class Scene :

class Scene(model.Models)    
    title = models.CharField(max_length=30)
    description = models.TextField()
    choices = # TODO

I have tried several solutions but get always blocked. I suspect I do not conceive it right from the beginning. Any help would appreciated.
Stéphane

CodePudding user response:

The best solution for such thing is ManyToMany relation. You can set it to the same model:

class Scene(models.Model):
    ...
    choices = models.ManyToManyField("self", blank=True)
  • Related