jazzband / django-constance

Dynamic Django settings.
https://django-constance.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.66k stars 306 forks source link

Setting image_field value with CLI (Question) #511

Closed ShirinovAdil closed 1 year ago

ShirinovAdil commented 1 year ago

HI! I have a setting of an image_field type specified as in docs, is it somehow possible to set a value for it using the management command? I'm trying to make a new management command based on _set_constance_value but not sure what format should the image be and if it's possible at all.

sergei-iurchenko commented 1 year ago

ImageField is TextField. So you can set image path as cli param.

ShirinovAdil commented 1 year ago

I run constance set HERO_IMAGE 'path/to/file/' and get this error CommandError: No file was submitted. Check the encoding type on the form.

My guess is it actually requires encoded image in some format since it works like Django forms

islam-kamel commented 1 year ago

I run constance set HERO_IMAGE 'path/to/file/' and get this error CommandError: No file was submitted. Check the encoding type on the form.

My guess is it actually requires encoded image in some format since it works like Django forms

Can you attach your config for constance in a settings.py file ?

sergei-iurchenko commented 1 year ago

docs

./manage.py shell

`In [1]: from constance import config

In [2]: config.LOGO_IMAGE Out[2]: 'Screenshot from 2023-02-08 22-17-31.png'

In [3]:

In [3]: setattr(config, 'LOGO_IMAGE', '/path/file.txt')

In [4]:

In [4]: config.LOGO_IMAGE Out[4]: '/path/file.txt' `

ShirinovAdil commented 1 year ago

I run constance set HERO_IMAGE 'path/to/file/' and get this error CommandError: No file was submitted. Check the encoding type on the form. My guess is it actually requires encoded image in some format since it works like Django forms

Can you attach your config for constance in a settings.py file ?

CONSTANCE_BACKEND = 'constance.backends.database.DatabaseBackend'
CONSTANCE_DBS = "default"
CONSTANCE_SUPERUSER_ONLY = False
CONSTANCE_ADDITIONAL_FIELDS = {
    'image_field': ['django.forms.ImageField', {'required': False}]
}
CONSTANCE_CONFIG = {
    'POWERED_BY_LOGO': ('', 'description', 'image_field'),
    'HERO_IMAGE': ('', 'description.', 'image_field'),
    'SIDEBAR_LOGO': ('', 'description', 'image_field'),
}
ShirinovAdil commented 1 year ago

docs

./manage.py shell

`In [1]: from constance import config

In [2]: config.LOGO_IMAGE Out[2]: 'Screenshot from 2023-02-08 22-17-31.png'

In [3]:

In [3]: setattr(config, 'LOGO_IMAGE', '/path/file.txt')

In [4]:

In [4]: config.LOGO_IMAGE Out[4]: '/path/file.txt' `

you're using setattr thus default constance set command is not suitable for that?

sergei-iurchenko commented 1 year ago

constance setworks through django from which requires file to exist in memory

ShirinovAdil commented 1 year ago

Ok, that makes sense. All my questions are answered, thanks.