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

664 questions
448
votes
25 answers

How to see the raw SQL queries Django is running?

Is there a way to show the SQL that Django is running while performing a query?
spence91
  • 77,143
  • 9
  • 27
  • 19
146
votes
9 answers

How to log all sql queries in Django?

How can I log all SQL queries that my django application performed? I want to log everything, including SQLs from admin site. I saw this question and a FAQ answer but I still can't figure out where should I put from django.db import…
Oleg Pavliv
  • 20,462
  • 7
  • 59
  • 75
142
votes
3 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,892
  • 4
  • 15
  • 15
88
votes
2 answers

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
  • 22,492
  • 12
  • 61
  • 85
69
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,915
  • 8
  • 37
  • 49
66
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
  • 9,924
  • 3
  • 42
  • 81
56
votes
6 answers

How to view database and schema of django sqlite3 db

I'm new to django framework. I tried to create a simple blog by following djangogirls tutorials. Here by default, we get sqlite3 as default database Engine: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', …
Shivkumar kondi
  • 6,458
  • 9
  • 31
  • 58
55
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
  • 547
  • 1
  • 5
  • 8
54
votes
7 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
  • 5,115
  • 3
  • 32
  • 52
49
votes
3 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,176
  • 1
  • 12
  • 16
31
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,326
  • 11
  • 38
  • 62
29
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
  • 2,034
  • 6
  • 24
  • 32
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
  • 12,722
  • 20
  • 82
  • 106
22
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 Dahan
  • 10,576
  • 11
  • 64
  • 137
21
votes
6 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
  • 10,243
  • 13
  • 60
  • 120
1
2 3
44 45