0

This below are the classes in my models.py.

class Category(models.Model):

TRAVEL = 'TR'
WEDDING = 'WE'
PORTRAITURE = 'PR'

CATEGORIES = (
    ('TRAVEL', 'travel'),
    ('WEDDING', 'wedding'),
    ('PORTRAITURE', 'portraiture'),
)

category = models.CharField(
    max_length=32,
    choices=CATEGORIES,
    default='PORTRAITURE',
)

class Image(models.Model):

image = models.ImageField((""), upload_to='images/', height_field=None, width_field=None, max_length=None, blank=True)

image_name = models.CharField(max_length=60)

image_description = models.TextField()

location = models.ForeignKey(Location, null=True)

category = models.ForeignKey(Category, default='PORTRAITURE')

pub_date = models.DateTimeField(default=datetime.now, blank=True)

tags = models.ManyToManyField(tags)

Ignore the part below.

Has two optional arguments for validation, max_length and allow_empty_file. If provided, these ensure that the file name is at most the given length, and that validation will succeed even if the file content is empty.

To learn more about the UploadedFile object, see the file uploads documentation.

When you use a FileField in a form, you must also remember to bind the file data to the form.

The max_length error refers to the length of the filename. In the error message for that key, %(max)d will be replaced with the maximum filename length and %(length)d will be replaced with the current filename length

Wendy
  • 1
  • 1

1 Answers1

0

When you create a model, Django will create by default an id = models.AutoField(primary_key=True) auto-incrementing primary key field for it. This key will be used for model-relationships. So in the Image model's category field you have to pass an integer id value of a Category for the default value, e.g. if PORTRAITURE's id is 3 then:

category = models.ForeignKey(Category, default=3)

But since you have a fixed list of categories you don't have to create a separate model, just them in the Image model like this:

class Image(models.Model):
    TRAVEL = 'TR'
    WEDDING = 'WE'
    PORTRAITURE = 'PR'

    CATEGORIES = (
        (TRAVEL, 'travel'),
        (WEDDING, 'wedding'),
        (PORTRAITURE, 'portraiture'),
    )

    image = models.ImageField(upload_to='images/', height_field=None, width_field=None, max_length=None, blank=True)
    image_name = models.CharField(max_length=60)
    image_description = models.TextField()
    location = models.ForeignKey(Location, null=True)
    category = models.CharField(
        max_length=2,
        choices=CATEGORIES,
        default=PORTRAITURE,
    )
    pub_date = models.DateTimeField(default=datetime.now, blank=True)
    tags = models.ManyToManyField(tags)

Note that I have also removed some unnecessary quotation marks.

Dauros
  • 9,294
  • 2
  • 20
  • 28