tags:

views:

31

answers:

2

Hello, I have models for eg like this.

class Mp3(models.Model):
    title=models.CharField(max_length=30)
    artist=models.ForeignKey('Artist')

and Here is how the Artist models looks like:

class Artist(models.Model):
    name=models.CharField(max_length=100,default="Unknown")

I have created Artist with id 1. How I can create a mp3 that is assigned to this artist?(I want need it for query like this for eg.

mp3=Mp3.objects.get(id=50)
mp3.artist

)I have tried sth like this

newMp3=Mp3(title="sth",artist=1)

but I got than

ValueError: Cannot assign "1": "Mp3.artist" must be a "Artist" instance.

I understand the error but still don't know how to solve this. Thanks for any help Best Regards

A: 

The answer would be:

newMp3=Mp3(title="sth", artist=the_artist)

where 'the_artist' is an actual instance of an Artist

stevejalim
A: 
artist = Artist.objects.get(id=1)  
newMp3 = Mp3(title="sth", artist=artist)
Seitaridis
This is the solution
Artur