0

I have a function which computes something like sum of data (it's not a simple sum, there is an increasing number that multiplies it every time) in database through year. It is calculated in views, I need to pass them to template. I store it in Dictionary portfolio_dict[year] += amount

{'2013': Decimal('92.96892879384746351465539182'), '2012': Decimal('71.48765907571338816005401399')}

But I need some extra data to send as well. Let's say:

date:date
amount:Decimal
year:string

I know it sounds kind of stupid to have a year and date as well. I use year as index. How do I pass these data to template/add date to my current dictionary? But now, I always had Model and I passed a list of that model instances. But now I don't need to store these data in database, so I don't want to create a model. Where do I create new class in django if I don't want it to be in database? Or should I use collections or data structures?

2
  • you need to store them somehow or no problem calculating every view Commented Oct 25, 2013 at 1:07
  • 1
    show us your view adn template Commented Oct 25, 2013 at 2:54

1 Answer 1

1

Only django.db.Model instances are stored in the database (and only if you explicitely ask for it). Else this is just plain old Python and you can create and use your own classes as you see fit.

But anyway: if all you need is a year-indexed collection of (date, amount) items, then a dict of dicts is enough:

{
   '2013': {
      'amount': Decimal('92.96892879384746351465539182'), 
      'date': datetime.date(2013, 10, 25)
      },
   # etc
}

Or if you need more than one (amount, date) per year, a dict with lists or dicts:

{
   '2013': [
      {
        'amount': Decimal('92.96892879384746351465539182'), 
        'date': datetime.date(2013, 10, 25)
      },
      {
        'amount': Decimal('29.9689287'), 
        'date': datetime.date(2013, 10, 21)
      },
    ],

   # etc
}

In fact the proper structure depends on how you're going to use the data.

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

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.