django-models

Avoiding O(n) queries with Django

I've got models like this: class PledgeItem(models.Model): title = models.CharField(...) usd_amount = models.DecimalField(...) class Pledger(models.Model): name = models.CharField(...) ... class Pledge(models.Model): pledger = models.ForeignKey(Pledger) item = models.ForeignKey(PledgeItem) usd_amount = m...

Creating a news archive in Django

Hi need some help, I looking to build a news archive in python/django, I have no idea where to start with it though. I need the view to pull out all the news articles with I have done I then need to divide them in months and years so e.g. Sept 09 Oct 09 I then need in the view to some every time a new news article is created for a new...

Is it possible to limit of object creation of a model in admin panel?

I just want to know that is it possible to limit the number of objects of a model in admin panel? It is that, for example, I have a model named 'Homepage' and in the admin panel I don't want a user can create more than one instance of Homepage. Is there a way I can do this? ...

Open Password Procted Power point file using Object model in c#

How to open password procted Power Point file using Microsoft object model. I could open simple password procted file using object model. But I don't find any property or object to set password for powerpoint object. If any one have any idea please help me. Regards vishal ...

Django Model in one to one relationship and displaying it from the admin

For the following models: class Price: cad = models.DecimalField(max_digits=8, decimal_places=2) usd = models.DecimalField(max_digits=8, decimal_places=2) class Product: name = models.CharField(max_length=255) price = models.ForeignKey(Price) For each product, it's related to one and only one Price object which will c...

Mashing and sorting 3 models in one data set

I've got three models that show some sort of activity on a website I'm making. Song, Vote and Comment. They all share a common column when which shows when a record was created. I want to show a list of activity based on all three models. In short I want to munge them together, sort by when, and scrape off the first 10 records. How I d...

Django model refactoring and migration.

I'd like to refactor a number of django apps in a way which involves moving Models from one app into another where they can be more readily reused. A number of these models have either ForeignKey relationships or M2M relationships to other models (such as User). For example: class Department(models.Model): name = models.CharField...

Django: How to access originating instance from a RelatedManager?

I would like to access the Foo instance foo within my manager method baz: foo.bar_set.baz() baz would normally take an argument of Foo type: BarManager(models.Manager): def baz(self, foo=None): if foo is None: # assume this call originates from # a RelatedManager and set `foo`. # Otherw...

django query db on every request

i have a navigation element that is determined by values in a database. no matter what view it is, i need to get these navigation objects out of the database. where in the code can i tell it to set a template variable containing all the navigation objeccts without setting it in every view? ...

What's the reason why Django has SmallIntegerField?

I'm wonder why it's provided. The field is database dependent, doesn't that make it totally unreliable to use? I want to store birth year in a model, kinda like class Person(models.Model): name = models.CharField(max_length=256) born = models.IntegerField() Of course this requires very little space, it should always be 4 "charact...

Django: How to modify a text field before showing it in admin

I have a Django Model with a text field. I would like to modify the content of the text field before it's presented to the user in Django Admin. I was expecting to see signal equivalent of post_load but it doesn't seem to exist. To be more specific: I have a text field that takes user input. In this text field there is a read more sep...

List foreign keys linking to a model

How can I display objects that link to an object via a ForeignKey in Django (specifically in the admin interface). For example, if I click on an object, I'll not only see the object but also any other object that link to it. e.g. I have a model for "Manufacturer" and another one for "Model"...the "Model" model links to "Manufacturer" v...

How do I memoize expensive calculations on Django model objects?

I have several TextField columns on my UserProfile object which contain JSON objects. I've also defined a setter/getter property for each column which encapsulates the logic for serializing and deserializing the JSON into python datastructures. The nature of this data ensures that it will be accessed many times by view and template logi...

Get records before and after current selection in Django query

It sounds like an odd one but it's a really simple idea. I'm trying to make a simple Flickr for a website I'm building. This specific problem comes when I want to show a single photo (from my Photo model) on the page but I also want to show the image before it in the stream and the image after it. If I were only sorting these streams by...

search functionality on multi-language django site

hi folks, I'm building a multi-language Django site, and I'm using django-transmeta for my model data translations. Now I'm wondering if there is a Django search app that works with multi-language models. I've played with Haystack and it works fine for single-language sites, but I can't get it to work with transmeta's metaclasses... Do...

How do I override delete() on a model and have it still work with related deletes

class Widget(models.Model): title = models.CharField(max_length=255) class WidgetFile(models.Model): widget = models.ForeignKey(Widget) def delete(): # do some custom hard drive file vodo super(WidgetFile, self).delete() ... some code to create a Widget and a WidgetFile to go with it ... some_widget_instan...

The Installer could not find Models of cms app in Django-CMS

I have installed django-cms in my hosting. But there is a problem. That's when I make syncdb, all my apps are synced, cms app is not. Although I have declared full enough in the settings. It is also not error at all. Someone help me. Thanks a lot! (1146, "Table '***.cms_page' doesn't exist") ...

In Django QuerySet, how do I check for a specific object in a ManyToMany field?

I have the following models: class Topping(models.Model): ... class Pizza(models.Model): toppings = models.ManyToManyField(Topping) I then have a topping object: cheese = Topping.objects.get(name='cheese') I then find all pizzas with the cheese topping with the following query: Pizza.objects.all().filter(toppings=cheese) ...

How to set django model field by name?

This seems like it should be dead simple, so I must be missing something. I just want to set the value of a field in my model instance by name. Say I have: class Foo(Model): bar = CharField() f = Foo() I want to set the value of bar by name, not by accessing the field. So something like: f.fields['bar'] = 'BAR" instead of f.bar...

Use Choices from Models in a template tag

Hi Guys, I have a model with a bunch of choices, that are in the DB configured as below. COL_CHOICES =( (1, 'Not Applicable'), (2, 'Black'), ) COL2_CHOICES =( (1, 'Green'), (2, 'Blue'), ) etc. I want to display all these options as a menu in my templates, (to be used ...