Assign static link for each of model instances.

Hi everyone.

I need a help on how to treat default images for some default categories.
Let’s say I have 10 default category instances of model:

class Category(MPTTModel):
    """ Category model. 
    Represents a category where money have been spent/earned."""

    name = models.CharField(max_length=54, unique=True)
    image = models.ImageField(
        upload_to=category_image_path,
        blank=True,
        null=True
    )

I have 10 different images for each category in my static files.
I want to assign specific image for specific category.

I had achieved this before by adding images to media and passed links in ImageField() but in this case it has to be stored in media which designed to store uploaded dynamic files if I understand it right.
I was trying to use Python function() which parses category name and returns link which I use in templates (for this purpose I had to create list of dicts with instance object and static image link) but that is not the best approch as I may guess.
Then I was trying to annotate() each category instance with link but can’t find a way to use python function() inside annotate(). And looks like it won’t work at all as instance can’t be annotated with external string object.

Is there is a right approach for this goal? Appreciate any advices.

I don’t think I’m quite following what you’re trying to describe here, so I’m not sure if this is the information that you are looking for - but both the FileField and ImageField have a url attribute that is used to get the reference url for a media file.

Also see the docs and example at Managing files | Django documentation | Django

If this doesn’t address your root issue, please provide more details about what the issue is that you’re trying to solve.

Sorry for unclear explanation of an issue.
Will try to simplify the question :

I have 10 model instances prepopulated in DB on initializing.
How to assign an individual static images for each of 10 intances? Is there is a way to store link to a STATIC files in a model? Not MEDIA files as this images are statics not meant to be changed.

What is the best approach for such a goal?

Media does not necessarily mean “meant to be changed”. It means, “able to be added later”.

If these are truly static images that you will have at the time the system is being deployed, then they belong in your static folder, and you don’t create FileField instances for them. You would simply identify the static url for them in a char field, if you need to reference them.

Ok, then I have to add another CharField() just to store some data for few objects. Seems like the only solution. But still wondering why I can just annotate() an instance with any random string using ExpressionWrapper but can’t use a python function which return string as well.

Thank you for help!

You’re not adding another char field, you’re replacing the file field with the char field.