django-models

Django model ON DELETE CASCADE policy, emulate ON DELETE RESTRICT instead, general solution

I'd like to delete an instance of a model, but only if it doesn't have any other instance of another class with a foreign key pointing to it. From Django documentation: When Django deletes an object, it emulates the behavior of the SQL constraint ON DELETE CASCADE -- in other words, any objects which had foreign keys pointing at the ...

Does Django support model classes that inherit after many non-abstract models?

Lets say I have three django model classes - lets call them A, B and C. If A and B are abstract, I can do something like: class C(A,B): pass What if they aren't abstract and I do the same? Will everything still work correctly or no? Or have I got it wrong and this should not be done with abstract models either? I'm having some is...

Is there a way to express Django admin settings inside the models, instead of admin.py?

Talking about Django 1.1.1. I thought at one time (0.96) the kinds of things put inside of the admin.py file were part of an inner class of the model. There's a certain beauty in having all of this in one place. But I don't know if this change was out of necessity. Any compelling reasons one way or the other? ...

Multiple Tables & Modified Querysets

I've got to keep an inventory of servers and their memory modules & drives. I've created three tables. I want to be able to retrieve all information about the server, including memory and drive information, and display it in one page. class Server(models.Model): Name = models.CharField(max_length=25) ServiceTag = models...

Share a model inbetween two django projects

Is it possible to have a model being shared by two or more apps located in separate projects and if so how? ...

How should I build this Django model to do what I want

This is what I had before (but realized that you can't obviously do it in this order: class MasterAdmin(models.Model): """ A permanent admin (one per Account) that shouldn't be deleted. """ admin = models.OneToOneField(AccountAdmin) class Account(models.Model): """ A top-level account in the system. """ ...

Django: How do I validate unique_together from within the model

I have the following: class AccountAdmin(models.Model): account = models.ForeignKey(Account) is_master = models.BooleanField() name = models.CharField(max_length=255) email = models.EmailField() class Meta: unique_together = (('Account', 'is_master'), ('Account', 'username'),) If I then create a new Accou...

Django: Getting the foreign key type

Is there a way to get the foreign key type of the model my key relates to? Currently, I'm trying something like: def __init__(self, *args, **kwargs): super(JobOrderSupplementForm, self).__init__(*args, **kwargs) for field in self.fields: if type(self.fields[field]) == TypedChoiceField: fieldOp...

Django Custom Model Field with Validation...how to hook it back to ModelForm

Hello... A common occurrence I have with one particular project is that it requires the user to enter dimensions (for width/depth/height) in Feet and Inches. Calculations are needed to be performed on that dimension, so I've been working on a custom field type that takes in a dimension in Feet/Inches (eg. 1'-10") and saves it to the dat...

How to set up Django admin.TabularInline on a table with a compount key.

I've essentially got two tables: Page(PK=url) and PageProperty(PK=url+name). Here is how I have my Models set up: class Page(model.Model): url = model.CharField(primary_key=True, max_length=255, db_column='url') #..... class PageProperty(model.Model): # table with compound key (url + name) url = model.ForeignKey('P...

Django: How do I insert an existing file into the db?

The user uploads a file and I convert that file to a new format. How do I insert the created file into the DB? When I do the obvious field.file = newfile, it tries to upload it. So, I guess the question is, how do I add a file to the database without having it try and write the file to the filesystem? --Edit-- I don't want to store th...

Is there a clever way to get the previous/next item using the Django ORM?

Say I have a list of photos ordered by creation date, as follows: class Photo(models.Model): title = models.Char() image = models.Image() created = models.DateTimeField(auto_now_add=True) class Meta: ordering = ('-created',) I have an arbitrary Photo object photo_x. Is there an easy way to find the previous an...

django queryset datetime values substraction

Hello, I have model with following field. date = models.DateTimeField(auto_now_add=True) When querying such model i`d like to have additional column that would keep difference between current date and previous one. So for 10 rows it would have 9 values, first one would be None. Are there any ways of achieving this with querysets? or m...

django - simple equation

why does model.diff return 18446744073709551615 in template, when model is like this and model.pos is 0 and model.neg is 1?: class Kaart(models.Model): neg = models.PositiveIntegerField(default=0) pos = models.PositiveIntegerField(default=0) def diff(self): return self.pos - self.neg ...

Changing properties of inherited field

I want to alter properties of a model field inherited from a base class. The way I try this below does not seem to have any effect. Any ideas? def __init__(self, *args, **kwargs): super(SomeModel, self).__init__(*args, **kwargs) f = self._meta.get_field('some_field') f.blank = True f.help_text = 'This is optional' ...

Why does Django throw an exception whenever I enable admin.autodiscover()?

Here is my setup. I am using Django version 1.1.1 on Dreamhost, Python 2.4. The problem I am having is whenever I create a simple app and also have admin.autodiscover() enabled, Django will throw an exception. My setup: from django.conf.urls.defaults import * from testapp.views import HelloWorld from django.contrib import admin admin.a...

Use the same Django models across two different databases in the same app

I have a set of Django models that are used in two databases (i.e. syncdb was run against two databases from the same app). Both databases are for production services (one database contains on-demand "sandbox" build information and the other contains nightly build information). The problem is that I want to have one Django app that dis...

authenticate returns nothing

Hi, what im experimenting is the next: S:\proj>manage.py shell Python 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) django 1.1.1 >>> from django.contrib.auth.models import User >>> u = User(username='luis', password='admin') >>> u.save() #sucessfull created in mysql db >>> from django.contrib.auth import authenticate >>> usuario = authenti...

Django admin site reverse ForeignKey inline

I have these models: (pseudocode) Course: ForeignKey(Outline, null=True, blank=True) ForeignKey(OutlineFile, null=True, blank=True) Outline: //data OutlineFile: //different data The situation is that any course can have an Outline associated with it, and/or an OutlineFile, or neither. An Outline can be asso...

Django: How to define a model relationship to achieve the following?:

My app has clients that each have a single billing profile. I'm envisioning my app having a "Client" model with an attribute called "billing_profile" which would reference another model called "BillingProfile". Rather than define "BillingProfile" with a foreign key back to "Client" (ie, "client = models.ForeignKey(Client)"), I was think...