Tuesday, June 10, 2014

Django Upload Models

import os
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
from django.forms import ModelForm
from exmaple.main_app.models import Category

def get_image_path(instance, filename):
    return os.path.join('../media/UploadImages', instance.user.username, filename)

class UploadImageFile(models.Model):
    name = models.CharField(max_length=200)
    user = models.ForeignKey(User)
    # image_file = models.FileField( upload_to=get_image_path)
    image_file = models.ImageField( upload_to=get_image_path)

class Image(models.Model):
    category = models.ManyToManyField(Category, blank=True)
    image_location = models.URLField(max_length=500, blank=True)
    midsize_image_location = models.URLField(max_length=500, blank=True)
    thumbnail_image_location = models.URLField(max_length=500, blank=True)
    title = models.CharField(max_length=100, blank=True)
    description = models.CharField(max_length=500, blank=True)
    enabled = models.BooleanField(default=True)

    def __str__(self):
        return '%s %s, %s, %s, %s' % (self.category, self.image_location, self.thumbnail_image_location, self.title, self.description)

class ImageForm(ModelForm):
    class Meta:
        model = Image

No comments:

Post a Comment