django - How to save ForeignKey in queryset? 'NoneType' object has no attribute 'myfield' -


how save foreignkey in queryset?

class bookfile(models.model):    myfile = models.filefield(upload_to="mybooks")  class mybook(models.model):     number = models.charfield(max_length=10)     file = models.foreignkey(bookfile) 

my try in views:

bookfile = form.cleaned_data.get('bookfile') mybook = mybook.objects.create(number="5555") mybook.file.myfile = bookfile mybook.save() 

error:

'nonetype' object has no attribute 'myfile' 

what in bookfile ? object or id ? if it's id, try:

bookfile = form.cleaned_data.get('bookfile') mybook = mybook.objects.create(number="5555")  bookfileobject = bookfile.objects.create() bookfileobject.myfile = bookfile bookfileobject.save() mybook.file = bookfileobject mybook.save() 

Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -