Questions tagged [django-database]

django-database refers to database backends that Django supports out of the box

refers to database backends that Django supports out of the box.

This tutorial help you to make application using mongodb database. enter link description here

551 questions
104
votes
2 answers

Update only specific fields in a models.Model

I have a model class Survey(models.Model): created_by = models.ForeignKey(User) question = models.CharField(max_length=150) active = models.NullBooleanField() def __unicode__(self): return self.question and now I want to…
Registered User
  • 1,452
  • 4
  • 12
  • 15
77
votes
1 answer

Add Indexes (db_index=True)

I'm reading a book about coding style in Django and one thing they discuss is db_index=True. Ever since I started using Django, I've never used this function because I'm not really sure what it does. So my question is, when to consider adding…
catherine
  • 21,020
  • 12
  • 54
  • 75
51
votes
6 answers

How can I subtract or add 100 years to a datetime field in the database in Django?

How can I subtract or add 100 years to a datetime field in the database in Django? The date is in database, I just want to directly update the field without retrieving it out to calculate and then insert.
kelvinfix
  • 2,435
  • 8
  • 34
  • 48
51
votes
3 answers

Django - Rollback save with transaction atomic

I am trying to create a view where I save an object but I'd like to undo that save if some exception is raised. This is what I tried: class MyView(View): @transaction.atomic def post(self, request, *args, **kwargs): try: …
Gocht
  • 8,493
  • 3
  • 31
  • 66
43
votes
2 answers

Simple Subquery with OuterRef

I am trying to make a very simple Subquery that uses OuterRef (not for practical purposes, but just to get it working), but I keep running into the same error. posts/models.py code from django.db import models class Tag(models.Model): name =…
mjuk
  • 427
  • 1
  • 4
  • 8
40
votes
2 answers

Django multiple and dynamic databases

I've been evaluating django and wondered if the following is possible. I've already looked at the regular multiple database docs so please don't point me to that because this use case isn't mentioned as far as i can make out. If i'm wrong i take it…
nickjb
  • 1,006
  • 1
  • 12
  • 16
40
votes
6 answers

Django - how to specify a database for a model?

Is there a way to specify that a model (or app, even) should only ever use one particular database? I am working with a legacy database that I don't want to change. I have two databases - the 'default' is an sqlite one that could be used for admin…
pfctdayelise
  • 4,677
  • 2
  • 29
  • 49
24
votes
3 answers

Django update table using data from another table

I have 2 tables products and catagories connected by foreign key. I need to update field products.new_cost using field catagories.price_markup as following: UPDATE products p INNER JOIN categories c ON p.category_id = c.id SET p.new_cost =…
Deadly
  • 1,794
  • 6
  • 20
  • 30
23
votes
2 answers

How to make Django work with unsupported MySQL drivers such as gevent-mysql or Concurrence's MySQL driver?

I'm interested in running Django on an async framework like Concurrence or gevent. Both frameworks come with its own async MySQL driver. Problem is Django only officially supports MySQLdb. What do I need to do to make Django work with the MySQL…
Continuation
  • 11,664
  • 19
  • 77
  • 102
20
votes
3 answers

How to count the number of rows in a database table in Django

I have database created by Django model, where status and id and username are the fields of the table Messages. I need to count the number of rows where status value= 0 for a particular username. This is my incomplete code: Message_me =…
Friend
  • 1,166
  • 10
  • 34
  • 60
19
votes
5 answers

Django Migration RunSQL Conditional on Database Type

I am trying to use a migrations.RunSQL Django migration to run some arbitrary SQL code. I would like to run this migration for only certain db backends (for example only for postgres). I would think to use something like this but I don't see the DB…
Alex Rothberg
  • 8,138
  • 10
  • 46
  • 102
19
votes
1 answer

How Django atomic requests works?

I'd like my Django views to be atomic. I mean, if there is 2 DB-writes in the view, I want either 0 write, either 2 writes. For example: def test_view(request): ''' A test view from views.py ''' MyClass.objects.create() raise…
David D.
  • 8,377
  • 5
  • 49
  • 101
16
votes
1 answer

Copy a database column into another in Django

I am writing a migration that requires me to fill a field with existing data from another field (with same type and constraints). Is it possible in Django to copy the data in a single operation? The destination column already exists when I need to…
mimo
  • 2,062
  • 20
  • 44
16
votes
3 answers

Django: dynamic database file

In my Django project I have a dependency for a third party application that produces SQLite cache files in various directories with a known schema. I'd like to use Django models to access those databases, but obviously I cannot use a static…
Constantinius
  • 30,633
  • 7
  • 68
  • 82
15
votes
1 answer

django database delete specific number of entries

How to delete specific number of entries from the database? I did something like this EntriesToDelete=Statusmessages.objects.filter(time__lt=date)[:30000] EntriesToDelete.delete() But I get an error which says: AssertionError. Cannot use 'limit' or…
arjun
  • 1,953
  • 4
  • 17
  • 21
1
2 3
36 37