Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Process a rather large python dictionary in Heroku?
I'm upserting roughly 17000 records after doing some calculations in pandas and my dictionary is way too large to be processed. To give you an idea, one record in the dictionary looks like this: [{'ticker': '24STOR', 'stock': '24Storage AB (publ)', 'exchange__exchange_code': 'ST', 'earnings_yield': Decimal('0.0000'), 'roic': Decimal('0.0000')}] Which I have transformed from a pandas dataframe: ranked_companies = df.to_dict(orient="records") # this is the dictionary with 17000 records I'm then looping through this: stocks_to_upsert = [] for company in ranked_companies: stocks_to_upsert.append( Stock( ticker=company["ticker"], stock=company["stock"], master_id=company["master_id"], exchange=Exchange.objects.get(exchange_code=company["exchange__exchange_code"]), earnings_yield=company["earnings_yield"], roic=company["roic"], roic_rank=company["roic_rank"], ey_rank=company["ey_rank"], sum_rank=company["sum_rank"], latest_report=datetime.strptime(company["latest_report"], "%Y-%m-%d").date() ) ) And then using a django package to bulk upsert these records: Stock.objects.bulk_update_or_create( stocks_to_upsert, ["ticker", "earnings_yield", "roic", "roic_rank", "ey_rank", "sum_rank", "latest_report"], match_field="master_id" ) I'm maxing out on my heroku memory and I somehow need to optimize the ranked_companies dictionary (I assume thats where the issue is). Everything is working fine If I limit the dictionary: df.head(100).to_dict(orient="records") # this executes just fine df.to_dict(orient="records") # this makes me run out of memory in my heroku logs # error looks like this: Error R14 (Memory quota exceeded) Any idea on how to proceed? -
counting number of file downloads in django
I have a link that lets the user download a file. something like this <a href="{{x.image.url}}" download="none">Get</a> Now I want to find a way to keep track of number of downloads. I have model like this with a downloads object to keep count of them. class Photo(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) image = models.ImageField(upload_to="home/images") downloads = models.PositiveIntegerField(default=0) def __str__(self): return str(self.id) So, I just want to know if there is a way to update the downloads object whenever a user presses the download link. Thankyou! -
How to create and update products to a REST API from Angular
I am new to and currently working on a Django and Angular webapp and I cant seem to be able to create and update my products from the angular web app and send them to the REST API to update the server. I have written a delete function and service request that works and I can retrieve the products from the API but I don't know how to write the functions for create products and update products. Would anyone be able to help me here? Here is what I have so far: api.service.ts import { Injectable } from '@angular/core'; import { HttpClient, HttpHeaders } from '@angular/common/http' import { Product } from './models/Product'; import { CookieService } from 'ngx-cookie-service'; import { Category } from './models/Category'; import { Shop } from './models/Shop'; import { Observable } from 'rxjs'; @Injectable({ providedIn: 'root' }) export class ApiService { baseUrl = 'http://127.0.0.1:8000/'; baseProductUrl = `${this.baseUrl}app/products/` baseCategoryUrl = `${this.baseUrl}app/categories/` baseShopUrl = `${this.baseUrl}app/shops/` headers = new HttpHeaders({ 'Content-Type': 'application/json', }); constructor( private httpClient: HttpClient, private cookieService: CookieService, ) { } /** Product CRUD */ /** ADD: add a product to the server */ addProduct(): Observable<Product> { const productUrl = `${this.baseProductUrl}` return this.httpClient.post<Product>(productUrl, {headers: this.getAuthHeaders()}) } /** DELETE: … -
Django Summernote wont get configured on pythonanywhere
I have installed django summernote locally with a virtualenv But on the server I havent created a virtualenv I am getting the following error on pythonanywhere: ModuleNotFoundError: No module named 'django_summernote' Summernote installs fine on pythonanywhere but I still get No module named 'django_summernote' I have included the 'django_summernote' in installed apps. What is the problem? -
Couldn't connect the list view to HTML file in Django
Couldn't connect the list view to HTML file in Django. models.py class AccountList(models.Model): name=models.CharField(max_length=200) paltform=models.CharField(max_length=200) username=models.CharField(max_length=200,null=True,blank=True) password=models.CharField(max_length=200) comments=models.TextField() def get_absolute_url(self): return reverse("accdetail",kwargs={'pk':self.pk}) def __str__(self): return self.name views.py class Account_list(ListView): model=AccountList def get_queryset(self): return AccountList.objects.all() AccountList_list.html {% extends 'PasswordSafe/base.html' %} {% block content %} <div class="centerstage"> {% for acc in acc_list %} <h1><a href="{% url 'accdetail' pk=post.pk %}">{{ acc.name }}</a></h1> {% endfor %} </div> {% endblock %} urls.py from django.conf.urls import url from . import views urlpatterns=[url(r'^$',views.HomePageView.as_view(),name="hmpage"), url(r'^accountlist/',views.Account_list.as_view(),name="acc_list"), url(r'^newsafe/',views.NewSafe.as_view(),name="new_safe"), url(r'^accdetail/(?P<pk>\d+)$',views.AccDetail.as_view(),name="accdetail"), url(r'^about/',views.AboutPage.as_view(),name="about")] I Don't Know where did i go wrong. -
why this statement 'This field is required.' keep displays in my webpage
my webpage why that statement keep displays in my webpage?,i created this webpage using django my html: <form method ='post'> {% csrf_token %} {{form}} <input type="submit" value = "Search"> </form> <form method="POST", enctype="multipart/form-data"> {% csrf_token %} {{forms}} <input type="submit" value="upload"> </form> forms.py from django import forms from .models import EbookModel class SearchForm(forms.Form): pdf_title=forms.CharField(label='pdf_title',max_length=100) class UploadPdf(forms.ModelForm): class Meta: model=EbookModel fields=('title','pdf',) -
KeyError when Deploying project to google cloud
I'm trying to deploy my django+postgreSQL project to GCP by following this guide https://codeburst.io/beginners-guide-to-deploying-a-django-postgresql-project-on-google-cloud-s-flexible-app-engine-e3357b601b91 I came to the point where I was going to write this command "./manage.py collectstatic" but I get a KeyError. I think it is because I'm not using conda but instead I'm using venv(?). This is my error: File "C:\Users\xxxxxxxx\settings.py", line 12, in DEBUG = os.environ['DEBUG'] =='True' File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.7_3.7.2544.0_x64__qbz5n2kfra8p0\lib\os.py", line 681, in getitem raise KeyError(key) from None KeyError: 'DEBUG' Is there a way to follow the guide without using conda? I'm very new to this so I'm sorry if my explaining is bad. -
How to avoid seeing sensitive user data in django admin page?
I have done a django web app in which users can input some data. I have created a super user and as the admin of the app I can see the data input by users which is fine for name and not sensitive data but I do not want to be able to see their sensitive data such as health data. I have used the encrypt module from django_cryptography.fields as follow: health_data = encrypt(models.IntegerField(default=140)) I figured out that if I am looking at the database from external script or simple DBbrowser, it works well as I cannot see the sensitive data. But I understood that this is seamless in the django admin: in django admin page it is decrypting before it is rendered. So I am ok with the encrypt data which is additional safety but this was not my first goal. I want to have the super user but I want that I am not able to see their sensitive data. Do you have any ideas ? I appreciate your time reading. -
How to properly use Django ORM Count with shopping cart
I have such models structure and I need to get information about how many pizzas was ordered. I did something like this PizzaOrder.objects.all().values('pizza').annotate(total=Count('pizza')). It works fine for orders where count in PizzaOrder equals to 1, but if count more than 1, it displays wrong count number. So I want somehow to connect Django Count with my field count in order to know how many pizzas was ordered. models.py class Pizza(models.Model): name = models.CharField(max_length=256) class Order(models.Model): order_number = models.IntegerField(primary_key=True) pizzas = models.ManyToManyField(to='Pizza', through='PizzaOrder') class PizzaOrder(models.Model): pizza = models.ForeignKey( to=Pizza, on_delete=models.PROTECT, ) order = models.ForeignKey( to=Order, on_delete=models.PROTECT, ) count = models.SmallIntegerField() -
Django Project validity
Is my Django based project valid if I don't use Django HTML and instead use simple HTML? Django would be used for the backend still. But for the frontend I am using plain HTML and bootstrap. So would it be valid? -
Django: Inline admin with 'detail'
I'm trying to build a Django App for fish farmer. Farmer(s) will access it using their smart phone. So readability will effected by phone's screen size (mostly 7") Here is my models.py from django.contrib.auth.models import User from django.db import models from django.conf import settings from django.db.models.signals import post_save from django.dispatch import receiver # Create your models here. class Pond(models.Model): name = models.CharField(max_length=20,) def __str__(self): return self.name class Food(models.Model): name = models.CharField(max_length=20,) def __str__(self): return self.name class Production(models.Model): pond = models.ForeignKey(Pond, on_delete=models.CASCADE, related_name='production',) qty= models.PositiveIntegerField(verbose_name='qty of fish', blank=True, null=True) weight = models.DecimalField(max_digits=7, decimal_places=2, verbose_name='Avg. Weight / fish', blank=True, null=True) date_created = models.DateTimeField( verbose_name=("Start Date"), auto_now_add=True, null=True, editable=False,) def __str__(self): return '{} {}'.format(self.pond self.date_created) @property def qty_n(self): return self.mortal.last().qty_n @property def sr(self): return (self.qty_n / self.qty) * 100 class Monitoring(models.Model): production=models.ForeignKey(Production, on_delete=models.CASCADE, related_name='monitoring',) date_created = models.DateTimeField( verbose_name=("Tanggal"), auto_now_add=True, null=True, editable=False,) prevmon=models.ForeignKey('self', on_delete=models.CASCADE, related_name='nextmon', editable=False, blank=True, null=True) dead= models.PositiveIntegerField(blank=True, null=True) food= models.ForeignKey(Food, on_delete=models.CASCADE, related_name='monitoring', blank=True, null=True) foodqty=models.PositiveIntegerField(blank=True, null=True, verbose_name='Food (grams)') ph = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True) weight = models.DecimalField(max_digits=7, decimal_places=2, verbose_name='Avg. Weight / fish', blank=True, null=True) @property def qty_n(self): # Current number of fishes if prevmon == None: qty_s = self.production.qty else : qty_s = self.prevmon.qty_n return qty_s-self.dead @property def t_dead(self): # Total … -
Django CORS denying access even though I've set CORS_ORIGIN_ALLOW_ALL = True
I've got 2 separate projects and project A needs to access some things from project B. I've already installed CORS on project B but accessing its resources from project A still raises the following error: Access to XMLHttpRequest at 'http://api.example.com/api/spotify/1IJxbEXfgiKuRx6oXMX87e' from origin 'http://127.0.0.1:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. CORS on project B is set to CORS_ORIGIN_ALLOW_ALL = True. Am I missing an additional setting or something? -
djangosaml2 authenticates user but i get anonymous user in my django view
I am using djangosaml2 to authenticate my users. i've been using it for a long time now with no problem. i am currently in the process of upgrading python and django to newer versions and the authentication does not work any more. Using the logs, i see that the authentication in djangosaml2 is successful butin my view, the request.user is anonymous user. Here are the working and none-working libraty versions that i use: Python: 2.7 --> 3.8 Django: 1.9 --> 1.11 djangosaml2: 0.17.2 (in both evns.) pysaml2: 4.0.5 --> 6.5.1 (tested also with 4.0.5) -
ModuleNotFoundError: No module named 'django_backend.todo'
Just started a fresh Django Rest framework project and I'm getting stuck on importing a view (TodoView) in urls.py This is a completely fresh project, all I did was add the model, view and now attempting to add the url. File "C:\Users\Simon\Documents\GitHub\tradingjournal\django_backend\django_backend\urls.py", line 20, in <module> from django_backend.todo.views import TodoView ModuleNotFoundError: No module named 'django_backend.todo' urls.py from django.contrib import admin from django.urls import path from rest_framework import routers from django_backend.todo.views import TodoView router = routers.DefaultRouter() router.register(r'todos', TodoView, 'todo') urlpatterns = [ path('admin/', admin.site.urls), ] views.py from django.shortcuts import render # Create your views here. from rest_framework import viewsets from django_backend.todo.models import Todo from django_backend.todo.serializers import TodoSerializer class TodoView(viewsets.ModelViewSet): serializer_class = TodoSerializer queryset = Todo.objects.all() settings.py """ Django settings for django_backend project. Generated by 'django-admin startproject' using Django 3.0.7. For more information on this file, see https://docs.djangoproject.com/en/3.0/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/3.0/ref/settings/ """ import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'n3^zfbj#c&i-9v^8q(%iox!kuy@gy2liy-1b+)q21-g&nwezf(' # SECURITY WARNING: don't run with debug turned on in production! DEBUG … -
ERP- tool to efficiently manage employees with projects - Django
I would like to efficiently schedule my staff on different projects. The employees have different capacities. One works full time, another part time. And the projects have different characteristics aswell. Goal: visualization of employees and projects in a MAtrix form. It is an ERP Does anyone have experience, approaches or a goal-oriented tutorial? I thought that many to many relationship might be a key, but i don´t get it. -
How to integrate CKEditor and Cloudinary in Django?
How to upload images in the Model with RichTextField to Cloudinary from the Django admin panel? -
"Django-insecure" in secret key in settings.py in django
After creating a new project with django-admin startproject my settings.py contain: SECRET_KEY = 'django-insecure <actual secret key>' I've never seen "django-insecure' before in a secret key. What does it mean? -
ImproperlyConfigured: Field name `titleweb_poster` is not valid for model `ContestDetail`
I have wriiten some code here. Please check out all the files. When I filled all the fields from django admin then it's working but when I redirect it with http://127.0.0.1:8000/contest/ then I am getting below error django.core.exceptions.ImproperlyConfigured: Field name `titleweb_poster` is not valid for model `ContestDetail`. Where I did wrong? How can I solve it? -
email already exists while updating user in django rest framework
I was updating the User when faced email already exists crossed my way. how ever i could use the extra_kwargs to overcome the email already exists or phone already exists issue. and then using from django.core.validators import EmailValidator in extra_kwargs as "email": {"validators": [EmailValidator]}.. bring back my email field validations. but that don't seems as a real solution. As we are just updating the existing model object.. why It's behaving like we are creating a new one. I think there should be something as defining update or partial_update or i don't know what ever.. is there any other solutions for this ?? class UserSerializer(serializers.ModelSerializer): class Meta: model = User fields = "id", "full_name", "gender", "phone", "email", "date_of_birth" extra_kwargs = { # "phone": {"validators": []}, # "email": {"validators": [EmailValidator]}, } -
Django dark mood
I'm using django i added dark mood to my website. But only for logged in users, is there a way to add it for anonymous users? Code: class Mood(models.Model): user = models.ForeginKey(User, null=True, blank=True, related_name=dark) Anf i check if it exists in template views function: dark = Mood.object.filter(user=request.user) Context={'dark':dark} Then I check for it i template {% if dark %} add css file for dark mood {%else%} Css light mood file {%endif%} Now the only way to add dark mood is for logged in users, is there another way to do it for anonymous ones? -
Django: How to implement transaction rollback in database if there is insertion in more than one model consecutively?
I am pretty new to Django and I have searched for this topic on the internet but I am not understanding. Basically, I am creating a student user which will be linked to the built-in User model in Django. In my code, I create the user in the user model first then I create it in the StudentUser model. However, if ever an error occurs, maybe the StudentUser was not created, I want to undo the changes in user. How to go around this with rollback? Here is my code snippet: views.py def user_signup(request): if request.method == 'POST': # Signup for student if(request.POST['stud_fname']): stud_fname = request.POST['stud_fname'] stud_lname = request.POST['stud_lname'] stud_uni = request.POST['stud_uni'] stud_email = request.POST['stud_email'] stud_password = request.POST['stud_passwd'] try: user = User.objects.create_user(first_name=stud_fname, last_name=stud_lname, username=stud_email, password=stud_password) StudentUser.objects.create(user=user, university=stud_uni, user_type="Student") messages.success(request, "Account successfully created. You can now log in") return redirect('login') except: messages.error(request, "Failed to create account. Try again!") return render(request, 'signup.html') else: return render(request, 'signup.html') -
Target WSGI cannot be loaded as Python module
How to solve these errors ? Thank you very much for your help. [Sat Apr 24 06:58:37.647922 2021] [:error] [pid 1430] [remote 172.31.12.184:248] mod_wsgi (pid=1430): Target WSGI script '/opt/python/current/app/greatkart/wsgi.py' cannot be loaded as Python module. [Sat Apr 24 06:58:37.648302 2021] [:error] [pid 1430] [remote 172.31.12.184:248] File "/opt/python/current/app/greatkart/settings.py", line 120, in [Sat Apr 24 06:58:37.648305 2021] [:error] [pid 1430] [remote 172.31.12.184:248] 'HOST': os.environ['RDS_HOSTNAME'], wsgi.py import os from django.core.wsgi import get_wsgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'greatkart.settings') application = get_wsgi_application() settings.py # Database Configuration if 'RDS_DB_NAME' in os.environ: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': os.environ['RDS_DB_NAME'], 'USER': os.environ['RDS_USERNAME'], 'PASSWORD': os.environ['RDS_PASSWORD'], 'HOST': os.environ['RDS_HOSTNAME'], 'PORT': os.environ['RDS_PORT'], } } requirements.txt asgiref==3.2.10 certifi==2020.12.5 chardet==4.0.0 Django==3.1 django-admin-honeypot==1.1.0 django-admin-thumbnails==0.2.5 django-session-timeout==0.1.0 idna==2.10 Pillow==8.2.0 psycopg2-binary==2.8.6 python-decouple==3.4 pytz==2020.1 requests==2.25.1 six==1.15.0 sqlparse==0.3.1 urllib3==1.26.3 db-migrate.config container_commands: 01_migrate: command: "django-admin.py migrate" leader_only: true option_settings: aws:elasticbeanstalk:application:environment: DJANGO_SETTINGS_MODULE: greatkart.settings django.config option_settings: "aws:elasticbeanstalk:container:python": WSGIPath: "greatkart/wsgi.py" "aws:elasticbeanstalk:container:python:staticfiles": "/static/": "static/" config.yml branch-defaults: main: environment: django-env-4 group_suffix: null environment-defaults: django-env: branch: null repository: null seast-kart-env: branch: null repository: null global: application_name: django-tutorial-4 branch: null default_ec2_keyname: aws-eb default_platform: Python 3.6 default_region: us-west-2 include_git_submodules: true instance_profile: null platform_name: null platform_version: null profile: eb-cli repository: null sc: git workspace_type: Application -
Optimize multiple Django Autocomplete Light Views to one View- Django
I am using django-autocomplete-light widget for loading data dynamically to display in the forms. I want to optimize below urls to one url and views classes to single view for all the models as most of the code (90%) is common across all the views, only model name and query is different. Here are my urls: path('locations/', views.LocationAutocomplete.as_view(), name='location-autocomplete'), path('societies/', views.SocietyAutocomplete.as_view(), name='society-autocomplete'), path('propcats/', views.PropertyCategoryAutocomplete.as_view(), name='propcat-autocomplete'), ... path('projects/', views.ProjectAutocomplete.as_view(), name='project-autocomplete'), Here are its Views: class PropertyCategoryAutocomplete(autocomplete.Select2QuerySetView): def get_queryset(self): if not self.request.user.is_authenticated: return PropertyCategory.objects.none() enquiry_flag = self.request.session.get('enquiry_flag', 3) qs = PropertyCategory.objects.filter(type__enq_code = enquiry_flag) if self.q: qs = qs.filter(name__istartswith=self.q) return qs class LocationAutocomplete(autocomplete.Select2QuerySetView): def get_queryset(self): if not self.request.user.is_authenticated: return Location.objects.none() qs = Location.objects.all() if self.q: qs = qs.filter(name__istartswith=self.q) return qs class FloorAutocomplete(autocomplete.Select2QuerySetView): def get_queryset(self): if not self.request.user.is_authenticated: return Floor.objects.none() qs = Floor.objects.all() if self.q: qs = qs.filter(floor__istartswith=self.q) return qs class ProjectAutocomplete(autocomplete.Select2QuerySetView): def get_queryset(self): if not self.request.user.is_authenticated: return Project.objects.none() qs = Project.objects.all() if self.q: qs = qs.filter(name__istartswith=self.q) return qs class SocietyAutocomplete(autocomplete.Select2QuerySetView): def get_queryset(self): if not self.request.user.is_authenticated: return Society.objects.none() preferred_area = self.forwarded.get('preferred_area', None) qs = Society.objects.filter(location_id__in=preferred_area) if self.q: qs = qs.filter(name__istartswith=self.q) return qs I was trying to something like this, but not sure how can I pass other relevant parameters such as query parameters, model … -
I want value instead of id in django after left join
Hi i have three tables in models.py and i perform Left Join. When i render data in template after join i get job_no as id instead of value. Can u help me to get the original value instead of id. class Job(models.Model): job_no = models.CharField(max_length=200) class RecievableInvoice(models.Model): invoice_no = models.CharField(max_length=200) invoice_amount = models.IntegerField() job_no = models.ForeignKey(Job,on_delete=models.CASCADE) class PayableInvoice(models.Model): invoice_no = models.CharField(max_length=200) invoice_amount = models.IntegerField() job_no = models.ForeignKey(Job,on_delete=models.CASCADE) And here is my query cursor.execute(''' Select RecievableInvoice.job_no,RecievableInvoice.invoice_no,RecievableInvoice.invoice_amount, PayableInvoice.invoice_no,PayableInvoice.invoice_amount, FROM RecievableInvoice LEFT JOIN PayableInvoice ON RecievableInvoice.job_no = PayableInvoice.job_no''') Screenshot Attached -
How do I keep the current user for a child form from a formset?
I don't fully understand the logic. In my model, the attachment has an author, and the post also has an author. But, I need to save two current users in Post and Attachment. But only the current user is saved to the form, and not saved to the formset. class PostCreateView(CreateView): model = Post template_name = 'modules/post/post_create.html' form_class = PostForm success_url = None def get_context_data(self, **kwargs): data = super(PostCreateView, self).get_context_data(**kwargs) if self.request.POST: data['formset'] = PostAttachmentFormSet(self.request.POST) else: data['formset'] = PostAttachmentFormSet() return data def form_valid(self, form): context = self.get_context_data() formset = context['formset'] with transaction.atomic(): form.instance.created_by = self.request.user formset.instance.created_by = self.request.user #not working... self.object = form.save(commit=False) form.save_m2m() if formset.is_valid(): formset.instance = self.object formset.save() return super(PostCreateView, self).form_valid(form) def get_success_url(self): return reverse_lazy('main')