Skip to content

Instantly share code, notes, and snippets.

@hzbd
Forked from gileno/models.py
Created April 23, 2013 17:51
Show Gist options
  • Save hzbd/5445808 to your computer and use it in GitHub Desktop.
Save hzbd/5445808 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
import os
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from django.core.files.storage import FileSystemStorage
from django.conf import settings
storage = FileSystemStorage(location=settings.PROJECT_PATH, base_url='/')
def _upload_to_file(file, filename):
user_id = file.user.id
path = os.path.join('files', 'uploads')
path = os.path.join(path, unicode(user_id))
return os.path.join(path, filename)
class Upload(models.Model):
title = models.CharField(max_length=255, verbose_name=_(u'Título'))
file = models.FileField(upload_to=_upload_to_file, verbose_name=_(u'Arquivo'), storage=storage)
user = models.ForeignKey(User, verbose_name=_(u'Usuário'), related_name='uploads')
added_on = models.DateTimeField(verbose_name=_(u'Adicionado em'), auto_now_add=True)
def __unicode__(self):
return self.title
class Meta:
ordering = ['title']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment