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
5
votes
2 answers

In Django, can I specify database when creating an object?

Look at this Django ORM code: my_instance = MyModel() my_instance.some_related_object = OtherModel.objects.using('other_db').get(id) At this point, in the second line, Django will throw an error: ValueError: Cannont assign "":…
mnowotka
  • 16,430
  • 18
  • 88
  • 134
5
votes
1 answer

How to introduce unique together with conflicting data?

I worte a little qna script for my website and to prevent users from starting a discussion I want every user only to be able to reply once. class Q(models.Model): text = models.TextField() user = models.ForeignKeyField('auth.User') class…
JasonTS
  • 2,479
  • 4
  • 32
  • 48
5
votes
1 answer

Django: how to wrap a bulk update/insert operation in transaction?

This is my use case: I have multiple celery tasks that run in parallel Each task could Bulk create or update many objects. For this I'm using django-bulk So basically I'm using a very convenient function insert_or_update_many: it first performs…
Leonardo
  • 4,046
  • 5
  • 44
  • 85
5
votes
1 answer

How to create a Django custom Field to store MYSQL DATETIME(6) and enable fractional seconds (milliseconds and or microseconds) in Django/MySQL?

MySQL 5.6.4 and up expands fractional seconds support for TIME, DATETIME, and TIMESTAMP values, with up to microseconds (6 digits) precision: http://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html Django 1.5 and up supports fractional…
allcaps
  • 10,945
  • 1
  • 33
  • 54
5
votes
3 answers

Is there any more elegant way to add a value sensitive unique together constraint in Django Model?

Here is the problem: I have a model like this: class UserBook(models.Model): user = models.ForeignKey(User) book = models.ForeignKey(Book) is_active = models.BooleanField(default=False) class Meta: unique_together = ("user",…
Johnny Zhao
  • 2,858
  • 2
  • 29
  • 26
4
votes
2 answers

Django: write something to the database in a catch block, when using an atomic transaction

I have a Django REST Framework serializer which uses select_for_update in combination with atomic transitions, like this: https://docs.djangoproject.com/en/4.2/ref/models/querysets/#select-for-update/. That works fine, except that I want to write…
Kevin Renskers
  • 5,156
  • 4
  • 47
  • 95
4
votes
1 answer

Django unique_together on postgres: enforced by ORM or DB?

As I look at the sqlall for a models.py that contains unique_together statements, I don't notice anything that looks like enforcement. In my mind, I can imagine that this knowledge might help the database optimize a query, like so: "I have already…
jMyles
  • 11,772
  • 6
  • 42
  • 56
4
votes
1 answer

Django Microservices authentication

I was reading about Microservices in django , and came to know in Microservices we keep small services separately and they can operate individually . if I am not wrong about this concept how I will validate using JWT token from a user from one…
ephemeral
  • 429
  • 2
  • 7
  • 16
4
votes
1 answer

How to filter objects at django based on related field?

I have models: Product Store ProductStore (additional table with foreign keys to Store and Product, also Boolean 'enabled', and stock(integer) ) The questions: How can I filter Products which has Enabled=True for current store__id (from…
Taras
  • 447
  • 2
  • 7
  • 18
4
votes
1 answer

Return object when aggregating grouped fields in Django

Assuming the following example model: # models.py class event(models.Model): location = models.CharField(max_length=10) type = models.CharField(max_length=10) date = models.DateTimeField() attendance = models.IntegerField() I want…
Demetris
  • 2,981
  • 2
  • 25
  • 33
4
votes
1 answer

Count same field values in Django queryset

I have a Django model with three fields: product, condition and quantity with data such as: | Product | Condition | Quantity | +---------+-----------+----------+ | A | new | 2 | | A | new | 3 | | A | new …
msampaio
  • 3,394
  • 6
  • 33
  • 53
4
votes
1 answer

Django channels 2, accessing db in tests

I recently updated my project to Django 2 and channels 2. Right now I am trying to rewrite my tests for chat app. I am facing a problem with tests that depend on django db mark from pytest-django. I tried to create objects in fixtures, setup…
Quba
  • 4,776
  • 7
  • 34
  • 60
4
votes
1 answer

Query still running for django_migrations

I'm developing a django web app and i have notice something strange. The following query will stay in execution in the DB SELECT "django_migrations"."app", "django_migrations"."name" FROM "django_migrations" here the example from: select…
Mattia
  • 961
  • 13
  • 25
4
votes
2 answers

Django / Cassandra: can not createsuperuser

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:
karelok
  • 301
  • 2
  • 9
4
votes
3 answers

Django testing commit_on_success

I have a function that I've got wrapped in @transaction.commit_on_success and running Django unit tests on it. The function is too long to paste, but some pseudocode is: @transaction.commit_on_success def func(): order = Order.create() …
grokpot
  • 1,462
  • 20
  • 26