Closed koddr closed 6 years ago
In my case, I just call to ForeignKey model from DB in generator
. But I don't know, it's right way?
Like this:
@register.generator('example:example:image_800x800')
class TextOverlayImage(ImageSpec):
format = 'JPEG'
options = {'quality': 100}
@property
def processors(self):
model, field_name = get_field_info(self.source)
# Call to DB
info = ExampleSecond.objects.filter(id=model.id)
return [
ResizeToFill(800, 800),
TextOverlayProcessor(text=info.name)
]
Your question is not related to ImageKit
. You already found out how to get the model instance with get_field_info
which is specific to ImageKit
. After that you are using pure Django ORM functionality which is not related to ImageKit
at all.
In your case model
is not a model but actually an instance of the model. Also as the exception i saying 'RelatedManager' object does not support indexing
. This is because you can't do:
instance = Example.objects.get(pk=some_pk)
info = instance.info[0]
You can change that to:
info = instance.info.all()[0]
If you will have only one info
then you can replace it with:
info = instance.info.get()
or directly change your model to represent that you have only one info by using OneToOneField
:
example = models.OneToOneFIeld(Example, related_name='info', ...)
and then accesing it directly with:
info = instance.info
You can read more in the Django documentation https://docs.djangoproject.com/en/1.11/ref/models/relations/
Hello. Little question is "how to get value from
ForeignKey
field to the custom ImageKit processor?"My
models.py
:My custom processor:
But how to get
name
field value fromExampleSecond
model for place toTextOverlayProcessor
attribute?I will be glad to explanatory comments and/or use cases.