Django.fun

Questions & Answers about Django, page 587

18.08.2021
1
answers

Django aggregate sum on child model field

Consider the following models: from django.db import models from django.db.models import Sum from decimal import * class Supply(models.Model): """Addition of new batches to stock""" bottles_number = models.PositiveSmallIntegerField( bottles_remaining = models.DecimalField(max_digits=4, decimal_places=1, default=0.0) def remain(self, *args, **kwargs): used = Pick.objects.filter(supply=self).aggregate( total=Sum(Pick.n_bottles))[bottles_used__sum] …

18.08.2021
1
answers

Stripe 'card-element' is not displaying. Why?

Completely losing my mind with this... I am creating a ecommerce website with django. I use Stripe as Payment Gateway Provider. I don't understand ...Stripe 'card-element' is not displaying. Why ? For information, the card-element used to be displayed correclty …

18.08.2021
1
answers

How do i relate model X to an instance of model X?

Suppose i have class modelX(models.Model): value = models.CharField(max_length=200) now var1 = modelX(value='some_value') var2 = modelX(value='another_value') How do i connect var1 and var2 ? I know about .objects.all method but thats not what i want. I want something like …

18.08.2021
2
answers

How to handle a post request in json format to own laptop in python via django or flask

I have configured an http webhook on thethingsstack webpage cloud.thethings.network that performs a post request to a configured endpoint (my laptop) as follows: http://xxx.ngrok.io -> http://localhost:80 For uplink message: http://localhost:80/uplink-message For join ack: http://localhost:80/join-accept Using ngrok.exe http 80 I …

18.08.2021
0
answers

Django db block for migration with no schema change

When updating the helptext of field of a Django model, it produces a migration to track this change. This operation doesn't perform any sql query (checked with manage.py sqlmigrate myapp). However, does it still lock the table like normal schema …

18.08.2021
2
answers

How to Issue and Return a book in Library Managament System

1st Problem - What I want is that when a user issue a book the quantity must be decreased by 1 and once there is no book user should not be able to issue the book and this should be …

18.08.2021
0
answers

Login not working in django when using rewrites from firebase hosting to cloud run

Current Setup: I've got a Django application behind gunicorn running on Cloud Run. Since the region it is deployed in does not support Custom Domains, I have a firebase hosting setup with the following code: { "hosting": { "ignore": [ …

18.08.2021
0
answers

How to display python variable to Django UI

I'm trying to subscribe to a MQTT topic called Wind. After that I want to show the value in my Django chart. But whenever I return the result of my MQTT value with test it showed me name 'test' is …

18.08.2021
0
answers

Django unable to persisted cached array

Django cannot persist my cached data even if I set timeout to none. My settings.py contains this: CACHES = { 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', 'LOCATION': 'django_cache', #'/var/tmp/django_cache', 'TIMEOUT': None, } } I save data with this line of code: …

18.08.2021
1
answers

Use context variable to compare with forloop.revcounter in Django

I can not use context variable to compare with forloop.counter or forloop.revcounter. No error returning. But, every time if statement become false. here is my view func- def view_single_server(request, pk): server = PhysicalServer.objects.get(pk=pk) context = { 'server' : server, 'n':range(42) …

18.08.2021
1
answers

How can i get to filter talent , talent2 both using django filter

views.py def get_ajax(self, request, *args, **kwargs): talent= request.GET.get('talent', None) users = User.objects.all().order_by('last_name') if talent: user = users.filter(user_talent=talent) and users.filter(user_talent2=talent) models.py def get_talent(self): talent = f'' if self.user_talent: talent = f'{self.user_talent}' if self.user_talent2: talent = f'{self.user_talent}, {self.user_talent2}' return talent

18.08.2021
0
answers

Not able to connect to websocket using Nginx and Uvicorn

I built a docker container with Django, Uvicorn, Nginx and Redis, and am using django-channels but when I run this it says it cannot connect to the websocket and this is seen in the browser console: WebSocket connection to 'ws://127.0.0.1:8080/ws/notifications/' …

18.08.2021
0
answers

How to make datetime already saved in database timezone aware in a Django project

We had a couple of timezone unaware datetime in model definition which resulted in saving a number of entries that were naive datetimes. The default was set to be utcnow like this time_sent = models.DateTimeField(default=datetime.utcnow, blank=True) And we got …

18.08.2021
0
answers

Django||Python :: ValueError: bad marshal data (unknown type code)

I have tried creating a login/register Template that I can use to skip the login/register setup phase every time I need to create a new Django project. However, when I copy the code over and rename the file (and change …

18.08.2021
1
answers

Saving django parent and child form

I have a page where users enter details that relate to two models - that representing a patient, and another representing medications taken by that patient. I have designed the interface so that multiple medications can be added dynamically by …

18.08.2021
0
answers

Django error after install Bootstrap4 with pip

I just installed bootstrap4 using pip and then I execute runserver and this is what I got. I've been looking for solutions and now I´m here asking for your help please Watching for file changes with StatReloader Exception in thread …

18.08.2021
0
answers

Routing access to custom user model through database router in django

Consider following situation: You have multiple python/django applications, which share a common user database. You implemented the custom user model in a separate django-app called 'benutzer', which is imported into every applications project directory. Now simply routing access to the …

18.08.2021
1
answers

How to perform conditional aggregation on a Django model's one to many related field?

Given the following model schemas: class Transaction(models.Model): wallet = models.ForeignKey(related_name="transactions") amount = models.DecimalField() # must be positive value type = models.CharField(choices=[("deposit", "deposit"), ("withdrawal", "withdrawal")] class Wallet(models.Model): pass What is the most efficient Django ORM query to obtain the balances …

18.08.2021
0
answers

Django - How to handle unmanaged models during tests

In my Django project I am using 3 different databases, of which 2 are pre-populated with data (legacy databases), thus many of my models are not managed by Django managed = False. When I run tests Django throws django.db.utils.OperationalError: no …

18.08.2021
1
answers

Field 'id' expected a number but got (something with Id)

Django 3.2.6 class FeedbackForm(CreatedMixin, CommentMixin, FlagMixin, models.Model): name = models.CharField(blank=False, default="", null=False, unique=False, max_length=500, verbose_name=gettext("Name:")) email = models.EmailField(blank=True, default="", null=False, unique=False, verbose_name=gettext("Email:")) phone = PhoneField(blank=True, default="", null=False, unique=False, verbose_name=gettext("Phone:")) message = models.TextField(blank=False, default="", null=False, verbose_name=gettext("Message:")) def __str__(self): return f"{self.name}_{self.email}_{self.phone}" class …

1 2 585 586 587 588 589 638 639