How to count the number of rows in a database table in Django
Try this code: Message_me = Messages.objects.filter(username=”myname”, status=0).count()
Try this code: Message_me = Messages.objects.filter(username=”myname”, status=0).count()
See the docs FAQ: “How can I see the raw SQL queries Django is running?” django.db.connection.queries contains a list of the SQL queries: from django.db import connection print(connection.queries) Querysets also have a query attribute containing the query to be executed: print(MyModel.objects.filter(name=”my name”).query) Note that the output of the query is not valid SQL, because: “Django … Read more
I will open with “You should not edit settings at runtime”. Having said that, I have exactly this same issue, where I want to create a unique database for each user. The reason for doing this is I am offering the ability for the user to save/access to/from a database not stored on my server, … Read more
You can’t specify a database for a model, but you can define it in a custom DB router class. # app/models.py class SomeModel(models.Model): … # app/dbrouters.py from app.models import SomeModel … class MyDBRouter(object): def db_for_read(self, model, **hints): “”” reading SomeModel from otherdb “”” if model == SomeModel: return ‘otherdb’ return None def db_for_write(self, model, **hints): … Read more
Goto the folder where the database is and then sqlite3 db.sqlite3 Then .tables or .schema depending on what you want. Instead of invoking sqlite3 directly you could do python manage.py dbshell and then type the sqlite commands. If you are working with a legacy database you can generate Django models for that using the python … Read more
One of the problems with your example is that you cannot use queryset.count() as a subquery, because .count() tries to evaluate the queryset and return the count. So one may think that the right approach would be to use Count() instead. Maybe something like this: Post.objects.annotate( count=Count(Tag.objects.filter(post=OuterRef(‘pk’))) ) This won’t work for two reasons: The … Read more
Atomicity Documentation To summarize, @transaction.atomic will execute a transaction on the database if your view produces a response without errors. Because you’re catching the exception yourself, it appears to Django that your view executed just fine. If you catch the exception, you need to handle it yourself: Controlling Transactions If you need to produce a … Read more
I would use the relativedelta function of the dateutil.relativedelta package, which will give you are more accurate ‘n-years ago’ calculation: from dateutil.relativedelta import relativedelta import datetime years_ago = datetime.datetime.now() – relativedelta(years=5) Then simply update the date field as others have shown here.
This is not really django specific; more to do with databases. You add indexes on columns when you want to speed up searches on that column. Typically, only the primary key is indexed by the database. This means look ups using the primary key are optimized. If you do a lot of lookups on a … Read more
To update a subset of fields, you can use update_fields: survey.save(update_fields=[“active”]) The update_fields argument was added in Django 1.5. In earlier versions, you could use the update() method instead: Survey.objects.filter(pk=survey.pk).update(active=True)