15

I have a model

class Anh_chi_tiet(models.Model):
    du_an       = models.ForeignKey(Du_an)
    title       = models.CharField(max_length=120)
    url         = models.ImageField(upload_to='images', height_field='50', width_field='100')
    url_detail  = models.ImageField(upload_to='images', height_field='200', width_field='400')

I always receive this error:

'Anh_chi_tiet' object has no attribute '100'

It seems Django require both the fields in the same table Anh_chi_tiet to save height_field and width_field.

How can I directly set these values without append new fields in my table?

1

2 Answers 2

27

height_field represents a property of your model which is used to store the height.

class Anh_chi_tiet(models.Model):

  url_height=models.PositiveIntegerField()
  url_width=models.PositiveIntegerField()
  url = models.ImageField(upload_to='images', height_field='url_height', width_field='url_width')

Notice height_field='url_height'

Sign up to request clarification or add additional context in comments.

1 Comment

Are there any way to set height_field and width_field directly without create url_height and url_width fields?
15

I think you are misunderstanding what the height_field and width_field attributes are for.

The height_field isn't used to change the height. It's used to record what the height already is.

This way you can do something like

<img src="..." height={{my_instance.my_height_field}} /> 

without having to read the file.

If you are trying to resize all images uploaded via an ImageField, check here: Image resizing with django?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.