I'm trying to create a simple upload center with accounting .
I have a User
and a File
model
like this :
class File(models.Model):
name = models.CharField(max_length=100)
path = models.CharField(max_length=255)
short_discripton = models.TextField(max_length=500, null=False)
upload_date = models.DateTimeField("Uploaded Date", auto_now_add=True)
def upload_date(self):
return timezone.datetime.now()
def __unicode__(self):
return self.name
class User(models.Model):
username = models.CharField(max_length=25, null=False, blank=False)
password = models.CharField(max_length=255, null=False, blank=False)
user_path = models.CharField(max_length=255, null=False, blank=False)
first_name = models.CharField(max_length=50, null=False, blank=False)
last_name = models.CharField(max_length=60, null=True, blank=True)
creation_date = models.DateTimeField("Register Date", auto_now_add=True)
files = models.ForeignKey(File, null=True, blank=True)
def __unicode__(self):
return self.userna
me
But when i try to retrieve the files using :
all_user_files = File.objects.get(pk=user.id)
It only returns one of the files though there is more files in the database(checking admin panel).
And the other way when i use :
user_all_files = user.files_set.all()
It will raise the error :
'User' object has no attribute 'files_set'
I'm really confused with that.Is there any other way ?
You need delete files = models.ForeignKey(File, null=True, blank=True)
And add to File (model) user= models.ForeignKey(User)
example here docs.djangoproject.com/en/1.8/topics/db/examples/many_to_one