django-models

Dynamically Display field values in Django template (object.x)

Hi Guys, I am currently working on an app that uses custom annotate querysets. Currently i have 2 urls setup, but i would need one for each field that the users would like to summarize data for. This could be configured manually, but it would violate DRY! I would basically have +-8 urls that basically do the same thing. So here is wha...

Making transient (non-database) attributes in Django model available to template.

One of my models has attributes that are not stored in the database. Everything is fine at the view and model level but I cannot seem to display these 'non-database' attributes in my template. Here's some example code, an artificial example that mirrors the actual problem domain, to demonstrate the undesired behavior. The view: def o...

django: time range based aggregate query

Hi there! I have the following models, Art and ArtScore: class Art(models.Model): title = models.CharField() class ArtScore(models.Model): art = models.ForeignKey(Art) date = models.DateField(auto_now_add = True) amount = models.IntegerField() Certain user actions results in an ArtScore entry, for instance whenever y...

Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like the following: On model load: SELECT AES_DECRYPT(fieldname, password) FROM tablename On model save: INSERT INTO tablename VALUES (AES_EN...

How to get two random records with Django

How do I get two distinct random records using Django? I've seen questions about how to get one but I need to get two random records and they must differ. ...

Problem getting started with GeoDjango

As soon as I add "from django.contrib.gis.db import models" instead of "from django.db import models", Django stops recognizing the app and gives this error: Error: App with label location could not be found. Are you sure your INSTALLED_APPS setting is correct? The error goes away as soon as I comment out "from django.contrib.gis.db i...

Django auto_now and auto_now_add

For Django 1.1. I have this in my models.py: class User(models.Model): created = models.DateTimeField(auto_now_add=True) modified = models.DateTimeField(auto_now=True) When updating a row I get : [Sun Nov 15 02:18:12 2009] [error] /home/ptarjan/projects/twitter-meme/django/db/backends/mysql/base.py:84: Warning: Column 'crea...

object_list of multiple models in django

I have multiple abstract models similar to this Class Article(models.Model): title = models.CharField() body = models.TextField() created_date = models.DateTimeField() author_name = models.CharField() class Video(models.Model): title = models.CharField() body = models.TextField() created_date = models.DateTi...

How can I limit the available choices for a foreign key field in a django modelformset?

Application: This is a workshop proposal system for a conference. A user can create presenters and workshops, and link them together. Each user should only have access to the presenters and workshops that s/he created/owns. # Models: class Workshop(models.Model): name = models.CharField(max_length=140, db_index=True) presenter...

What's the best way to create a 'history' type model in django?

I'd like to create a feature for my Django app similar to Django admin's 'Recent Actions', in order to store history information on my other models. For example say I have two models called Book and Author. I want to have a third model that stores information such as what action was performed on a given object in a model (add, modify, d...

Many to many relationships with additional data on the relationship.

I'm trying to create a django database that records all my comic purchases, and I've hit a few problems. I'm trying to model the relationship between a comic issue and the artists that work on it. A comic issue has one or more artists working on the issue, and an artist will work on more than a single issue. In addition, the artist has ...

Django FileField url not relative

I have something like: MEDIA_ROOT = '/home/httpd/foo/media/' MEDIA_URL = 'http://www.example.org/media/' (...) file = models.FileField(upload_to='test') When I create an object with that field in the admin page Django stores in the DB the full file path, like: "/home/httpd/foo/media/test/myfile.pdf". This is contrary to what says i...

Django database query - return the most recent three objects

Hi This can't be hard, but... I just need to get the most recent three objects added to my database field. So, query with reverse ID ordering, maximum three objects. Been fiddling round with Records.objects.order_by(-id)[:3] Records.objects.all[:3] and including an if clause to check whether there are actually three objects: n...

How can I use conditional sorting at Django queries?

I'm implementing a basic forum app. I would like to sort the questions by their last reply time. I have the following line: questions = Question.objects.filter(deleted=False).order_by("last_comment__created_at") However, this query ignores the new questions with no answers. What would be the best way to fix this without creating a...

Need help with Django model design, ManyToManyField "through" an intermediate model and its implications for uniqueness

I have the following Django models: - class Company(models.Model): name = models.CharField(max_length=50) is_active = models.BooleanField(db_index=True) class Phase(models.Model): company = models.ForeignKey(Company) name = models.CharField(max_length=50) is_active = models.BooleanField(db_index=True) class Proces...

how to insert a infomation on a table in Django

This is my form on models.py class ItemForm(forms.Form): itemname = forms.CharField(max_length=100) itemwording = forms.CharField(max_length=100) notes = forms.CharField() abundance = forms.IntegerField(max_value=10) collunit = forms.CharField(max_length=50) litref = forms.CharField(max_length=100) litkey = ...

Django: Is there an in_ lookup for querying model objects like there is for SQLAlchemy queries?

I'm trying to accomplish something like this: userSelectionIDs = [pref.selectionID for pref in UserColumnSelectionPreference.objects.filter(user=reqUser).all()] selections = ColumnSelections.objects.filter(id.in_(userSelectionIDs)).filter(type=2).all() Or, is there a better way for me to get that set of objects? ...

Django - how to determine if model class is abstract

If a django model is made abstract, like below, is there a way to inspect the class to determine that it is abstract? class MyModel(models.Model): class Meta: abstract = True I would expect that I could examine MyModel.Meta.abstract, but according to Django docs: Django does make one adjustment to the Meta class of an abstra...

How to count and display objects in relation ManyToMany in Django

Hi! I have a simple model with news and categories: class Category(models.Model): name = models.CharField() slug = models.SlugField() class News(models.Model): category = models.ManyToManyField(Category) title = models.CharField() slug = models.SlugField() text = models.TextField() date = models.DateTimeFie...

Interacting with external DB via Django

I'm working on a Django app that interacts with an existing database (think ERP/transaction type data) to perform analysis. There will be minimal/no updating of the existing database mainly reading data in. Its just a simple small setup so no replication etc. issues to think about re. updating. The analysis would result in new records c...