django-models

How do you override the automatically generated fieldnames in a Django model?

I have a (pre-existing) table witha column 'foo'. I want the model to have a property 'bar' which maps to foo. I already use class Meta: db_table = u'actual_table_name' to remap classes/tables from the default table name. Is there a similar way to do this for properties/fields? Thanks, Chris. P.S. This seems like a very sim...

Datetime.now() abnormality - Python

Hi folks, I'm serving a Python app through Django. Within the app I'm storing the classic "created" field within a few tables. This is how the field looks like within the Django form: created = models.DateTimeField(blank=True, default=datetime.now()) Unfortunately, datetime.now() is not accurate. In fact in the database I have set...

django model versioning/revisions/approval - how to allow user to edit own profile but keep old online until new approved?

I am building a site where users can make changes to their publicaly displayed profile. However I need all changes to be approved by an admin before going live. Until the changes are approved their old profile will be displayed. In the admin there should be a list of profiles awaiting approval. It is preferable, but not required, to keep...

Does Django admin provide group edit of models?

For instance, I have a model called Person, and it has a bool field called 'isAthlete'. I would like to be able to check off True for 50 of these Person records, and then hit submit, without having to go into each Person model record and make the change. Is there an easy or already provided way to set this up in Django? ...

How to select model records which are referenced the most by another model type in Django?

For instance, given a model A, and a model B, where A are unique records, and B are records that have Foreign Keys to A, how would you go about getting a set of A where those in the set are referenced by B at least n or more times? ...

Django Model/Database design for subclasses

Hi. Ok, i'm shit at describing. Here's a relationship diag. In Django i've made my models like: from django.db import models from datetime import datetime class Survey(models.Model): name = models.CharField(max_length=100) pub_date = models.DateTimeField('date published',default=datetime.now) def __unicode__(self): ...

Django: Get list of model fields?

I've defined a User class which (ultimately) inherits from models.Model. I want to get a list of all the fields defined for this model. For example, phone_number = CharField(max_length=20). Basically, I want to retrieve anything that inherits from the Field class. I thought I'd be able to retrieve these by taking advantage of inspect.ge...

Django singals file, cannot import model names

I have such file order: project/ app/ models.py signals.py I am keeping signals inside signals.py as it should be. and at the top of the signals.py file, I include myapp models as I do queries in these signals with from myproject.myapp.models import Foo However it doesnt seem to find it, as I run the server or ...

django model subclassing: get the subclass by querying the superclass

The following code is given: class BaseMedium(models.Model): title = models.CharField(max_length=40) slug = models.SlugField() class A(BaseMedium): url = models.URLField() class B(BaseMedium): email = models.EmailField() I now want to query every BaseMedium. b = BaseMedium.objects.all() How do I print every infor...

How to convert a Foreignkey field into ManyToMany field in Django?

how I can accomplish this without losing data? someone know how to do that? thanks to all. ...

Concurrency in django admin change view

My model: class Order(models.Model): property_a = models.CharField() property_b = models.CharField() property_c = models.CharField() Many users will access a given record in a short time frame via admin change page, so I am having concurrency issues: User 1 and 2 open the change page at the same time. Assume all values a...

get an error while applying a widget to a field

Here is my model class RecipeIngredient(models.Model): recipe = models.ForeignKey(Recipe) ingredient = models.ForeignKey(Ingredient) serving_size = models.ForeignKey(ServingSize) quantity = models.IntegerField() order = models.IntegerField() created = models.DateTimeField(auto_now_add = True) updated = models...

sharing database table between two django projects

I have two different Django projects that are meant to run in parallel and do pretty different things. However they need to share a common database table, the Client table.. Both projects contains multiple apps that needs to contain foreign keys mapped to that Client model.. I'm not sure what would be the best approach.. ...

Make django admin logEntry read only?

I found this post and it was very useful, but i need put the logEntry model into read-only in the admin interface, its that possible? thanks and sorry for my english! ...

How to make an auto-filled and auto-incrementing field in django admin

[Update: Changed question title to be more specific] Sorry if I didn't make the question very well, I can't figure how to do this: class WhatEver(): number = model.IntegerField('Just a Field', default=callablefunction) ... Where callablefunction does this query: from myproject.app.models import WhatEver def callablefunction(): ...

Models does not create tables when synched

I have some django models for my extended users profile. Problem is that this code does not create tables when syncdb is used (simply nothing happens. No validation errors). Why is that happening? (Also those models give import error elsewhere) : #!/usr/bin/env python # encoding: utf-8 from django.db import models from django.contrib.au...

How to order by aggregate with conditions on fields of a relation

My code: class School(models.Model): pass class Student(models.Model): school = models.ForeignKey(School) TYPE_CHOICES = ( ('ug', 'Undergraduate'), ('gr', 'Graduate'), ('al', 'Alumnus'), ) type = models.CharField(max_length=2) How do I obtain a QuerySet of Schools ordered by the number of Under...

Returning values from methods in Django

Hi! So, time for a newbie question but so utterly important since the documentation seems to have missed this very very basic example. All I'm trying to do is to return a value from a model to a view in Django. Here is some code. The model class Page(models.Model): def index(self): ex = 'foo string' return ex The vie...

Ordering by two different aggregate values of a relation's fields

With the following code: from django.db import models class Blogpost(models.Model): pass class Vote(models.Model): question = models.ForeignKey(Blogpost) TYPE_CHOICES = ( ('up', 'Up-Votes'), ('dn', 'Down-Votes'), ) type = models.CharField(max_length=2, choices=TYPE_CHOICES) What is the best way to obt...

Django: NameError: name 'Category' is not defined

Hi, I'm practicing on Django and using some online tutorial to build a web blog. It went smoothly with the first project, yet when I tried the 2nd one, through developing the first view, there was this statement: categories = models.ManyToManyField(Category, related_name ="packages") In the tutorial, validating the model gives 0 er...