Reputation:
How do I go by showing only user files that they upload (not all) I'm using python 2.7 and django 1.7 with django allauth. Below is my model. If anyone can point me to the right direction thank you.
models.py
def hashed_uploads_dirs(instance, filename):
return os.path.join(instance.md5, filename)
class File(models.Model):
f = models.FileField(upload_to='.')
md5 = models.CharField(max_length=32)
created_at = models.DateTimeField(auto_now_add=True)
def was_published_recently(self):
return self.created_at >= timezone.now() - datetime.timedelta(days=1)
was_published_recently.admin_order_field = 'created_at'
was_published_recently.boolean = True
was_published_recently.short_description = 'Published recently?'
def __unicode__(self):
return self.f.name
@models.permalink
def get_absolute_url(self):
return ('file-add', )
def save(self, *args, **kwargs):
self.slug = self.file.name
super(File, self).save(*args, **kwargs)
def delete(self, *args, **kwargs):
"""delete -- Remove to leave file."""
self.file.delete(False)
super(File, self).delete(*args, **kwargs)
Upvotes: 0
Views: 110
Reputation: 752
Create a new field in File:user = models.ForeignKey(User)
Then you can do
file.user = request.user
file.save()
in the function that handles the file upload
Upvotes: 1
Reputation: 3184
You need to modify your file class to store a foreign key to a user. It should look like
...
from django.contrib.auth.models import User
class File(models.Model):
f = models.FileField(upload_to='.')
md5 = models.CharField(max_length=32)
created_at = models.DateTimeField(auto_now_add=True)
user = models.ForeignKey(User, related_name='uploaded_files')
...
When you upload the file, you'll also have to set the user before saving the file. Then when you have an instance of User you can get all the uploaded files with user.uploaded_files.
Upvotes: 2