django-models

Return function as a field on django model

I have a model which have a function to calculate the difference between two fields Example: Class MyModel(models.Model): fieldA = models.FloatField() fieldB = models.FloatField() def delta(self): return self.fieldA - self.fieldB Id like to use this model in a GenericView. I can use the function delta as an extraC...

How to find resource intensive queries in Django

How to find the execution time for various Model API Calls in Django. ...

Help with designing models for a Django 'Project' app with 1 or more urls in it (variable), with editing at once in admin interface

Hello, i'm building a simple app for Django and I'm facing some problems with my models design, and the use of 'inlines' in the administration interface. The app is to manage projects. A project consists of a name, a description, other fields (e.g. tags...) and of multiple urls (like project url, source code repository url) but the num...

Convention for checking the existence of a Django model?

What is the accepted way of checking a model's existence in a Django app? I've seen this method used: def profile_exists(user): try: UserProfile.objects.get(user = user) return True except: return False Is there a built-in function suited for this purpose? ...

Django model with filterable attributes

I've got two models. One represents a piece of equipment, the other represents a possible attribute the equipment has. Semantically, this might look like: Equipment: tractor, Attributes: wheels, towing Equipment: lawnmower, Attributes: wheels, blades Equipment: hedgetrimmer, Attributes: blades I want to make queries like, wheels = A...

How should I setup my homepage in django?

I'm not familiar with django conventions at all so if you do provide advice could you be specific Considering my homepage would contain components of articles: In Zend, in my IndexController I would create models and populate the view with articles and data. What's a convention/structure I could use for the homepage view ( sh...

Django model class methods for predefined values.

I'm working on some Django-code that has a model like this: class Status(models.Model): code = models.IntegerField() text = models.CharField(maxlength=255) There are about 10 pre-defined code/text-pairs that are stored in the database. Scattered around the codebase I see code like this: status = Status.objects.get(code=0)...

How to store python classes into a database with Django ?

Hi, I have two files: choices.py class SomeChoice: name = u"lorem" class AnotherChoice: name = u"ipsum" # etc... models.py from django.db import models import choices class SomeModel(models.Model): CHOICES = ( (1, choices.SomeChoice.name), (2, choices.AnotherChoice.name), # etc... ) so...

how do people normally deal with class variables in django?

I can't see any provision for this in the django docs, so how do people go about doing this. My specific case is this. I have a shopping cart, each cart instance has an invoice number field, however the invoice number is only generated if the cart goes to a paid status, so not all shopping cart instances will have an invoice number. I ...

How can _meta.local_fields not match the table schema in the database?

I'm completely confused about why _meta.local_fields returns more fields than the database table contains. The User model inherits from contrib.auth.models.User. $ mysql -u user -p database Enter password: Reading table information for completion of table and column names You can turn off this feature to get a quicker startup wit...

how to select orphan records from model where referential integrity is not enforced?

Given the following models implemented in sqlite3 class Parent(models.Model): pass class Children(models.Model): parent=models.ForeignKey(Parent,primary_key=True) After importing data from a spreadsheet into Children I need to get a list of Parents having no children and for this I'm using... Parent.objects.filter(children__...

Django localflavor for models? Default widget?

I see these nice "local flavors" for Canada, but they're only form fields. How would I use them in my model? I can create them all as CharFields sure, but then is there a way to set the default form widget from inside the model so that when I create a ModelForm it'll use them? ...

Django models: Why the name clash?

Firstly, I know how to fix the problem, I'm just trying to understand why it's occuring. The error message: users.profile: Reverse query name for field 'address' clashes with related field 'Address.profile'. Add a related_name a rgument to the definition for 'address'. And the code: class Address(models.Model): country = fie...

Django ManyToMany filter()

I have a model: class Zone(models.Model): name = models.CharField(max_length=128) users = models.ManyToManyField(User, related_name='zones', null=True, blank=True) And I need to contruct a filter along the lines of: u = User.objects.filter(...zones contains a particular zone...) It has to be a filter on User and it has...

Right way to return proxy model instance from a base model instance in Django ?

Say I have models: class Animal(models.Model): type = models.CharField(max_length=255) class Dog(Animal): def make_sound(self): print "Woof!" class Meta: proxy = True class Cat(Animal): def make_sound(self): print "Meow!" class Meta: proxy = True Let's say I want to do: animals =...

Django: Save User ID with Model Save

My question is very similar to this question: http://stackoverflow.com/questions/862522/django-populate-user-id-when-saving-a-model Unfortunately I did not quite understand their answer. When a user logs in I want them to be able to submit a link and I have a user_id foreign key that I just can't seem to get to populate. def submit(r...

Django admin inline form error

Hi. I have an inline formset in my admin site. I also have save_as = True in admin.py. My models are, for example: class Poll(models.Model): question = models.CharField(max_length=200, unique = True) pub_date = models.DateTimeField('date published') class Choice(models.Model): poll = models.ForeignKey(Poll) choice = mod...

Utilization of get_FOO_display()

I want to show the human-readable name for the type selected but I keep getting the stored value. TYPE_CHOICES = ( ('0', 'Basic'), ('1', 'Full'), ('2', 'Intermediate'), ) class ServiceType(models.Model): type = models.IntegerField(max_length=1, choices=TYPE_CHOICES) amount = models.DecimalField(max_digits=10, ...

Django data migration when changing a field to ManyToMany

I have a Django application in which I want to change a field from a ForeignKey to a ManyToManyField. I want to preserve my old data. What is the simplest/best process to follow for this? If it matters, I use sqlite3 as my database back-end. If my summary of the problem isn't clear, here is an example. Say I have two models: class...

Order Django objects randomly.

Here's an interesting problem, I have a list of users that I list in order of rating, and where two users have the same rating, I have a random number that I use to make sure the list won't always be the same. Currently I do this by executing a query: select app_model1.column1, app_model1.colum2, app_model2.column3 from app_mode...