django-database

Django Get Last Object for each Value in List

Django Get Last Object for each Value in List Question: I have a model called Purchase, with two fields, User and amount_spent. This is models.py: class Purchase(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) amount_spent = models.IntegerField() created_at = models.DateTimeField(auto_now_add=True) I want to get the last purchases from a list of users. On views.py I have a list …

Total answers: 2

python AttributeError 'dict' object has no attribute

python AttributeError 'dict' object has no attribute Question: I get following error: AttributeError at /filterrule/createresultset/ ‘dict’ object has no attribute ‘filter_query_json’ Here’s the code: from django.db import connections def fetch_filter_rule_sql_from_ui_db(filter_rule_id): with connections[‘frontend’].cursor() as cursor: query = “SELECT id, filter_query_json FROM dmf_filter_rules WHERE id=%s LIMIT 1” cursor.execute(query, [filter_rule_id]) filter_rule_sql_json = dictfetchall(cursor)[0].filter_query_json # filter_rule_sql_json = dictfetchall(cursor)[0][filter_query_json] return …

Total answers: 2

Simple Subquery with OuterRef

Simple Subquery with OuterRef Question: 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 = models.CharField(max_length=120) def __str__(self): return self.name class Post(models.Model): title = models.CharField(max_length=120) …

Total answers: 2

Django / Cassandra: can not createsuperuser

Django / Cassandra: can not createsuperuser Question: I am running Debian server with Django and Cassandra. I am not able to create the admin user via command: python manage.py createsuperuser Running the command causes an error: cassandra.protocol.SyntaxException: <Error from server: code=2000 [Syntax error in CQL query] message=”line 1:260 no viable alternative at input ‘.’ (…auth_user.date_joined …

Total answers: 2

Django – Rollback save with transaction atomic

Django – Rollback save with transaction atomic Question: 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: some_object = SomeModel(…) some_object.save() if something: raise exception.NotAcceptable() # …

Total answers: 3

django database delete specific number of entries

django database delete specific number of entries Question: 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 ‘offset’ with delete. How can I specify the number of entries to be deleted. Asked By: arjun || …

Total answers: 2

How to log all sql queries in Django?

How to log all sql queries in Django? Question: 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 connection connection.queries to …

Total answers: 9

Django Table with Million of rows

Django Table with Million of rows Question: I have a project with 2 applications ( books and reader ). Books application has a table with 4 milions of rows with this fields: book_title = models.CharField(max_length=40) book_description = models.CharField(max_length=400) To avoid to query the database with 4 milions of rows, I am thinking to divide it …

Total answers: 7