django-models

django model/modelForm - How to get dynamic choices in choiceField?

hi i'm experimenting with django and the builtin admin interface. I basically want to have a field that is a drop down in the admin UI. The drop down choices should be all the directories available in a specified directory. If i define a field like this: test_folder_list = models.FilePathField(path=/some/file/path) it shows me all t...

DRY unique objects in Django

I want to ensure an object is unique, and to throw an error when a user tries to save it (e.g. via the admin) if not? By unique, I mean that some of the object's attributes might hold the same values as those of other objects, but they can't ALL be identical to another object's values. If I'm not mistaken, I can do this like so: class ...

Date range headache

#model class Promotion(models.Model): name = models.CharField(max_length=200) start_date = models.DateTimeField() end_date = models.DateTimeField() #view def promo_search(request): ... results = Promotion.objects.filter(start_date__gte=start_date).filter(end_date__lte=end_date) ... (The code above obviously is...

Default ordering for m2m items by intermediate model field in Django

Hi, I have an unusual problem. Let's consider such models (taken from django docs): class Person(models.Model): name = models.CharField(max_length=128) def __unicode__(self): return self.name class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through='Me...

Django: add image in an ImageField from image url

Hi, please excuse me for my ugly english ;-) Imagine this very simple model : class Photo(models.Model): image = models.ImageField('Label', upload_to='path/') I would like to create a Photo from an image URL (i.e., not by hand in the django admin site). I think that I need to do something like this : from myapp.models import Ph...

Django model inheritance, filtering models

Given the following models:(don't mind the TextFields there're just for illustration) class Base(models.Model): field1 = models.TextField() class Meta: abstract=True class Child1(Base): child1_field = models.TextField() class Child2(Base): child2_field = models.TextField() class Content(models.Model): aso_item...

How to sort by annotated Count() in a related model in Django

Hi, I'm building a food logging database in Django and I've got a query related problem. I've set up my models to include (among other things) a Food model connected to the User model through an M2M-field "consumer" via the Consumption model. The Food model describes food dishes and the Consumption model describes a user's consumption o...

Getting the history of an object

Hello I use django admin for my users to add their Model objects, as you know django is keeping the track of user actions such as the user who added an item. For an object, At a custom view outside the admin panel, I need to display the user name of the adder. How can I fetch/retrieve this data ? Cheers ...

Django model inheritance - can I change model type?

When I use multi-table inheritance, Django creates two tables - one for the base class, and one for the derived one, pointing to the first. Is there a way to keep the base table entry while deleting the derived one, and create another entry for another model? To put it simpler: I have models: A, B(derived from A), C(derived from A). I w...

Store django forms.MultipleChoiceField in Models directly

Say I have choices defined as follows: choices = (('1','a'), ('2','b'), ('3','c')) And a form that renders and inputs these values in a MultipleChoiceField, class Form1(forms.Form): field = forms.MultipleChoiceField(choices=choices) What is the right way to store field in a model. I can of course loop thr...

detect the HOST domain name in django models

Hi folks! In my model, I want to use the domain name (HOST) I'm using in my views. In views that'd be doable, thanks to the "request" object. But how do I do this models methods? Which don't use "HttpRequest" objects? Now I'm setting a global value HOST in settings.py and using it, but that's ugly. Also, I don't really want to manag...

Speed up database inserts from ORM

I have a Django view which creates 500-5000 new database INSERTS in a loop. Problem is, it is really slow! I'm getting about 100 inserts per minute on Postgres 8.3. We used to use MySQL on lesser hardware (smaller EC2 instance) and never had these types of speed issues. Details: Postgres 8.3 on Ubuntu Server 9.04. Server is a "large"...

django: manually adding a foreign key column (newcolumn_id_refs_id_4bfb2ece ?)

Hi there, I need to add a foreign key field to an existing django model/postgres table. As per the django documentation, I ran the 'sqlall myapp' command to 'work out the difference'. The obvious difference is that the table in question now has an extra column with a new contraint, which looks like this: ALTER TABLE "myapp_mytable" A...

Django, sorl-thumb dont work

Hi guys, i dont know what im doing wrong, but sorl-thumb just upload the image but dont make thumbs... model.py from sorl.thumbnail.fields import ImageWithThumbnailsField ,ThumbnailField imagen = ImageWithThumbnailsField(upload_to='images', thumbnail={'size': (75, 75)}, ...

Django: Skipping model validation

Hi I'm using the development server (runserver) in Django and it's started to annoy me that Django is validating the models every time I save a file and the server restarts. I have ~8000 entries in my SQLite3 database and it takes up to five seconds to validate the models. I'm not familiar with how Django validates the models, but I'm...

Change list link to foreign key change page

When viewing the admin change list for a model, is it possible to make the columns that correspond to foreign keys links to their respective pages? A simple example is I have a Foo object which contains Bar as a foreign key. If I'm viewing the admin change list for Foo (and have it set to include Bar in the display_list columns), the m...

Should I refactor my Django object model?

For reasons it's not worth getting into, the object Model of the django application I am working on is now "wrong" insofar as a number of relations that are 1 to Many are represented as Many to Many. The application functions correctly and is most of the way through QA. It has never been deployed. My design OCD makes me want to refact...

use models in separate project as a foreign key in Django?

Possible stupid question: let's say I have two apps contained within one project. Can I use models from one app as foreign keys in the other app's models? ...

Django, actual month in queryset

how ill, get my register based in the current (actual) month in my queryset?, i have a ModelManager(), that just show the LIVE register status, but now i want to show the register with LIVE status and in the current (actual) month, i know that ill make something like .filter(...), but i dont know how get the current month.. model.py #m...

adding the same object twice to a ManyToManyField

I have two django model classes: class A(models.Model): name = models.CharField(max_length = 128) #irrelevant class B(models.Model): a = models.ManyToManyField(A) name = models.CharField(max_length = 128) #irrelevant What I want to do is the following: a1 = A() a2 = A() b = B() b.a.add(a1) b.a.add(a1) #I wan...