Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
MySQL handling large queries
I'm using MySQL + Django and I'm downloading and updating data in my database. I have a few large queries which they sometimes fail -> "MySQL server has gone away" The query is such SELECT * FROM XYZ_table WHERE id IN (list with about 10-40K values) What are my possibilities here? Should I split the query and then concatenate the separate lists? Or is there a MySQL config value I can increase? I have max_packet_size set at 512M -
Django doesn't recive pre save signal
I'm writing DRF appliaction and I have problem which I can't understand. I'm trying to send request when my model changes. Here is my model: class Sensors(models.Model): CATEGORIES = [ ('temperature', 'temperature'), ('humidity', 'humidity'), ] id = models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID') name = models.CharField(max_length=30, unique=True) category = models.CharField(max_length=15, choices=CATEGORIES) battery_level = models.IntegerField(blank=True, null=True) notifications = models.BooleanField(default=True) is_active = models.BooleanField(default=True) frequency = models.IntegerField(default=300) ip_address = models.GenericIPAddressField(blank=True, null=True) class Meta: verbose_name = "Sensor" verbose_name_plural = "Sensors" def __str__(self): return str(self.id) And here is my signal.py: from django.db.models.signals import pre_save from django.dispatch import receiver from sensors.models import Sensors import requests @receiver(pre_save, sender=Sensors) def do_something_if_changed(sender, instance, **kwargs): try: sensor = Sensors.objects.get(pk=instance.pk) except Sensors.DoesNotExist: pass # Object is new, so field hasn't technically changed, but you may want to do something else here. else: if not sensor.frequency == instance.frequency: # Field has changed # do something data_for_sensor = { 'id': sensor.id, 'frequency': instance.frequency } response = requests.post('http://192.168.1.21:8000/receive', data=data_for_sensor) response.raise_for_status() Generally post is not sending and I cannot find why -
How to implement a custom search remote data for each column on material-tale in React
I was wondering if it is possible to implement a custom search row for each column in my material-table data in React ? I would like to have a function which calls my Django Rest api's search function with the data submitted in my search filter in material-table and then display only the matched data. Based on the material-table docs I tried to implement customFilterAndSearch and pass the term to a custom method which calls my Rest api with the search term but the customFilterAndSearch access the method multiple times. Actually I get to a point where there axios get method get called by the number of rows items I have in my table. Here is the custom customFilterAndSearch call: customFilterAndSearch: (term, rowData) => this.getDataFilterNomService(term, rowData) }, Here is my custom method I used: async getDataFilterNomService(term, rowData){ console.log("TermDinFilter", term) //console.log("rowDataDinFilter", rowData) try{ let response = await axiosInstance.get('get/servicecatalog/filterNomService/', { params: { "nom_service":term } }); console.log("Response la Filtru NomService", response) } catch(error){ console.log("Error: ", JSON.stringify(error, null, 4)); throw error; } } And here is how django gets called when I try to search for a name like : "Viorel" backend_1 | [11/Jun/2020 16:49:02] "GET /api/get/servicecatalog/filterNomService/?nom_service=Viorel HTTP/1.1" 200 968 backend_1 | [11/Jun/2020 16:49:03] … -
Integration of a second application into Django project
I have two separate working Django projects. Both consist of one application. Both have own data base. Now I am trying to join them. I took the first project as the base, copy+paste the folder with the second application on the same level with first one. I have added ane integration record into main file urls.py path('rsf/', include('rsf.urls')), and a record into INSTALLED_APPS. Then at start, the second application raised errors, because its old database was not reacheable. I have commented nearly everything in the second application and left its models.py file only. Then I tried to make migrations. Django does not see the new models, belonging to the second app, which must be created from the new models.py file and says that there is nothing to migrate. No mistakes here are raised. Please help. Thank you. ael - is the first app. rsf - is the second app from django.contrib import admin from django.urls import path, include from django.conf.urls.i18n import i18n_patterns from django.contrib.sitemaps.views import sitemap from ael.sitemaps import StaticViewSitemap, CrossRefTableSitemap from ael import views as ael_views from django.contrib.sitemaps import views as sitemaps_views from django.views.decorators.cache import cache_page sitemaps = { 'static': StaticViewSitemap, 'crossreftable': CrossRefTableSitemap, } urlpatterns = [ path('admin/', admin.site.urls), … -
Django Check for duplicates with 2 model fields
I have a model that looks like this: class UssdCode(models.Model): title = models.CharField(max_length=100) code = models.CharField(max_length=100) product = models.CharField(max_length=100) How can I get the admin to alert me and reject my entry when I try to add a new object that has the same 'code' and 'product' as an object already in the database. -
How to connect your data base to your pycharm html files
I am wondering on to how to connect my pycharm html file to a database and display it on the html webpage for the material I have been using online have not been working towards the connection on the database to the html? any supplementary material that can be provided will help as well. -
Include chatbox in all views Django
I have created a chat in Django using django-private-chat package which uses Websockets. I want to have a chat that is displayed in every page of the website allowing client asking questions to staff members. My problem is that I have created a chat button in the base.html in order to be able to chat in every views. So I include the view users_list.html (view where we have all the users) and when I click on chat with someone, my dialog page (which is the chat page) appears but it is empty. I wanted to use the context_processor.py like I used it for user_list.html to have all the users but I don't understand how to do it for this. In base.html {% if user.is_authenticated %} {% block chat %} <div id="notification-btn" class="notification-button"> <button class="button-base" onclick="displayChooseUser()"> <i class="fas fa-comment icon-chat"></i> </button> </div> <div id="chooseUser"> {% include "../django_private_chat/user_list.html" %} </div> {% endblock %} {% endif %} In the user_list.html {% for user in users %} {% if user != request.user %} <div class="chat_list active_chat"> <div class="chat_people"> <div class="chat_img"> <img src="https://ptetutorials.com/images/user-profile.png" alt="sunil" class="avatar"> </div> <div class="chat_ib"> <h5> <p onclick="chatShow()"> Chat with {{user}}</span> <div class="test" style="display:none;"> {% url 'dialogs_detail' user as dialogs %} {% … -
I want to load the default image of the user in if the user has not uploaded the pic. But its not showing the default pic of the user
I want load the default pic of the user but its not showing any image and after submiting the pic its showing RelatedObjectDoesNotExist. User has no attribute userprofile. models.py from django.db import models from django.contrib.auth.models import User class userprofile(models.Model): user = models.OneToOneField(User,on_delete = models.CASCADE) profilepic = models.ImageField(default='pp.png',upload_to='profile_pic',blank = True) forms.py from django import forms from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm from .models import userprofile class ProfileUpdateForm(forms.ModelForm): class Meta: model = userprofile fields = ['profilepic',] views.py @login_required def profile(request): if request.method == 'POST': p_form = ProfileUpdateForm(request.POST,request.FILES,instance=request.user.userprofile) if p_form.is_valid(): p_form.save() return render(request,'profile.html') else: p_form = ProfileUpdateForm(instance=request.user.user) context = { 'p_form': p_form } return render(request,'profile.html',context) profile.html <form method ="POST" class="sm:w-1/3 text-center sm:pr-8 sm:py-8" enctype="multipart/form-data" > {% csrf_token %} <img id="profile_pic" alt="team" class="flex-shrink-0 rounded-lg w-full h-56 object-cover object-center mb-4" src="{{user.userprofile.profilepic.url}}"> {{ p_form|crispy }} <br> <input id="file-input" style="padding: 8px 93px;" class="text-white bg-green-500 border-0 py-2 px-8 focus:outline-none hover:bg-green-700 rounded text-lg" type="submit" value="Upload"> </form> -
Django import export edit queryset before export
I'm trying to calculate the pending amount via models and export result in the csv. But the csv shows an empty coloumn for amountpending ''''class FinancePendingResource(resources.ModelResource): invoiceNumber = Field(attribute='invoiceNumber', column_name='Invoice Number') student = Field(attribute='student', column_name='Student') Schedule = Field(attribute='Schedule', column_name='Schedule') TotalAmount = Field(attribute='TotalAmount', column_name='Total Value(PKR ₨)') issueDate = Field(attribute='issueDate', column_name='Issue Date') dueDate = Field(attribute='dueDate', column_name='Due Date') amountPaid = Field(attribute='amountPaid', column_name='Amount Paid (PKR ₨)') class Meta: model = FinancePending import_id_fields = ('invoiceNumber',) fields = ('invoiceNumber', 'student', 'amountPaid', 'issueDate', 'dueDate', 'Schedule', 'TotalAmount', 'AmountPending',) exclude = ('id',) skip_unchanged = True report_skipped = True def before_export(self, queryset, *args, **kwargs): amount_paid = FinancePending.objects.values_list('amountPaid', flat=True) amount_paid = list(amount_paid) total_amount = FinancePending.objects.values_list('TotalAmount', flat=True) total_amount = list(total_amount) # total - paid TotalFee = [float(s.replace(',', '')) for s in total_amount] AmountPaid = [float(s.replace(',', '')) for s in amount_paid] def Diff(li1, li2): return (list(set(li1) - set(li2))) amount_pending = Diff(TotalFee, AmountPaid) finance_pending = FinancePending() i = 1 while i <= len(amount_pending): FinancePending.objects.filter(invoiceNumber=i).update(AmountPending=str(amount_pending[i])) i = i + 1 queryset.refresh_from_db() -
Update Location.PointField in real time whenever the latitude or longitude values are changed
I looked at Romas post (GeoDjango: Can't save a model with PointField(), Error: SpatialProxy (POINT) with value of type: <class 'users.models.Point'>) and copied his idea. I calculate Location by doing self.Location = Point(self.Longitude, self.Latitude). The problem with this approach is that the location is only updated when I click save (at the moment I am modifying the table via the admin page and not the front end). Is there a way of getting the Location value to be updated in real time? class Property(models.Model): id = models.AutoField(primary_key=True) User = models.ForeignKey(User, on_delete=models.CASCADE) Latitude = models.FloatField(blank=True, null=True, verbose_name='Latitude', default="00.0000000") Longitude = models.FloatField(blank=True, null=True, verbose_name='Longitude', default="00.0000000") Location = models.PointField(blank = True, null=True, srid=4326) City = models.CharField(max_length=50, default="") Street = models.CharField(max_length=60, default="") PostCode = models.CharField(max_length=60, default="") Residential = models.CharField(choices=NO_OF_HRS, max_length=60, default='1') def save(self, *args, **kwargs): self.Location = Point(self.Longitude, self.Latitude) super(Property, self).save(*args, **kwargs) -
Convert file extension to lowercase on upload wagtail
i have a problem while uploading images to wagtail admin panel. The issue is that if i upload a file with a capitalized extension like .JPG the file brakes. Is there a way of converting the format to lowercase on upload? -
Building a Django App without Default Auth and User and Use Admin
I am working on a application (Using Django and Django Rest Framewok) with has 3 services User Management Service Notification Service Payments Service Since i am handling User and Authentication in User Management Service itself (Using Token Based Authentication), so i don't need the default Authentication and User provided by django in other two services. In Notification service i have added a Custom Middle-ware which validates the token from User Management Service. How can i get rid of default User and Auth but still use the django Admin interface? I tried removing auth middle ware but getting error that admin requires that (for obvious reason). -
Fetch AWS lambda generated thumbnail in django template
I am trying to make a gallery page in django where thumbnails of images are dislayed but by clicking it the full size image open. For this purpose, I have an Image model with a ImageField where the full size image is stored: models.py class Image(models.Model): title = models.CharField(max_length=200) date = models.DateField(default=datetime.now) image = models.ImageField(upload_to='gallery_images/') When the file is uploaded to s3, it triggers a lambda function that create a thumbnail and save it in a subdirectory gallery_images/thumbnails/. My question is how can I fetch the thumbnail to display it in the template? The exact location in s3 is known but I need to get a URL that, I think, contains a unique id for s3 to authenticate me. -
Django - HELP!! Media images won't appear on the template screen
I need help with making images uploaded by users to appear on templates. I've been doing every solutions that I could find, but they all left me with nothing but broken images on the screen. The framework I'm currently using is Django and here are the codes consisting the program. I can upload other codes as well if requested. 'avatar' is the name for the profile image. settings.py MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') views.py def signup(request): if request.user.is_authenticated: return redirect('movies:home') if request.method == 'POST': form = CustomUserCreationForm(request.POST, request.FILES) if form.is_valid(): form.save() return redirect('movies:home') else: form = CustomUserCreationForm() context = { 'form':form, } return render(request,'accounts/forms.html',context) models.py class User(AbstractUser): nickname = models.CharField(max_length=30, unique=True, error_messages={'unique':"Sorry! This nickname is already in use TT"}) email = models.EmailField(verbose_name='email',max_length=255, unique=True, error_messages={'unique':"This email is already in use XD"}) description = models.CharField(max_length=40, blank=True) avatar = models.ImageField(upload_to = 'images', blank=True) profile.html => this is the template where users can see their own profiles as well as others' If a user had not uploaded any images to be set as their profile image, an image file that I had prepared in a static folder is set as their default image {% extends 'base.html' %} {% load static %} {% … -
Persistent Django's postresql database in Docker
I am learning Django by trying to create the polling app described on the Django's website. At the same time, I am using Docker for the first time and I have encountered problems trying to make PostgreSQL database, used by this app, persistent. According to the tips on using Docker with Django, found on the Docker website, I am running two services with docker-compose: one handling the database and the other the app itself (at least that's how I understand it). I have tried mounting a volume at the /var/lib directory inside the database container, where I found PostgreSQL directory. Although this directory became persistent, which I checked by creating some dummy files, migrations done via the web service are being erased every time I restart those containers. Below are my docker-copmpose.yml file and Django's settings.py. What should I do to make this database persistent? #docker-compose.yml version: '3' services: db: image: postgres environment: - POSTGRES_DB=postgres - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres volumes: - .:/code - .:/var/lib web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/code ports: - "8000:8000" depends_on: - db #(...) # Database # https://docs.djangoproject.com/en/2.2/ref/settings/#databases DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'postgres', 'USER': 'postgres', 'PASSWORD': 'postgres', 'HOST': … -
Django refresh_from_db in transaction
I'm dealing with django legacy project, there is some usage of refresh_from_db method, inside of atomic transaction clause: with transaction.atomic(): self.refresh_from_db() The question is - will the refresh_from_db take existing values in DB? i.e ignoring the transaction and updating the instance? If so its kind of dangerous doing this kind of move -
Search Bar Bringing A Blank Page
I am trying to build a search bar and this is the code in my view.py ''' def searchjobs(request): if request.method == 'GET': query = request.GET.get('q') submitbutton = request.GET.get('submit') if query is not None: lookups = Q(title_of_job__icontains = query) | Q(work_type__icontains = query) | Q(location__icontains = query) | Q(industry__icontains = query) | Q(job_function__icontains = query) results = Job.objects.filter(lookups).distinct() context = {'results': results, 'submitbutton': submitbutton} return render(request, 'job/search.html', context) else: return render(request, 'job/search.html') else: return render(request, 'job/search.html') ''' then for my search.html template, this is what I have ''' {% if submitbutton == 'Search' and request.GET.q != "" %} {% if results %} Results for {{request.GET.q}} {% for result in results %} <div class="container"> <div class="card flex-row" style="box-shadow: 5px 5px lightgrey;"> <div> <img class="rounded-circle card-img" src="{% static 'images/briefcase.png' %}"> </div> <div class="card-body"> <h4><a href="{% url 'job-detail' job.id %}" class="card-link">{{ job.title_of_job }}</a></h4> <h5><a href="" class="card-link">{{ job.publisher }}</a></h5> <h5 class="card-text">{{ job.location }} | {{ job.work_type }}</h5> <h5><a href="" class="card-link">{{ job.job_function }}</a></h5> </div> </div> </div> {% endfor %} {% endif %} {% endif %}''' when I run it in my browser, all I get is a blank page, and not the things that is being put in the search bar. Please I will like … -
Django Channels: URLRouter can't take list as argument
I am working on a project that requires me to use 2 WebSockets with 2 different Consumers but when I try to feed URLRouter with a list it says the list has no attribute 'callback' from channels.routing import ProtocolTypeRouter, URLRouter from channels.auth import AuthMiddlewareStack import chat_app.routing, startupapp.routing application = ProtocolTypeRouter({ 'websocket': AuthMiddlewareStack( URLRouter([ chat_app.routing.websocket_urlpatterns, startupapp.routing.websocket_urlpatterns ]) ) }) Is there a solution or any other way to achieve the same? -
Which one is more convenient to create django form, CreateView or forms.ModelForm
I am very beginner in django. I want to create a post form which be able to have title, content, image/file upload and etc. I am very confused with concept of modelforms and createview. I tried this: blog/view.py: class PostCreateView(LoginRequiredMixin, CreateView): model = Post fields = ['title', 'content', 'imagefile'] success_url = '/blog/home/' # template_name = 'blog/post_form.html' def __init__(self, *args, **kwargs): super(PostCreateView, self).__init__(**kwargs) # Call to ModelForm constructor def form_valid(self, form): form.instance.author = self.request.user form.save() return super().form_valid(form) blog/templates/blog/post_form.html: {% extends "blog/base.html" %} {% load crispy_forms_tags %} {% block content %} <div class="content-section"> <form method="POST"> {% csrf_token %} <fieldset class="form-group"> <legend class="border-bottom mb-4">Post::</legend> {{ form|crispy }} <img src='{{ post.imagefile.url }}'> <br><br> </fieldset> <div class="form-group"> <button class="btn btn-outline-info" type="submit">Update</button> </div> </form> </div> {% endblock content %} blog/urls.py: from django.urls import path from .views import ( PostCreateView, ) urlpatterns = [ path('blog/post/new/', PostCreateView.as_view(), name='post-create') ] blog/models.py class Post(models.Model): title = models.CharField(max_length=100) content = models.TextField() date_posted = models.DateTimeField(default=timezone.now) # image = models.ImageField(default='default_post.jpg', upload_to='postimages') imagefile = models.FileField(upload_to='postimages', null=True, verbose_name="") # if user is deleted the idea should be deleted as author = models.ForeignKey(User, on_delete=models.CASCADE) def __str__(self): # return self.title return self.title + ": " + str(self.imagefile) def get_absolute_url(self): return reverse('post-detail', kwargs={'pk': self.pk}) My question: All I … -
Disable email confirmation for super user in django allauth
I am using django-allauth for registering user into my django dashboard application. Here the super user will be logging in and he can see certain data that I am fetching from DB. Whenever someone logs in into the dashboard with super user credential, it asks for email confirmation which I dont want. How can I disable that? -
Module not found Error while deploying website
I am tyring to deploy a django project on pythonanywhere.com, The project runs fine on the local server. I'm using a free account, I have created a virtual env and I've installed all required modules including django. I have also edited the wsgi.py file as follows: path = '/home/karmah24/reporting_portal' if path not in sys.path: sys.path.append(path) os.environ['DJANGO_SETTINGS_MODULE'] = 'portal.settings' I'm getting a "Module not found Error: test.urls not found" even though I've added this to the main project urls, and this project works on my local server. https://github.com/Karmah24/reporting_portal is my github repository that I want to deploy. Thanks :) -
PUT request for file in django-react-redux
I tried to apply put request in Django-react-redux for the file. What I was trying to do is, the user should have to replace the file using ID filed from put request. I write the following code in django-restframework. class EarSingleView(APIView): parser_classes = (MultiPartParser, FormParser) def get(self, request, ear_id, *args, **kwargs): ear = Ear.objects.get(pk=ear_id) serializer = EarSerializers(ear) return Response(serializer.data) def delete(self, request, ear_id, *args, **kwargs): ear = Ear.objects.get(pk=ear_id) ear.delete() return Response(status=status.HTTP_204_NO_CONTENT) def put(self, request, ear_id): ear = Ear.objects.get(pk=ear_id) serializer = EarSerializers(ear, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) My get request and delete request working perfectly. But in PUT request, If I tried to change the file filed and try to submit, the following error will occur, 400:Bad request. detail: "Multipart form parse error - Invalid boundary in multipart: None" My form looks like this, <form onSubmit={this.onSubmit} encType="multipart/form-data"> <Input placeholder="Enter layer name..." name="name" value={name} onChange={this.onChange} /> <input type="file" name="fileName" onChange={this.handleFileChange} /> </form> My form onSubmit event is looks like this, onSubmit = (e) => { e.preventDefault(); const { name, file, } = this.state; const newEARData = { id: this.props.idNumber, name, file, }; axios .put(`http://localhost:8000/api/ear/${newEARData.id}`, newEARData, { headers: { "content-type": "multipart/form-data", }, }) .then((res) => { dispatch({ type: UPDATE_EAR, … -
Django - Annotating the sum of a certain field in One To Many model
I am trying to get the sum of revenue of all the sales project, but I am not sure how to do it. Here are my models.py (simplified version but kept all the neccessary parts) class Quotation(models.Model): decisions = ( ('approved', 'Approved'), ('rejected', 'Rejected'), ('pending', 'Pending'), ) quotation_id = models.AutoField(primary_key=True) salesProject = models.ForeignKey('SalesProject' ,related_name='quotations', on_delete=models.CASCADE) quotation = models.MoneyField(max_digits=10, decimal_places=2) decision = models.CharField(max_length = 20, choices = decisions, default='pending') class SalesProject(models.Model): sales_project_id = models.AutoField(primary_key=True) sales_project_name = models.CharField(max_length=100) sales_project_est_rev = MoneyField(max_digits=10, decimal_places=2) What I want to do is to get the sum of all the 'quotation' field under the Quotation model that is tied to the same SalesProject instance. Is there a way to do so? My ideal output would be something like this (where actual_revenue is the sum of all the quotations tied to that particular SalesProject) [{ sales_project_id: 1, sales_project_name: 'Test Project', sales_project_est_rev: 200000, actual_revenue: 150000}, {...}, ...] All help is appreciated, thanks all! -
eb config and .ebextensions/ - .ebextensions/ not working
It is my understanding that editing the config via eb config and via .ebextensions/ both do the same thing. Using eb config directly changes the config were using .ebextensions/ changes the config but is scripted, thus repeatable. Is this correct? Initially, I usesed ebconf to change aws:elasticbeanstalk:container:python: NumProcesses: '1' NumThreads: '15' WSGIPath: application to aws:elasticbeanstalk:container:python: NumProcesses: '1' NumThreads: '15' WSGIPath: project.wsgi # <-- change which worked and I was able to run my application. I then decided I wanted to do all my changes thru .ebextensions/. I reverted the change made with eb config and created the file .ebextensions/02_python.config which contains: option_settings: "aws:elasticbeanstalk:container:python": WSGIPath: project.wsgi NumProcesses: 3 NumThreads: 20 "aws:elasticbeanstalk:environment:proxy:staticfiles": "/static/": "static/" after eb deploy all the chages are reflected when I do eb config except the WSGIPath value is not changed thus my app is no longer working. Why is .ebextensions/02_python.config not overwriting that one value? -
How to fix the “ unexpected token '=' ” error and can't assign to literalPython(parser-80) in python3 Django
I am trying to add the module to urls.py. Here my code: My init.py: from .account import ( AccountIsOwner, ) My view: class AccountIsOwner(StaffuserRequiredMixin, View): def post(self, request, *args, **kwargs): user = get_object_or_404(ReconUser, pk=self.kwargs['pk']) if user.is_account_owner: user.is_account_owner = False user.save() messages.success( self.request, 'User {username} no longer owner'.format( username=user.get_full_name() ) ) else: user.is_account_owner = True user.save() messages.success( self.request, 'User {username} is now an owner'.format( username=user.get_full_name() ) ) next = self.request.POST.get('next', '') if next: url = next else: url = reverse('account_detail', kwargs={'pk': user.pk}) return HttpResponseRedirect(url) my model: is_account_owner= models.BooleanField(default=False) my url: url(r'^user/(?P<pk>.+)/account_owner/$', AccountIsOwner.as_view(), name='account_is_account_owner' ) The errors are on the url: Line 1 can't assign to literalPython(parser-80) Line 2 class AccountIsOwner Mixin allows you to require a user with is_staff set to True. can't assign to literalPython(parser-80) Line 3 name: str can't assign to literalPython(parser-80) The code seems right,but VS reporting an error.