foreign key on delete arguments 1

foreign key on delete arguments

class Artist(models.Model):
    name = models.CharField(max_length=10)

class Album(models.Model):
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)

class Song(models.Model):
    artist = models.ForeignKey(Artist, on_delete=models.CASCADE)
    album = models.ForeignKey(Album, on_delete=models.RESTRICT)

Here is what the above code is Doing:
1. Create a model called Artist.
2. Create a model called Album.
3. Create a model called Song.
4. Add a foreign key to the Album model that points to the Artist model.
5. Add a foreign key to the Song model that points to the Artist model.
6. Add a foreign key to the Song model that points to the Album model.

Similar Posts