Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Utilizing Django cache_page() With Django REST Framework without Browser Cache
I'm attempting to cache high traffic requests within my application with my main goal being to offload database requests. I'm using the Django REST framework to create these requests. Currently I'm implementing the caching in this fashion. class FeaturedViewSet(viewsets.ReadOnlyModelViewSet): permission_classes = (HasAPIAccess,) serializer_class = FeaturedSerializer pagination_class = FeaturedSetPagination @method_decorator(cache_control(max_age=0)) @method_decorator(cache_page(60 * 60 * 2)) def dispatch(self, *args, **kwargs): return super(FeaturedViewSet, self).dispatch(*args, **kwargs) I'm setting the cache_control(max_age=0) because I don't want it to setup any kind of browser caching. The client needs to always make the request to the application server because the data needs to be accurate when a user makes the request. The caching should only be done on the server. On my Django server when I update anything related to data that needs to be "fresh" I call cache.clear() to make sure that old data is not being sent. So from my understanding this seems like it should offload request from the database and just pull them from the ElastiCache Server. However, when I run load testing I'm seeing the CPU on my database server spike still. I'm not sure if I don't understand how caching is working? Or maybe something is just not being setup correctly. Thank … -
<QuerySet []> is not JSON serializable - Django Session
I am trying to set session but it is giving me error is not JSON serializable self.request.session['acl_permissions'] = AclRoleAccess.objects.filter( Q(acl_company=self.request.user.userprofile.user_company) & Q(acl_role=self.request.user.userprofile.user_role) & Q(acl_has_access=True) ) if 'acl_permissions' in self.request.session: acl_permissions = self.request.session['acl_permissions'] print(acl_permissions) -
Limiting choices for ForeignKey field to instance variables in model
I created a Match model for a tournament related application (Team is just another model): class Match(models.Model): home_team = models.ForeignKey(Team, related_name="home_team", on_delete=models.CASCADE) away_team = models.ForeignKey(Team, related_name="away_team", on_delete=models.CASCADE) winning_team = models.ForeignKey(Team, related_name="winning_team", on_delete=models.CASCADE) My question is, is it possible to limit the choices for the winning_team field to only be either home_team or away_team? i.e. If I create a form to record the team that won, how can I limit the choices in that select list to just home_team and away_team? -
Create Django Model Object Instance Without DB Lookup For Foreign Keys
I have a use case where I want to be able to create instances of model objects that have foreign key constraints to other models. models.py from django.db import models class Foo(models.Model): __module__ = "some_app" foo_name = models.CharField(null=False, blank=False, unique=True, max_length=50) def __str__(self): return self.foo_name class Bar(models.Model): __module__ = "some_app" bar_name = models.CharField(null=False, blank=False, unique=True, max_length=50) def __str__(self): return self.bar_name class FooBar(models.Model): __module__ = "some_app" version = models.IntegerField(null=False, blank=False) foo = models.ForeignKey(Foo, on_delete=models.CASCADE) bar = models.ForeignKey(Bar, on_delete=models.CASCADE) def __str__(self): return f"{self.foo}@{self.bar}: {self.version}" Say for example I have instances of my Foo and Bar classes. a_foo = Foo(foo_name="my_foo") a_foo.save() a_bar = Bar(bar_name="my_bar") a_bar.save() Some time after they've been created, I know what the names are, but I don't have references to the objects. Similarly I don't know their ids. But I do want to use them to create an instance of FooBar. I can create that instance by doing this foo = get_object_or_404(Foo, foo_name="my_foo") bar = get_object_or_404(Bar, bar_name="my_bar") foo_bar = FooBar(foo=foo, bar=bar, version=1) But would like to avoid having to make two unnecessary DB calls to create, instances of the objects. Or even just to look up their ids. What I'd prefer to do is something like this foo_bar = FooBar(foo__foo_name="my_foo", … -
Enable PK based filtering in Django Graphene Relay while retaining Global IDs
Problem I am using django-graphene with Relay on our GraphQL Server. The implementation imposes a Global ID requirement in the graphene.relay.Node class that overrides and hides Django's ID field. As a result, I can query like this: { allBatches(id:"QmF0Y2hOb2RlOjE=") { edges { node { id pk } } } } And get this response: { "data": { "allBatches": { "edges": [ { "node": { "id": "QmF0Y2hOb2RlOjE=", "pk": 1 } } ] } } } However, what I lose is the ability to filter by the original ID (or PK) field of the Object itself: { allBatches(id:1) { edges { node { id pk } } } } In fact, I simply cannot filter objects by ID. I can think of two possible work-arounds to this: 1. Prevent django-graphene-relay from hijacking and shadowing the id field, perhaps force it to use a different field name such as gid 2. Find a way to include pk as a special field that is available both as a property and in filter Solution 1 I have made no progress on 1 since it appears as though django-graphene (and perhaps the relay standard) imposes a limitation that this field be called id Solution 2 On … -
CURL receive "empty reply from server", but browser receive response. Why?
I set up rest-api server. (Django 1.11 + NGINX + Gunicorn + Postgresql 9.6) For testing, I developed simple API. Get request URL /test, then respond "success" string. In web browser, it worked well. But when I request same url with CURL, I receive "curl: (52) Empty reply from server". I tried curl on other remote server, and I tried curl http://127.0.0.1/test on that server. Both didn't not work. I received "curl: (52) Empty reply from server". Why it work in web browser, and it not work in CURL? Where should I check? NGIMX? Django? I checked firewell. Sorry for poor english. Thanks! -
Django Queryset take too long to load
I just uploaded a project to my server on digital ocean. When I load the Partnership view it takes about 1 minute before the data is queried and filtered an d then displayed. There are 1,905,120 entries to be filtered to display about 200,000 entries. How can I optimize the process? views.py class PartnershipView(LoginRequiredMixin, generic.ListView): model = Partnership slug = '' slug1 = '' slug2 = '' def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) slug = self.kwargs.get("slug") slug1 = self.kwargs.get("slug1") slug2 = self.kwargs.get("slug2") currentYear = datetime.datetime.now().year currentMonth = calendar.month_name[datetime.datetime.now().month] partnershipArms = PartnershipArm.objects.all() context['partnershipArms'] = partnershipArms partnershipArm = PartnershipArm.objects.get(slug = slug) if slug =='': partnerships = self.get_queryset().filter(partnershipArm = partnershipArm, year=currentYear, month = currentMonth ) else: partnerships = self.get_queryset().filter(partnershipArm = partnershipArm, year=slug1, month = slug2 ) members = Member.objects.all() context['currentYear'] = currentYear context['currentMonth'] = currentMonth context['partnershipArmName'] = partnershipArm.name context['partnershipArm'] = slug context['partnerships'] = partnerships context['members'] = members context['year'] = slug1 context['month'] = slug2 return context partnership_list.html <table id="datatable-buttons" class="table table-striped table-bordered"> <thead> <tr> <th>Name</th> <th>contact</th> <th>Email</th> <th>Week 1</th> <th>Week 2</th> <th>Week 3</th> <th>Week 4</th> <th>Total</th> </tr> </thead> <tbody> {% for instance in partnerships %} <tr> {% for member in members %} {% if member.pk == instance.member_id %} <td>{{ member.title }} {{ member.fname }} … -
Summing a column after filtering Django 2.1
This is my for loop (in my template.html): {% for item in filter.qs %} <tr> <th scope="row">{{ item.id }}</th> <td>{{ item.nome }}</td> <td>{{ item.data|date:"d, F" }}</td> <td>{{ item.tipo_pgto}}</td> <td>{{ item.mes }}</td> <td>{{ item.entrada }}</td> <td>{{ item.valor}}</td> </tr> {% endfor %} The variable {{ item.valor }} holds a decimal number after filtering. How can I sum it and put the result in another part of my HTML? Is the better practice use now JavaScript or there is a way like creating a variable in my view.py and then place it in html? -
Django validators RegexValidator does not show custom message when field is incorrect
I need to add RegexValidator to URLField. It works, but I want to customize it`s message. I added parameter "message" with my custom text, but it still shows "Enter a valid URL." instead of my text. Django version: 2.1.5 class ExampleModel(models.Model): url_address = models.URLField( validators=[RegexValidator('https://www.google.com/.*', message='This is not a Google URL')]) -
Heroku not detecting Django settings for static files
I am creating a project that I would like to deploy on Heroku. We are using Django and want to maintain our statics on Heroku as it is a small application. The issue I am encountering is that when Heroku runs python manage.py collectstatic --noinput I get an error saying I haven't set my STATIC_ROOT. My file structure is as follows: ├── htmlcov ├── mysite │ ├── settings.py │ ├── wsgi.py │ ├── urls.py │ └── random.py │ ├── staticfiles ├── tweetmood │ ├── migrations │ └── tests │ ├── Procfile ├── runtime.txt └── manage.py My settings file is """ Django settings for mysite project. Generated by 'django-admin startproject' using Django 2.1.5. For more information on this file, see https://docs.djangoproject.com/en/2.1/topics/settings/ For the full list of settings and their values, see https://docs.djangoproject.com/en/2.1/ref/settings/ """ import os import django_heroku # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.abspath(__file__)) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/2.1/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = 'MY_SUPER_SECRET_KEY' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = ['0.0.0.0', 'localhost', 'https://my-project-name.herokuapp.com'] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', … -
Serve Django media files on another server
I have a Django web application on a server. I need to keep files uploaded by users on another server. Is it possible? and where can I begin the process? I use Gunicorn Nginx for the Django project sever. -
Best practice to transform a classical embedded HTML Project to a REST-friendly Project
Is there any tip or best practice to transform a legacy project that embedd data directly in HTML into a REST-friendly project? With embedded HTML Project I mean for example classical Django or JAVA EE Project, where the whole HTML site is rendered in Backend. What should I be awared of? Is there any example from professional Dev that I can learn from? -
how to show username instead of user id using xlwt
Am exporting data to excel using xlwt but the problem that am getting user id in the row user instead of username, how to fix this,, here is the view def export_buy_and_sell_report_csv(request): today_date = date.today() response = HttpResponse(content_type='application/ms-excel') response['Content-Disposition'] = 'attachment; filename="BuyAndSellReports_' + str(today_date) + '.xls"' wb = xlwt.Workbook(encoding='utf-8') ws = wb.add_sheet('BuyAndSells') # Sheet header, first row row_num = 0 font_style = xlwt.XFStyle() font_style.font.bold = True columns = ['المستخدم', ' القيمة', 'البيان', 'الفئة', 'نوع الحركة', 'التاريخ'] for col_num in range(len(columns)): ws.write(row_num, col_num, columns[col_num], font_style) # Sheet body, remaining rows font_style = xlwt.XFStyle() rows = models.BuyAndSellReport.objects.all().values_list('user', 'amount', 'text', 'move_category', 'move_type', 'date') rows = [[x.strftime("%Y-%m-%d %H:%M") if isinstance(x, datetime.datetime) else x for x in row] for row in rows] for row in rows: row_num += 1 for col_num in range(len(row)): ws.write(row_num, col_num, row[col_num], font_style) wb.save(response) return response -
Django Conditional login redirect
This is my middleware which is continuously redirecting. How can i give conditional redirecting in my middleware? middleware.py class PermissionRequiredMiddleware: def init(self, get_response): self.get_response = get_response def call(self, request): response = self.get_response(request) return response def process_view(self, request, view_func, view_args, view_kwargs): assert hasattr(request, 'user') path = request.path_info if request.user.is_authenticated: if request.user.userprofile.user_role.id == 1 : return redirect('superadmin_dashboard') elif request.user.userprofile.user_role.id == 2 : return redirect('admin_dashboard') elif request.user.userprofile.user_role.id == 3 : return redirect('admin_dashboard') elif request.user.userprofile.user_role.id == 4 : return redirect('admin_dashboard') -
Remove Database settings of django using bash script
I am trying to create a bash script to create a boilerplate django project that suits for my company. I need to delete the DATABASES in settings and append new one. The DATABASES is a python dictionary with structure DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } For this I tried something like this sed -i -e "/DATABASES = {/,/}/d" settings.py But it resulted in a trailing }. The output is } I understood that the pattern it matches is for the first curly braces but not the second. What should be the approach for this. -
Searching using normal characters not working but it's working with special characters
I'm working on app in django 1.11 and I have problem with searching in admin panel in django. When I type in admin panel for example müller - it is ok, I have results. But when I try to find for example muller - I have no results. There is some way to solve this issue? edit: I model I have setted str but I return not this field which I'm trying to find. -
How to extract tables seprately from a pdf file of 2-3 pages using tabula in python?
tabula.convert_into(path,"page1.csv",pages = 1, spreadsheet = True, multiple_tables = True, output_format="csv", guess=True) df1 = pd.read_csv('page1.csv', error_bad_lines=False) htmTable = df1.to_html() I want to fetch each table sepratly can i do that? and if yes how can i do so ? please guide me. Thanks -
Is is possible to decrease connection that made to db?
Currently, my Django application has millions of records and I want to update based on it's ManyToMany related fields value. Consequently what I did works, but it took so many times. In order to update only three records, it uses 13 queries. Record model has genres field which is ManyToMany field. Also, has authors field which is also ManyToMany field. And lastly, Author model has ManyToMany field that implies genres. for i in Record.objects.filter(authors__popular=True).only("authors", "genres"): for a in i.authors.all(): print(a.name) # test purpose for genre in i.genres.all(): if a.genres.exists(): a.genres.add(genre) When i run len(connection.queries) it shows query numbers that ran, i want it to be lesser than 13. -
How can I make SSE with Python (Django)?
I have two different pages, one (A) that displays data taken from a model object, and one (B) that changes its fields. I would like that when the post data is sent from B to the server, the server changes the values in A. What is the best way to do it? This example could work for me but it's in PHP... there is a way to replicate it whit Python? https://www.w3schools.com/html/html5_serversentevents.asp -
Class-based-views - Pulling together multiple models using FormView
I am trying to build a Survey App. I have defined a model for Survey, Questions, Responses as below. What I want to do is create a Survey (in admin, which I can do) then display this Survey as a page which users can fill in. I'm using Class Based Views for the first time and I'm stuck with how to render the Survey form along with the associated Questions and allow input of Answers. In Admin, I've created a Survey and successfully added Questions to it. FormView (and UpdateView, tried as part of another SO answer) allows me to display the Survey model's 'name' and 'description' attributes - but the questions don't appear. What do I need to do to make the Survey and it's Questions available in my form (and allow the user to enter a response)? MODELS class Survey(models.Model): name = models.CharField(max_length=400) description = models.TextField() def survey_questions(self): if self.pk: return Question.objects.filter(survey=self.pk) else: return None def publish(self): self.published_date = timezone.now() self.save() class Question(models.Model): survey = models.ForeignKey(Survey, on_delete=models.CASCADE) question = models.TextField() class Response(models.Model): member = models.ForeignKey(user_model, on_delete=models.SET_NULL, null=True, blank=True) survey = models.ForeignKey(Survey, on_delete=models.CASCADE) created = models.DateTimeField(auto_now_add=True) updated = models.DateTimeField(auto_now_add=True) class AnswerBase(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) response = models.ForeignKey(Response, on_delete=models.CASCADE) … -
Cypress Drag and drop test
I would like to test the sortable table with Cypress.io I follow their page instruction : https://docs.cypress.io/api/commands/trigger.html#Mouse-Events and it doesn't work... First : it doesn't move the element Second : when I see the step by step test on left side, mousedown and mousemove are located on same position... my test cy.get('.resource-filter-table') .trigger('mousedown', {witch:1, pageX:188, pageY:196}) .trigger('mousemove', {witch:1, pageX:188, pageY:261}) .trigger('mouseup') my html <table class="table table-borderless resource-filter-table"> <thead> <th scope="col"><input type="checkbox" checked=True class="resourceFilterElementAll" onchange="checkAllResources(this)"></th> <th scope="col">Line</th> </thead> <tbody> {% for linija in user_lines %} <tr> <th><input type="checkbox" checked=True class="resourceFilterElement" onchange="filterOfResources()" id="{{linija.pk}}"></th> <th class="filter-list-element">{{ linija.line_name}} <i class="filter-list-icon material-icons">drag_handle</i></th> </tr> {% endfor %} </tbody> </table> -
Embed OpenCV in Django Template
I'm trying to make simple local web app that will start the camera and have simple options like "Record" and "Save"(take a picture). I have read this post, and it does what it need. But the only problem is that i can't put it in a template or have the page and buttons show. So can someone give me some example on how to do it or hints what need to be done in order to have the camera show as frame in html? -
How to save a model in Django Rest Framework having one to one relationship
I have a Django model named BankDetail that has a one to one relationship with User. #BankeDetails class BankDetail(models.Model): account_holder_name = models.CharField(max_length=100) account_number = models.CharField(max_length=50, blank=True, null=True) iban = models.CharField("IBAN", max_length=34, blank=True, null=True) bank_name = models.CharField(max_length=100) bank_address = models.CharField(max_length=500) swift_bic_code = models.CharField(max_length=11) user = models.OneToOneField(MyUser,on_delete=models.CASCADE) accepting_fiat_currency = models.OneToOneField(AcceptedFiatCurrency) created_at = models.DateTimeField(auto_now_add=True,null=True) updated_at = models.DateTimeField(auto_now=True) The serializer is as listed below : class BankDetailSerializer(serializers.ModelSerializer): """Serializer for BankDetails""" class Meta: model = BankDetail fields = "__all__" def validate(self, data): if data['account_number'] or data['iban']: raise serializers.ValidationError("Please fill Account Number or IBAN") return data Request Payload : { "account_holder_name":"Aladin", "account_number":"1239893", "bank_name":"Aladin Bank", "bank_address":"Republic of Wadia", "swift_bic_code":"1", "user_id":"1", "accepting_fiat_currency_id":"1" } Now when I'm trying to save the model from my view, I get the following error : { "user": [ "This field is required." ], "accepting_fiat_currency": [ "This field is required." ] } How can I pass I refrence ob these objects, do I need to manually retrieve them from the db using the id's? -
Sharing session with threads in Django views
I'm trying to access a session variable with AJAX. I have different two views, the main one (index) and another one only returning the session value (refreshSession). The main one starts a thread that is changing the session value every second but when the view that only returns the session value access to it, the changes that the thread is doing are being lost. // views.py // def refreshSession(request): sesAlarm = request.session['alarm'] return JsonResponse({'alm': sesAlarm}) def index(request): request.session['alarm'] = 0 t = Thread(target = threadFunction, args=(request,)) t.daemon = True t.start() context={} return HttpResponse(template.render(context, request)) def threadFunction(request): while True: request.session['alarm'] += 1 time.sleep(1) // JS // var idAlarm = setInterval(function(){ $.ajax({ type:"POST", url:"/app/update_session/", cache: 'false', success: function(data) { alert(data.alm); } }); }, 5000); This code always shows alerts with '0'. I think the problem is that changes in the thread are not being reflected in the request in refreshSession() because request is not passed by "reference" to the thread (I'm a main C programmer). If I use a global variable instead of a session it works perfect, the alert shows the number increasing. But I've read that using global variables in views is not recommended. So, what am I missing? How … -
How to obtain Object Values from Class Based Views in Django
I'm using the DetailView to open a new page based on two variables of a model (dtcalculo and cdcarteira) where pk = 'dtcalculo' The purpose is to retrieve what dtcalculo and cdcarteira is used because I want to use this date to filter another two models. views.py class VisualizaFundoSolutions(DetailView): #slug_field = 'cdcarteira' context_object_name = 'fundo' template_name = 'prototipo_fundo.html' queryset = RmDadoscarteira.objetos.all() # Como o modelo tem duas primary keys, e necessario usar sobrescrever a funcao get_object def get_object(self): return RmDadoscarteira.objetos.get(pk=self.kwargs["pk"], cdcarteira=self.kwargs["cdcarteira"]) # A view trabalha com 3 templates utilizando o filtro de data e nome do fundo def get_context_data(self, **kwargs): # Obtendo nome do fundo e data context = super(VisualizaFundoSolutions, self).get_context_data(**kwargs) print(context) return context urls.py from portal_riscos.views import * from django.urls import path from django.contrib.auth.views import LoginView app_name = 'portal_riscos' # urlpatterns contém a lista de roteamento URLs urlpatterns = [ # Dashboard Solutions path('', FlagshipSolutions, name='dash_solutions'), path('solutions_fundos/<pk>/<cdcarteira>', VisualizaFundoSolutions.as_view(), name='solutions_fundos') ] Is there any way using get_context_data or get_object where can I store the "filter" variables? Thanks