django-models

Represent Ordering in a Relational Database

I have a collection of objects in a database. Images in a photo gallery, products in a catalog, chapters in a book, etc. Each object is represented as a row. I want to be able to arbitrarily order these images, storing that ordering in the database so when I display the objects, they will be in the right order. For example, let's say...

Django signals vs. overriding save method

I'm having trouble wrapping my head around this. Right now I have some models that looks kind of like this: def Review(models.Model) ...fields... overall_score = models.FloatField(blank=True) def Score(models.Model) review = models.ForeignKey(Review) question = models.TextField() grade = models.IntegerField() A ...

In Django how do I notify a parent when a child is saved in a foreign key relationship?

I have the following two models: class Activity(models.Model): name = models.CharField(max_length=50, help_text='Some help.') entity = models.ForeignKey(CancellationEntity) ... class Cancellation(models.Model): activity = models.ForeignKey(Activity) date = models.DateField(default=datetime.now().date()) descrip...

Database and version control system.

Hi, I'm work on project with django framework and use control version system to synchronize my code with other peoples. But i don't know how organize work with database. In django, any people, worked on project, may changes django models, and tell 'syncdb' to synchronize model objects with db. But other people don't about this changes, ...

Django with custom, non-SQL service to store models?

Can I have a custom service providing the storage of the models I use in Django? That service would not be speaking SQL. I would like to build a web frontend to a system that consists of multiple services linked with a network based IPC, one of which provides an interface to commonly used, persistent objects (stored in a database). The...

Generate JavaScript objects out of Django Models

I am performing a lot of JavaScript work in the browser and would like to have some of that backend functionality in the front-end. Specifically, it would be nice to have the functions get(), save(), all() and count() available to the client. Additionally, it would be great to have the field list of the model already available in the gen...

How do I restrict foreign keys choices to related objects only in django

I have a two way foreign relation similar to the following class Parent(models.Model): name = models.CharField(max_length=255) favoritechild = models.ForeignKey("Child", blank=True, null=True) class Child(models.Model): name = models.CharField(max_length=255) myparent = models.ForeignKey(Parent) How do I restrict the choices ...

Single Table Inheritance in Django

Is there explicit support for Single Table Inheritance in Django? Last I heard, the feature was still under development and debate. Are there libraries/hacks I can use in the meantime to capture the basic behavior? I have a hierarchy that mixes different objects. The canonical example of a corporation structure with an Employee class, ...

In django, how do I sort a model on a field and then get the last item?

Specifically, I have a model that has a field like this pub_date = models.DateField("date published") I want to be able to easily grab the object with the most recent pub_date. What is the easiest/best way to do this? Would something like the following do what I want? Edition.objects.order_by('pub_date')[:-1] ...

designing model structure for django

Hi, I'm trying to design a model for a application allowing 2 people to bet with each other (I know, sounds stupid...). What I'm wondering about is how to connect the bet with users. The structure is like this |-------------| |----------| | Bet | | User | | BetUser1 | |----------| | BetUser2 | | Winn...

Can you achieve a case insensitive 'unique' constraint in Sqlite3 (with Django)?

So let's say I'm using Python 2.5's built-in default sqlite3 and I have a Django model class with the following code: class SomeEntity(models.Model): some_field = models.CharField(max_length=50, db_index=True, unique=True) I've got the admin interface setup and everything appears to be working fine except that I can create two Som...

How can one get the set of all classes with reverse relationships for a model in Django?

Given: from django.db import models class Food(models.Model): """Food, by name.""" name = models.CharField(max_length=25) class Cat(models.Model): """A cat eats one type of food""" food = models.ForeignKey(Food) class Cow(models.Model): """A cow eats one type of food""" food = models.ForeignKey(Food) cl...

Django: How do I model a tree of heterogeneous data types?

I need to store a tree data structure in my database, for which I plan on using django-treebeard or possibly django-mptt. My source of confusion is that each node could be one of three different possible types: root nodes will always be a type A entity, leaf nodes a type C entity, and anything in between will be a type B entity. I woul...

"Unknown column 'user_id' error in django view

I'm having an error where I am not sure what caused it. Here is the error: Exception Type: OperationalError Exception Value: (1054, "Unknown column 'user_id' in 'field list'") Does anyone know why I am getting this error? I can't figure it out. Everything seems to be fine. My view code is below: if "login" in request.sessi...

Django foreign key access in save() function

Here's my code: class Publisher(models.Model): name = models.CharField( max_length = 200, unique = True, ) url = models.URLField() def __unicode__(self): return self.name def save(self): pass class Item(models.Model): publisher = models.ForeignKey(Publisher) name =...

In Django, how does one filter a QuerySet with dynamic field lookups?

Given a class: from django.db import models class Person(models.Model): name = models.CharField(max_length=20) Is it possible (and if so how) to have a QuerySet that filters based on dynamic arguments, likeso: # instead of Person.objects.filter(name__startswith='B') # and Person.objects.filter(name__endswith='B') # is th...

Admin generic inlines for multi-table subclassed models broken --- any alternatives?

Here's what I'm trying to do, and failing... I have a File model which has a generic-relation to other objects: class File(models.Model): content_type = models.ForeignKey(ContentType) object_id = models.PositiveIntegerField() content_object = generic.GenericForeignKey() file = models.FileField(upload_to='files/%Y/%m/%d'...

Using django how can I combine two queries from separate models into one query?

In my specific case, I have two kinds of "messages" that I need to retrive and paginate. Let's omit the details, and just say that the first kind is in a model called Msg1 and the other is called Msg2 The fields of these two models are completely different, the only fields that are common to the two models are "date" and "title" (and o...

Django: How can I protect against concurrent modification of data base entries

If there a way to protect against concurrent modifications of the same data base entry by two or more users? It would be acceptable to show an error message to the user performing the second commit/save operation, but data should not be silently overwritten. I think locking the entry is not an option, as a user might use the "Back" but...

Foreign key from one app into another in Django

Hi, I'm wondering if it's possible to define a foreign key in a models.py file in Django that is a reference to a table in another app? In other words, I have two apps, called cf and profiles, and in cf/models.py I have (amongst other things): class Movie(models.Model): title = models.CharField(max_length=255) and in profiles/mo...