Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7827487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T09:53:40+00:00 2026-06-02T09:53:40+00:00

I have 3 models. class Picture(models.Model) name = models.CharField(max_length=255) image_field = models.ImageField(upload_to=foo/) slug =

  • 0

I have 3 models.

class Picture(models.Model)
  name = models.CharField(max_length=255)
  image_field = models.ImageField(upload_to="foo/")
  slug = models.SlugField()
  [...]


class Size(models.Model):
  name = models.CharField(max_length=255)
  width = models.IntegerField()
  height = models.IntegerField()
  crop = models.BooleanField(default=True)
  upscale = models.BooleanField(default=False)

  def __unicode__(self):
    return self.name

class Cache(models.Model):
  size = models.ForeignKey('Size')
  picture = models.ForeignKey('Picture')
  image_field = models.ImageField(upload_to="picture/resize/")

I want to use them as follows: First generate Picture objects. Then create Size objects. For every Size and Picture a Cache object should be generated when needed.

My problem is that I don’t know where to put the code. It should be something like (pseudocode):

def get_cached_picture(Picture,Size):
  try:
    cacheObj = Cache.objects.get(picture=Picture, size=Size):
    [.. use cacheObj  ..]
  except Cache.DoesNotExist:
    [.. resize Picture according to Size, insert into cache, use it ..]

So where can I plug this code ? I know I could do this within a view, but is there a way to embed it into the models ? Cache should never be filled in the admin, instead it should be generated whenever a certain combination between Cache and Picture is needed.

It is probably easy to do, but I’m lacking of the right keyword for google.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T09:53:42+00:00Added an answer on June 2, 2026 at 9:53 am

    Assumptions:

    1. You want to be able to load a “Representation” of an image at a certain size on the fly.
    2. Size model will store a predefined set of sizes that a Representation can have.

    I am going to refer to your Cache model as Representation because I think it makes more sense in context here.

    The entry-point of this process would obviously be a view. A request comes in and you determine you need to search “Image1.jpg” @ 800×600. The simplest approach is just to put your queries right into the view. But for reusability, it might be better to do any of the following:

    • A utility function which is then called from the view to retrieve a Representation object
    • A classmethod of your Representation model, since you are already referencing Picture and Size as foreign keys.
    • Using a custom manager on the Representation model to do something similar to the above

    Any of these will make it reusable as an app if someone else were to have their own views and needed to simply retrieve a Rep instance directly.

    While your pseudocode refers to this process being on the Picture mode, I maintain that it should probably be on the Representation model, since it contains both foreign keys and you can easily see if you have a proper rep. If not, create it.

    The view should only have to call something simple as opposed to having all of the logic:

    # as classmethod
    rep = Representation.get_cached_picture(picture, size)
    # or with a manager
    rep = Representation.objects.get_cached_picture(picture, size)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
Hay, i have a model which saves 2 images class Picture(models.Model): picture = models.ImageField(upload_to=make_filename)
I have the following in models.py: class Choices(models.Model): description = models.CharField(max_length=300) def __unicode__(self): return
I have a model like this: class Item(models.Model): code = models.CharField(max_length=200, unique=True) barcode =
Let's say i have many models like that: class ExampleModel(models.Model): photo = models.ImageField(upload_to='photos/') image
I have these models: class Model1(models.Model): ... class Model2(models.Model): m1 = models.OneToOneField(Model1, related_name='m2') ...
I have these models: class UserProfile(models.Model): user = models.OneToOneField(User) #etc class Organization(models.Model): users =
I have two models Profile and Avatar. models.py class Profile(models.Model): user = models.ForeignKey(User) profile_name
I have two models class User < ActiveRecord::Base has_many :posts searchable do text :post_titles
I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.