Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Changing the queryKey argument in bootstrap autocomplete
I am using bootstrap autocomplete to fetch data from django-tastypie. The problem that I am facing is that bootstrap autocomplete uses a parameter q, while tastypie has all the regular django options. I could like to change q to name__contains so that the ajax query can work with the api exported with tastipie. How can I do it? I am not able to find a way to achieve this. -
How to convert below query to ORM equivalent
Below query How DO I convert into Django ORM equivalent. select channel, country, sum(impressions) as impressions, sum(clicks) as clicks from sampledataset where date <= '2017-06-01' group by channel, country order by clicks desc; -
Django Admin intercept onchange event of a field and make an action
In my django project i would clear a field value every time another select field have an onChange event. I have an add form like thisone: every time Template field change (onChange), Test Case field have to become blank. How can i do this in a django admin add or edit page? So many thanks in advance -
How to add the current user to a many-to-many field when creating via a viewset?
I have a Django project which has "workspaces", and users can belong to multiple workspaces, so there's a many-to-many relationship between the users and the workspaces. Now, everything else works fine so far, but I'm having trouble adding the current user to the workspace's list of users when the user creates that workspace. The model looks like this: class Workspace(models.Model): name = models.CharField(max_length=100) users = models.ManyToManyField(User, related_name='workspaces') The serializer looks like this: class WorkspaceSerializer(serializers.ModelSerializer): class Meta: model = Workspace fields = ('name', 'users') And finally, the view: class WorkspaceViewSet(viewsets.ModelViewSet): queryset = Workspace.objects.all().order_by('name') serializer_class = WorkspaceSerializer permission_classes = [permissions.IsAuthenticated,] So, to elaborate, when a user creates a new workspace, I'd like the workspace's users field to refer to that user. Currently, that field remains empty. How would I populate the field so that the current user is added there right on creation? -
Modify <item><description> element from Feedparser
I'm using feedparser to handle an RSS fed from pubmed. The feed link is https://eutils.ncbi.nlm.nih.gov/entrez/eutils/erss.cgi?rss_guid=1RGmO3jHeXUu8o2CWPinET6JLLik93hwR2IAJ5mU-YzoPeX1-O The 'Abstract' for each article in the feed is buried within HTML in the <(description)> element, and it's the abstract that I want to display in a webpage (using Django). All of the other elements are easily accessible to me. I played around and wrote some code below to strip away anything that isn't in the abstract from and print it, but even using solutions found on Stackoverflow I can't substitute the 'abstract' variable back into the original feedparser dictionary. What exists: <(item)> <(description)> loads of HTML What I want: <(item)> <(description)> abstract or: <(item)> <(description)> <(abstract)> abstract Hope that makes sense. the code is: import feedparser rss = 'https://eutils.ncbi.nlm.nih.gov/entrez/eutils/erss.cgi?rss_guid=1RGmO3jHeXUu8o2CWPinET6JLLik93hwR2IAJ5mU-YzoPeX1-O' feed = feedparser.parse(rss) for post in feed.entries: try: abstract = (post.description[post.description.index("<p>Abstract<br/>")+len("<p>Abstract<br/>"):post.description.index("</p><p>PMID:")])[:-14] print (abstract) except ValueError: break FWIW, here's the code for the front end: {% for post in feed.entries %} <div class="panel panel-default"> <div class="panel-heading"> <h4><a href="{{ post.link }}" target="_blank"> {{ post.title }} </a></h4> <h5> {{ post.description }} </h5> <h5> {{ post.author }}, {{ post.category }} </h5> </div> </div> {% endfor %} Thanks a million for any tips! -
Case insensitive URLS causes admin page error
In my Django site, I just made my urls case insensitive using re_path(r'^(?I)urlName', views.viewName) But this caused an error when attempting to access my admin page, ValueError at /admin/ Non-reversible reg-exp portion: '(?i' I have tried adding my admin page to my apps urls, my project urls, having admin not use re_patha and having an admin url in both the project and app urls.py in project urls.py: from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('interface.urls')), path('', include('interface.urls')), path('', include('interface.urls')), path('', include('interface.urls')), path('', include('interface.urls')), path('', include('interface.urls')), path('', include('interface.urls')), path('', include('interface.urls')), path('', include('interface.urls')), path('', include('interface.urls')), path('', include('interface.urls')), path('', include('interface.urls')), path('', include('interface.urls')), ] in app urls.py: from django.urls import path, re_path from . import views urlpatterns = [ path('', views.indexView), re_path(r'^(?i)edit/', views.editView), re_path(r'^(?i)add/', views.addView), re_path(r'^(?i)delete/', views.deleteView), re_path(r'^(?i)postEditColumnData/', views.postEditTableData), re_path(r'^(?i)updateTableEdit/', views.postUpdateEdit), re_path(r'^(?i)postAdd/', views.postAdd), re_path(r'^(?i)postUploadFile/', views.postUploadFile), re_path(r'^(?i)postDelete/', views.postDelete), re_path(r'^(?i)postDelete/', views.postDelete), re_path(r'^(?i)postUploadDeleteFile/', views.postUploadDeleteFile), re_path(r'^(?i)uploadEditFile/', views.uploadEditFile), re_path(r'^(?i)denied/', views.authDenied), ] How would I make this error go away, and is there a way to make the admin url case insensitive along with the other urls? -
Django view dramatically slower when called via HTTP request than when called via Django shell
I have a view that retrieves a class instance and runs a method on it: def run_loader(request, loader_id): try: pl = Loader.objects.get(pk=loader_id) pl.run() return JsonResponse({ 'success': True}, safe=False) except Exception as e: print(e) return JsonResponse( 'error', safe=False ) When this view is run via an HTTP request to the associated path, processing is approximately 300 times slower than when run via the Django shell using RequestFactory. I have tried hitting the path using both Axios and Postman, with the same result. The method, run(), involves processing many rows of Pandas dataframes. When the view is run via the Django shell, it takes a few minutes to complete. Eventually I'll move this to an asynchronous task worker, but I'm baffled by the speed disparity. What could be the cause? -
How to fix Error : KeyError at /input 'Company'?
I want to Represent Single Row In HTML In Tabular Form. But getting Error KeyError at /input 'Company'. Am workin With Django. def input(request): if 'pass' in request.POST: company = request.POST['pass'] else: company = False df = pandas.read_csv('data.csv',index_col = None) take = df.groupby('Company').mean() table = take[take['Company'] == company] table_content = table.to_html(classes = 'table') return render(request,'result.html',{'table_content': table_content}) KeyError at /input 'Company' -
Extending django.contrib.auth.User model
I'm using django.contrib.auth and I have a working login/registration system, however I want to extend my User model to have street address and phone number for registration purposes only, and I'm not sure how can I do that properly. What I have done works but feels wrong and I would like to know a proper way to do this. I created accounts app and in models.py I have this: from django.db import models from django.contrib.auth.models import AbstractUser class User(AbstractUser): address = models.CharField(max_length=200) address_number = models.CharField(max_length=20) phone = models.CharField(max_length=20) ...then I registered that in admin.py: from django.contrib import admin from .models import User admin.site.register(User) ...then in settings.py I added this line: AUTH_USER_MODEL = 'accounts.User' ...and wherever I used django.contrib.auth.User, now I use accounts.models.User. As I said, this works however it somehow feels wrong and it easily could be as this is my first time extending the User model. Why I think this is wrong? For one, Users has disappeared in admin from "Authentication and Authorization" and is instead under "Accounts" (as expected). How can I accomplish this in a proper way? -
Django model.objects.all does not show information
I am using django to make a webapp and I am trying to learn how to make queries. I already connected my SQLServer database to django, and from the SQL Management studio added information for the 3 tables that I have. Then I ran python manage.py inspectdb, copied the results in my blog.models.pyand put python manage.py makemigrations and then migrate However, when I open the python shell via python manage.py shell, import the models and make a `model.objects.all(), the result is the following: <QuerySet [<Usuario: Usuario object (1)>, <Usuario: Usuario object (2)>]> Django identifies that I have 2 objects created, but whenever I try to get specific information from one of them it shows the following: >>> Usuario.objects.all() <QuerySet [<Usuario: Usuario object (1)>, <Usuario: Usuario object (2)>]> >>> user= Usuario.objects.all() >>> user[0] <Usuario: Usuario object (1)> >>> user[0].ID_Usuario Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: 'Usuario' object has no attribute 'ID_Usuario' >>> This is the information that I inserted via SQL Management studio INSERT INTO Usuario(ID_Usuario,Nombre,Apellido,Edad,Email,Password,ID_Cargo,ID_Rol) VALUES(1,'Pepe','Arana',36,'pepe@hotmail.com','pass',1,1), (2,'Roquito','Velarde',40,'roquito@hotmail.com','pass',2,1) Why isn´t django recognizing the attributes but it is recognizing the fact that an object has been created? Thanks -
How to define a variable in Django TemplateView?
I created a Django project and prepared 3 html pages. By the way, my English is not good, I apologize in advance. Here is the views.py file example; class HomePageView(TemplateView): template_name = 'home.html' class LoginPageView(TemplateView): template_name = 'login.html' class NotFoundPageView(TemplateView): template_name = 'login.html' I want to add a variable to the home.html file. How do I define it here? Thanks in advance for your help. -
python code not screening out future dates with datetime comparison
I am having a strange issue when trying to screen out future dates from an API endpoint of the application. I have the following tournaments with 1 tournament listed an hour in the future (tourney 8): I have the following model: class Tournament(models.Model): name = models.CharField(max_length=250) start_time = models.DateTimeField(auto_now=False) active = models.BooleanField(default=True) def __str__(self): return self.name I have the following view: def get_current_tournaments(request, player_id): registration_queryset = TournamentRegistration.objects.filter(player=player_id) active_tournaments = [] for registration in registration_queryset: tournament = registration.tournament if tournament.active: utc=pytz.UTC start_time = tournament.start_time.replace(tzinfo=utc) if start_time > datetime.now(start_time.tzinfo): active_tournaments.append(tournament.pk) print(active_tournaments) return JsonResponse({'tournament_ids': active_tournaments}) Every time I call the get_current_tournamnents() view I get a list of ALL the tourneys a player is registered for, including ones in the future (tourney 8 is in the future). My expectation was that if start_time > datetime.now(start_time.tzinfo): would screen out future times and only include currently active tournaments, i.e. tourneys that have already started (start_time is in the past). HTTP GET /tournaments/current-tournaments/1/ 200 [0.02, 127.0.0.1:55555] [2, 1, 4, 6, 5, 7, 8] This list above includes tourney 8, which is in the future as of right now. Now when i change that line to if start_time < datetime.now(start_time.tzinfo):, so if start_time is less than now, it … -
Putting message in queue and sending it as soon as socket gets open
Considering channels == 1.1.8. Suppose we have added different reply_channels in a Group. How can we keep track whether message has been successfully received by all the members added in a Group? e.g Suppose we have Group named "chat" and to this Group three reply channels i.e reply_channel1, reply_channel2 and reply_channel3 are added. Suppose sockets of reply_channel1 and reply_channel2 are open but socket of reply_channel3 is in error state. If we execute Group("chat").send({"text":json.dumps({"welcome":"How are you"})}) then message will successfully be received by listeners at reply_channel1 and reply_channel2 but listener at reply_channel3 will not receive this message. How can i put this message in queue and send to listener at reply_channel3 as soon as socket of reply_channel3 gets opened ? -
Javascript: Add Data to Request
TLDR: Need to pass JSON info from javascript to my python backend AND return an excel download of that JSON info. How do I return byte info in a POST API call or add JSON info to the request object on the javascript side? I am using Python (3.7) and Django (2.1.5) to host my web app. On a web page I format a JSON based on dynamic user inputs. It is not a form, the JSON is stored as a global variable that gets updated as the user changes aspects of the page. I would like to download the JSON object as an excel (xlsx) file when the user is done. Without going into too much detail, a normal form submission is not an option here. The excel file needs to look a certain way. This means that I would like to pass the JSON object from javascript to my backend (Python), make it a Panas DataFrame, perform different xlsx_writer formats, and return the new object. My main issue revolves around either downloading the excel object or sending the JSON info. There are two ways that I have thought to send the JSON data from Javascript to Python. Option … -
Django: How to return None if any values in a queryset aggregation/annotaion is null
I want to do a aggregation/annotation on a Django queryset. If any of the values is Null, the total returned should be None. I always thought that is the default, but it doesn't seem to be the case. See example below: class Check(models.Model): value = models.FloatField(null=True) >>> Check.objects.create(value=20) >>> Check.objects.create(value=30) >>> qs=Check.objects.all() >>> qs.aggregate(Sum('value')) {'value__sum': 50.0} # this is expected >>> Check.objects.create() # Create object with blank value >>> qs=Check.objects.all() >>> qs.aggregate(Sum('value')) {'value__sum': 50.0} # I want this to return None How can I get the desired behaviour in the calculation? When using an aggregation it's easy to check if any values are None, but it is not so easy when using an annotation. I'm using Django 1.11.18 with Postgres -
Approaches for Database model with multiple junctions
I work with the python Django Framework and try to build a website. I want to achieve a good performance for the case described below and search for a good database/model design. Since this is a database-relating question in my eyes, I wrote about columns and tables. In Django I would replace them with the respective models and fields. I have a model with a user and a profile table. user contains the primary user data like username and email. profile contains all other profile details like name, birthdate, favorite color and so on. profile has also some dependent tables like e.g. custom_fields, where users can add custom fields to their profile. One user has exactly one profile that he owns. Every contact can see the users profile. I need to store the profile owner with the profile. If a user (A) views the profile of another user (B) and misses some information, A can add this information into the profile of B. That changes are not visible for anyone but the creator A. That means I need to store the editor (A) and the target (B) together with the profile. I thought about how to design this and found … -
Django ORM: how to make aggregation with filter with annotated field?
I have view with statistics where I calculate multiple count's on different filters of some base query set: qs = Model.onjects.filter(...).annotate(a=...) a = qs.filter(Q(a__lt=5)).count() b = qs.filter(Q(a__lt=10)).count() # this is just an example, real filters are more complex ... But each count makes separate query to the DB and I want to optimize it. I tried aggregation: qs.aggregation( a=Count('a', filter=Q(a__lt=5)), b=Count('a', filter=Q(a__lt=10)), ) but got an error: django.db.utils.OperationalError: (1054, "Unknown column '__col2' in 'field list'"). I don't even know where this __col2 comes from. It seems like aggregation doesn't work well with annotation because when I use regular model field inside count.filter instead of annotated field a everything is fine. -
How to create a link to DJANGO detail page through INFOWINDOW in GOOGLEMAP using JAVASCRIPT
I want to create a URL link on the InfoWindow from Google Map that render a detail page of each object. How can I possibly achieve this? I tried a couple of ways but was not able to accomplish it. Currently the data are generated through an API and I use DJANGO backend with Javascript to render the map. I have used the sign HERE -->>> on the codes below to show where to apply the logic but i am looking for the right way to accomplish. What I want to accomplish is whenever someone click on "Picture 2 in modal dialog" it should redirect to the detail page of the object. API APP URL app_name = 'blog' urlpatterns = [ path('', PostList.as_view()), path('<int:pk>/', PostDetail.as_view()) ] VIEW class PostList(generics.ListCreateAPIView): permission_classes = (permissions.IsAuthenticated,) queryset = Post.objects.all() serializer_class = PostSerializer class PostDetail(generics.RetrieveUpdateDestroyAPIView): permission_classes = (permissions.IsAuthenticated,) queryset = Post.objects.all() serializer_class = PostSerializer HTML <script type="text/javascript"> fetch('http://127.0.0.1:8000/api/') .then(function (response) { return response.json(); }) .then(function (locations) { var map = new google.maps.Map(document.getElementById('mapDiv'), { zoom: 13, center: new google.maps.LatLng(40.736481, -73.988243), mapTypeId: google.maps.MapTypeId.ROADMAP }); var infowindow = new google.maps.InfoWindow(); var marker, i; for (i = 0; i < locations.length; i++) { marker = new google.maps.Marker({ position: new … -
from .forms import CheckoutForm ModuleNotFoundError: No module named 'core.forms'
I am trying to make checkout form for my website. I receive an error saying No module named 'core.forms'. Here are my files: View.py from django.contrib import messages from django.core.exceptions import ObjectDoesNotExist from django.contrib.auth.decorators import login_required from django.contrib.auth.mixins import LoginRequiredMixin from django.shortcuts import render, get_object_or_404 from django.views.generic import ListView, DetailView, View from django.shortcuts import redirect from django.utils import timezone from .forms import CheckoutForm from .models import Item, OrderItem, Order class CheckoutView(View): def get(self, *args, **kwargs): form = CheckoutForm() context = { 'form': form } return render(self.request, "checkout.html", context) def post(self, *args, **kwargs): form = CheckoutForm(self.request.POST or None) if form.is_valid(): print("The form is valid") return redirect('core:checkout') Form.py from django import forms from django_countries.fields import CountryField class CheckoutForm(forms.Form): street_address = forms.CharField() apartment_address = forms.CharField(required=False) country = CountryField(blank_label='(select country)') zip = forms.CharField() same_billing_address = forms.BooleanField(widget=forms.CheckboxInput()) save_info = forms.BooleanField(widget=forms.CheckboxInput()) payment_option = forms.BooleanField(widget=forms.RadioSelect()) Plz, Help -
Inherit template blocks from siblings in Django
I've 3 template files named 'base.html', 'navbar.html' and 'dashboard.html'. 'base.html' is the main parent file which has a {% block content %}. Navbar has {% block navtitle %}. Now What I want to do is, I want navtitle block in my 'dashboard.html' file. Both 'navbar.html' and 'dashboard.html' extends 'base.html'. I'm able to get content block from base.html file, but can't get navbar block. Please guide me how can I do the same Sample files below base.html {% block content %} {% endblock %} navbar.html {% extends 'base.html' %} {% block navtitle %} {% endblock %} dashboard.html {% extends 'base.html' %} {% block content %} Demo {% endblock %} # Able to print it {% block navtitle %} Demo 2 {% endblock %} # Not able to print it I also tried to extend navbar.html file in dashboard.html file but still no luck. -
Redirect to URL from admin list display
I am trying to add link on admin page, which will open a template. In my admin view I have all fields from the model and I need last field to be link to my template. I tried returning httresponse, but it always takes me to object edit page. class RoomAdmin(admin.ModelAdmin): model = Room list_display = ('name', 'property', 'room_id', 'link_to_price_history_view',) list_display_links = ('link_to_price_history_view',) actions = ['room', 'price_history', 'checkin', 'checkout', 'price', 'refund_status', 'scanned', 'availability_count', 'max_people', 'meal'] def link_to_price_history_view(self, obj): return mark_safe('%s%s' % ('http://127.0.0.1:8000/price-history/', obj.room_id)) link_to_price_history_view.allow_tags = True link_to_price_history_view.short_description = "Price History" The link becomes clickable, but it only takes me to edit page, I tried reading previous questions and tried returning httpresponse, I am sure there is an easy fix. Thank you -
Least bad way to sleep in a Django view
I have a synchronous API in Django that makes multiple external requests. For performance reason, I'll depreciate it and switch to an asynchronous model: the request will trigger multiple celery sub-tasks and the client will have to fetch the task status until it is completed. However I cannot get rid of the existing API right away as we need to enforce backward compatibility. For this to work, I intend to implement a synchronous wrapper around my asynchronous tasks. This is what the backward compatible API must do in my opinion: launch the asynchronous tasks sleep refresh the object, exit if it's completed or loop Something along the lines of : task = MyTask.objects.create(...).run() # Will span multiple parallel sub-tasks while task.is_ongoing(): # Check if all sub-tasks are completed sleep(1) task.refresh_from_db() Of course it's very bad design to sleep in Django's view but I don't think there is another way to ensure backward compatibility with my synchronous API. If Django did support async things would be better but I haven't found a way to suspend current thread without blocking everything. Of course I could run all tasks synchronously in the synchronous API but it'd defeat the purpose of parallelizing "long-running" tasks … -
Django ORM: how count in annotation differ from count on query set?
qs = ... qs = qs.annotate(v=Count('*', filter=Q(a__lt=5))) a = qs.first().v b = qs.filter(Q(a__lt=5)).count() assert a == b # error Is there any reason why these methods could produce different results? -
How to Configure uwsgi + nginx + Django + websocket?
I successfully deployed uwsgi + nginx + Django. But I want to want to communicate with my server using websocket. I followed this tutorial to handle normal https requests. https://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html The nginx conf file settings are like this: upstream django { server unix:///path/to/your/mysite/mysite.sock; # for a file socket } # configuration of the server server { # the port your site will be served on listen 443 ssl; # managed by Certbot # the domain name it will serve for server_name example.com; # substitute your machine's IP address or FQDN charset utf-8; location /static { alias /path/to/your/mysite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed } } I Googled a lot and found that people recommended to add these settings: proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; proxy_redirect off; It does not work. I did not use proxy_pass because I am already using uwsgi_pass django; and my client attempts to connect using websocket to the same url (location) I uwsgi_pass to django. I looked into uwsgi docs and found this page: https://uwsgi-docs.readthedocs.io/en/latest/WebSockets.html According … -
Django Rest Framework - how to get only one field from related models set
I have following models: from django.db import models class City(models.Model): name = models.CharField(max_length=30) last_update = models.DateTimeField(null=True) class BusStop(models.Model): city = models.ForeignKey(City, on_delete=models.CASCADE) name = models.CharField(max_length=200, blank=True, default='') Now using Django Rest Framework, I would like to create serializer that will return City details along with the list of all BusStops in the city - but I want the list to be only strings with BusStop names, like this: { "id": 1 "name": "City" "last_update": "2019-09-19T22:13:54.851363Z" "bus_stops": [ "stop1", "stop2", "stop3" ] } What I've tried so far is following serializers: from rest_framework import serializers class BusStopSerializer(serializers.ModelSerializer): class Meta: model = BusStop fields = ('name', ) class CityDetailsSerializer(serializers.ModelSerializer): busstop_set = BusStopSerializer(many=True) class Meta: model = City fields = ('id', 'name', 'last_update', 'busstop_set') But this creates list of objects with 'name' in them. So, how can I create a list with only BusStop names (as strings) in it?