Issue
here are models.py file
class album(TimeStampedModel):
#album_artist = models.ForeignKey(artist, on_delete=models.CASCADE)
album_name = models.CharField(max_length=100, default="New Album")
#released_at = models.DateTimeField(blank=False, null=False)
#price = models.DecimalField(
# max_digits=6, decimal_places=2, blank=False, null=False)
#is_approved = models.BooleanField(
# default=False, blank=False)
#def __str__(self):
# return self.album_name
class song(models.Model):
name = models.CharField(max_length=100,blank=True,null=False)
album = models.ForeignKey(album, on_delete=models.CASCADE,default=None)
# image = models.ImageField(upload_to='images/', blank = False)
# thumb = ProcessedImageField(upload_to = 'thumbs/', format='JPEG')
# audio = models.FileField(
# upload_to='audios/', validators=[FileExtensionValidator(['mp3', 'wav'])])
# def __str__(self):
# return self.name
I want to make the default value of the song name equal to the album name which i choose by the album Foreignkey. (in the admin panel page) any help ?
Solution
Provided the song name is set to blank=True
. You can create a pre_save signal to set the name.
...
from django.db.models.signals import pre_save
from django.dispatch import receiver
....
@receiver(pre_save, sender=Song)
def song_pre_save(sender, instance, **kwargs):
if instance.name is None:
album = Album.object.get(pk=instance.album)
instance.name = album.album_name
You can read more on the Django website
Answered By - theeomm
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.