django-orm

Django ORM and PostgreSQL connection limits

I'm running a Django project on Postgresql 8.1.21 (using Django 1.1.1, Python2.5, psycopg2, Apache2 with mod_wsgi 3.2). We've recently encountered this lovely error: OperationalError: FATAL: connection limit exceeded for non-superusers I'm not the first person to run up against this. There's a lot of discussion about this error, speci...

Does a Postgresql dump create sequences that start with - or after - the last key?

I recently created a SQL dump of a database behind a Django project, and after cleaning the SQL up a little bit was able to restore the DB and all of the data. The problem was the sequences were all mucked up. I tried adding a new user and generated the Python error IntegrityError: duplicate key violates unique constraint. Naturally I ...

Django: return one filtered object per foreign key

Is it possible to return querysets that return only one object per foreign key? For instance, I want the to get the latest comments from django_comments, but I only want one comment (the latest comment) per object, i.e., only return the latest comment on an object and exclude all the past comments on that object. I guess this would be ...

Is there anything like unique_together(max_occurences=3) ?

I have a model: class MyModel(models.Model): a = models.IntegerField() b = models.IntegerField() c = models.IntegerField() Now, I need something like unique_together(a,b, max_occurences=3) constraint to be added to the above model (so that there can be up to 3 values of c for each pair of (a,b), and ideally those 3 val...

Django ORM misreading PostgreSQL sequences?

Background: Running a PostgreSQL database for a Django app (Django 1.1.1, Python2.4, psycopg2 and Postgres 8.1) I've restored the database from a SQL dump several times. Each time I do that and then try to add a new row, either shell, admin, or site front end, I get this error: IntegrityError: duplicate key violates unique constraint "a...

Django - Selecting related set : how many times does it hit the database ?

I took this sample code here : http://stackoverflow.com/questions/853184/django-orm-selecting-related-set polls = Poll.objects.filter(category='foo') choices = Choice.objects.filter(poll__in=polls) My question is very simple : do you hit twice the database when you finally use the queryset choices ? ...

django's .extra(where= clauses are clobbered by table-renaming .filter(foo__in=... subselects

The short of it is, the table names of all queries that are inside a filter get renamed to u0, u1, ..., so my extra where clauses won't know what table to point to. I would love to not have to hand-make all the queries for every way I might subselect on this data, and my current workaround is to turn my extra'd queries into pk values_li...

How many rows were deleted?

Is it possible to check how many rows were deleted by a query? queryset = MyModel.object.filter(foo=bar) queryset.delete() deleted = ... Or should I use transactions for that? @transaction.commit_on_success def delete_some_rows(): queryset = MyModel.object.filter(foo=bar) deleted = queryset.count() queryset.delete() PHP...

How to mimic Python set with django ORM?

Hello all, I am working on a membership application. I would like to make a membership reminder. (member during a period of time which is not member for another period of time). Currently, I am using set for making this calculation. See the code below. class Member(models.Model): ... class Membership(models.Model): member = ...

django-orm case-insensitive order by

Hello all, I know, I can run a case insensitive search from DJango ORM. Like, User.objects.filter(first_name__contains="jake") User.objects.filter(first_name__contains="sulley") User.objects.filter(first_name__icontains="Jake") User.objects.filter(first_name__icontains="Sulley") And also, I can fetch them as user_list = User.objects...

Elegant way to count frequency and correlation of many-to-many relationships with Django ORM?

Hi I have a Pizza model and a Topping model, with a many-to-many relationship between the two. Can you recommend an elegant way to extract: the popularity (frequency) of each topping the correlation between toppings (i.e. which sets of toppings are most frequent) Thanks ...

Django ORM Query: relationships

There is a model: class DomainPosition(models.Model): domain = models.ForeignKey(Domain) keyword = models.ForeignKey(Keyword) date = models.DateField() position = models.IntegerField() class Meta: ordering = ['domain', 'keyword'] How to get the data for a template? For each domain want to display table (fi...

How to create postgres date index properly?

Hi. I'm using Django ORM and postgresql. ORM creates a query: SELECT (date_part('month', stat_date)) AS "stat_date", "direct_keywordstat"."banner_id", SUM("direct_keywordstat"."total") AS "total", SUM("direct_keywordstat"."clicks") AS "clicks", SUM("direct_keywordstat"."shows") AS "shows" FROM "direct_keywor...

Django equivalent of SQL REPLACE

Is there a Django ORM best practice for this SQL: REPLACE app_model SET field_1 = 'some val', field_2 = 'some val'; Assumption: field_1 or field_2 would have a unique key on them (or in my case on both), otherwise this would always evaluate to an INSERT. Edit: My best personal answer right now is this, but it's 2-3 queries where 1 s...

Is there a downside to using ".filter().filter().filter()..." in Django?

Are the following two calls resolved to the equivalent SQL query in Django? Chaining multiple calls Model.objects \ .filter(arg1=foo) \ .filter(arg2=bar) \ ... Wrapping all the args together: Model.objects \ .filter(arg1=foo, arg2=bar) I'd like code to be readable (there are MANY more filter calls than I've shown), but only if the...

Serializing Foreign Key objects in Django

I have been working on developing some RESTful Services in Django to be used with both Flash and Android apps. Developing the services interface has been quite simple, but I have been running into an issue with serializing objects that have foreign key and many to many relationships. I have a model like this: class Artifact( models.Mod...

Django query on history

Hi there, I have a model "Item", which has a 1:n to "Location". Means, there is a location-history for items. Location has a FK to "Room", "Room" to "Floor" and "Floor" to "Building". Now, I want to select all Items which are currently located an a specific Floor. I could solve it with a list comprehension, but is there any nicer way ...

order objects by most unique views (stored in another table with ip) in django

Related models: models.py class Entry(models.Model): ... class EntryView(models.Model): entry = models.ForeignKey(Entry) dt_clicked = models.DateTimeField(auto_now_add=True) ip = models.CharField(max_length=15, db_index=True, blank=True) host = models.CharField(max_length=64, db_index=True, blank=True) referer ...

Increating a datime field with queryset.update

My model looks like this class MyModel(models.Model): end_time = DateTimeField() and this is what I'm trying to achieve: m=MyModel.objects.get(pk=1) m.end_time += timedelta(seconds=34) m.save() but I want to do it with update() to avoid race conditions: MyModel.objects.filter(pk=1).update(end_time=F('end_time')+timedelta(secon...

Django model inheritance problem. How to solve?

Hello all, I have an existing app with the following model class Contact(models.Model): lastname = models.CharField(max_length=200) firstname = models.CharField(max_length=200) ... class Journalist(Contact): pass I have a Contact in my database and I would like that it becomes a Journalist. In raw sql, it seems...