django-models

How do I create trivial customized field types in Django models?

I'm trying to make some types in Django that map to standard Django types. The custom model field documentation goes into complicated cases; I just want to store a basic Django type from a class with a bunch of handy methods. For example, if I were storing playing cards, I want something like: class Card(object): """ A playing car...

Looking for input in model design for Django Schools

Today I'm starting a little project to create a Django based school administration program. I'm currently designing the models and their corresponding relationships. Being rather new to Django and relational databases in general, I would like some input. Before I show you the current model layout, you need to have an idea of what the pr...

How do I find the "concrete class" of a django model baseclass

I'm trying to find the actual class of a django-model object, when using model-inheritance. Some code to describe the problem: class Base(models.model): def basemethod(self): ... class Child_1(Base): pass class Child_2(Base): pass If I create various objects of the two Child classes and the create a queryset con...

Django dynamic OR queries

I have a MultipleChoiceField on a form holding car makes. I want to filter my database of cars to the makes that were checked but this causes a problem. How do I get all the Q(make=...) statements in dynamically? How I start: ['value1', 'value2', ...] How I want to end: Q(col='value1') | Q(col='value2') | ... I've couple of other meth...

Beginner: Trying to understand how apps interact in Django

I just got done working through the Djano tutorials for the second time, and am understanding things much more clearly now. However, I'm still unclear how apps inside a site interact with one another. For example, lets say I'm writing a blog application (a rather popular activity, apparently). Blog posts and comments tend to go together...

How can I get the number of records that reference a particular foreign key in Django?

I'm working on a blog application in Django. Naturally, I have models set up such that there are Posts and Comments, and a particular Post may have many Comments; thus, Post is a ForeignKey in the Comments model. Given a Post object, is there an easy way (ideally, through a method call) to find out how many Comments belong to the Post? ...

Django Queryset across Models?

I have several Models and want to return a queryset of all the Models belonging to a User, I'm wondering if its possible to return one Queryset from multiple Models? ...

MVC and django fundamentals

Pretty new to this scene and trying to find some documentation to adopt best practices. We're building a fairly large content site which will consist of various media catalogs and I'm trying to find some comparable data / architectural models so that we can get a better idea of the approach we should use using a framework we've never ma...

Setting up a foreign key to an abstract base class with Django

I've factored out common attributes from two classes into an abstract base class, however I have another model that needs to reference either one of those classes. It's not possible to reference an ABC as it doesn't actually have a database table. The following example should illustrate my problem: class Answer(models.Model): ovram...

Django: How can I use my model classes to interact with my database from outside Django?

I'd like to write a script that interacts with my DB using a Django app's model. However, I would like to be able to run this script from the command line or via cron. What all do I need to import to allow this? ...

Django model naming convention

What is the preferred naming convention for Django model classes? ...

What are the steps to make a ModelForm work with a ManyToMany relationship with an intermediary model in Django?

I have a Client and Groupe Model. A Client can be part of multiple groups. Clients that are part of a group can use its group's free rental rate at anytime but only once. That is where the intermediary model (ClientGroupe) comes in with that extra data. For now, when I try to save the m2m data, it just dies and says I should use the C...

Can I use a ForeignKey in __unicode__ return?

I have the following classes: Ingredients, Recipe and RecipeContent... class Ingredient(models.Model): name = models.CharField(max_length=30, primary_key=True) qty_on_stock = models.IntegerField() def __unicode__(self): return self.name class Recipe(models.Model): name = models.CharField(max_length=30, primary_...

How do I query a Django model dynamically?

Is there a way of doing the following? model = "User" model.objects.all() ...as opposed to User.objects.all(). EDIT: I am trying to make this call based on command-line input. Is it possible to avoid the import statement, e.g., model = django.authx.models.User ? Django returns an error, "global name django is not defined." ...

How can I define a polymorphic relation between models in Django?

I am working on a Django application which contains an Offer model. An Offer instance contains the pricing conditions and points to a product definition. The product model is actually a hierarchy (I have a Television model, a Camcorder model, etc.). So I would like the Offer model to contain a polymorphic (or "generic") association to...

How to do text full history in Django?

I'd like to have the full history of a large text field edited by users, stored using Django. I've seen the projects: Django Full History (Google Code) Django ModelHistory, and Django FullHistory I've a special use-case that probably falls outside the scope of what these projects provide. Further, I'm wary of how well documented, te...

In django, what is a "slug"?

When I read django code I often see in models what is called a "slug". I am not quite sure what this is but I do know it has something to do with URL:s. How and when is this slug-thing supposed to be used? (I have read it's definition in this glossary) ...

Saving object with ManyToMany relation

I have models (simplified example): class Group(models.Model): name = models.CharField(max_length = 32) class Person(models.Model): group = models.ForeignKey(Group) class Task(models.Model): group = models.ForeignKey(Group) people = models.ManyToManyField(Person) def save(self, **kwargs): ppl = Person.objects.all().filt...

Ordered lists in django

Hi, i have very simple problem. I need to create model, that represent element of ordered list. This model can be implemented like this: class Item(models.Model): data = models.TextField() order = models.IntegerField() or like this: class Item(models.Model): data = models.TextField() next = models.ForeignKey('self') ...

Cleaning up a database in django before every test method

By default when Django runs against sqlite backend it creates a new in memory database for a test. That means for every class that derives from unittest.TestCase, I get a new database. Can this be changed so that it is cleared before every test method is run? Example: I am testing a manager class that provides additional abstraction on ...