Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
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? -
Django - How to change dropdown form into table
I'm creating an application in Django and I'm having an issue with iterating through options in a dropdown from a form in Django. I was wondering if anyone could be of assistance. detail.html {% for subject in subject_list %} <form action="" method="POST"> {% csrf_token %} <table class='table table-borderless table-hover'> <tbody> <tr> <td>{{ subject.subjectname }} {% for field in form %} {{ field.hidden }} {% endfor %} </td> </tr> </tbody> </table> <button type="submit" id="login-btn" class="btn">Create a new evaluation</button> </form> {% endfor %} forms.py class CreateEvalForm(forms.ModelForm): subjectname = ModelChoiceField(queryset=Subject.objects, label='', empty_label="Choose subject..") class Meta: model = Evaluation fields = ['subjectname', ] models.py class Subject(models.Model): id = models.AutoField(primary_key=True) subjectname = models.CharField(max_length=255, help_text="Type the name of the subject.") slug = models.SlugField(max_length=200, unique=True) Ideally we want it to look like on the picture below: Note that the picture is in danish and Fag = Subject, Opret ny evaluering = Create new evaluation and Se evaluering = View evaluation we want this we don't want this -
redirecting users after login django
By default after login django redirects the user to an accounts/profile page or if you edit the LOGIN_REDIRECT_URL you can send the user to another page you specify in the settings.py. I want my users to, after logged in, not be able to go to the login page but instead be redirected to the LOGIN_REDIRECT_URL automatically. Is there a way of doing it using the default login view? Any input is appreciated. -
Django Performance, APIView vs ViewSet
I was looking at building a star topology type model with a CI/CD workflow, with a white list based signal flow, and with that design model I want to integrate APIs, but I want to optimize for performance to push my code to the limits. I was looking at APIView vs ViewSet, where one allows me to do a wide array of things, to include interacting with other APIs with an API, whereas the other is pretty well defined with preset tasks in mind, typically focused towards working with specific systems and models, but I wanted to also know if by doing so it gained a performance boost, or potentially gained a performance hit by being focused. I'm not entirely sure how to test it either. What are some recommendations on how to effectively test API performance on a side by side basis? My thoughts are to create a python script that queries the system with a specific predefined load, run a series of tasks with that predefined load, and tags the start and stop time. Fastest time wins obviously, but is that a fair assessment? Are there any previously built tools for this, and if so, what are they? … -
What should be applied, ManyToMany or a model with 2 Foreign Keys?
I have two models: Owner and Accountants. A Owner can have 0 or more Accountants, an Accountant can be connected to 1 or more Owners. So far I've created the two models using a ManyToManyField, here they are: models.py class Accountants(models.Model): owner = models.ManyToManyField(Owner, related_name='accountants') company = models.CharField(db_column='Company', max_length=25) fiscal_rep = models.BooleanField(default=False) class Owner(models.Model): name = models.CharField(db_column='Owner_Name', max_length=200) surname = models.CharField(db_column='Owner_Surname', max_length=30) property = models.ManyToManyField(Property, blank=True) I've created my form to add the accountant, it looks like this: class AccountantAddForm(forms.ModelForm): owner = forms.ModelMultipleChoiceField(queryset=Owner.objects.all(),widget=forms.Select( attrs={ ... The problem here is that when I add an accountant, the Owner field is expecting more than one value, and on my form I pass the owner pre-filled as initial data, so I came up with an idea of making another model named Owner_Accountant connecting both Owner and Accountants models, as below: class Owner_Accountants(models.Model): owner_pk = models.ForeignKey(Owner, related_name='owner_accountant') accountant_pk = models.ForeignKey(Accountants, related_name='accountants_owner') What I want to know is: is this a good solution or is it quite the same? Is there a better way to do this, or am I doing it completely wrong? -
Django Channels : How to run AsyncConsumer as a worker
In Django channels, I want to run an AsyncConsumer in an infinite loop as a separate process, like a worker. I can't use the usual worker because the channel name is fixed there. In my case, I want to use my own channel name on which to send data. I want to create a new channel name for the consumer to listen on. I was able to this in a crude way by just creating my own channel like below. self.layer = get_channel_layer() self.channel_name = await self.layer.new_channel() Similarly sending await self.layer.send(self.master, data) But, I thought, if we can use the AsyncConsumer and a recommended way to listen in a loop (as in runworker), it might be more stable. Any thoughts... -
How to accelerate autocomplete function with Django?
I have implemented django-autocomplete-light on my website (Python 3.6.5 / Django 1.11.20) but retrieving results is very slow (www.capelight.com). The source database is made of 12,000 names (cities, provinces, etc). How can I accelerate this process ? I have read that implementing a Trie strongly accelerates the process, but I have no clue how to do that. Or maybe django-autocomplete is already based on Trie ? Thanks a lot -
How to create pagination and category filter in django-cms?
I got the next model: class Post(models.Model): title = models.CharField(max_length=100) short_description = models.CharField(max_length=100) image = models.ImageField(upload_to="uploads/images/") content = HTMLField(blank=True) slug = AutoSlugField(always_update=True,populate_from='title', unique=True) date_created = models.DateField(default=datetime.date.today()) categories = CategoryManyToManyField() Here I get posts list: posts = [] for plugin in plugins posts.extend(Post.objects.filter(id=plugin[0].post_id)) My next step its to create pagination and categories filter, but I don't know how I can do that? For pagination, I have a simple solution - I want to split full posts list into small lists with static elements count (3 posts in the example): Full_list = [Post1, Post2, Post3, Post4, Post5, Post6, Post7, Post8, Post9] to split list = [[Post1, Post2, Post3], [Post4, Post5, Post6], [Post7, Post8, Post9]] Every element of the list corresponds to one page of the pagination. For every pagination page, I programmatically generate the page. So every element of list has his own page. And the same mechanism for categories - each category has own page. How to combine those things? Maybe I should create separated pagination pages for every category?