Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Are atomic transactions supposed to produce more connection.queries in Django?
I have the following in my models: from django.db import models class X(models.Model): ... class Y(models.Model): ... x = models.ForeignKey(X, on_delete=models.CASCADE) class Z(models.Model): ... y = models.ForeignKey(Y, on_delete=models.CASCADE) In one of my views, I am updating all my models like so: from .models import X, Y, Z from django.db import connection, transaction def do_something(bro, z_pk): z = Z.objects.select_related('y__x').get(pk=z_pk) y = z.y x = y.x ... with transaction.atomic(): z.save() y.save() x.save() print(len(connection.queries)) With the transaction.atomic(), the length of queries is 5. However, without it, the length returned is 4. That is, the following code: from .models import X, Y, Z from django.db import connection def do_something(bro, z_pk): z = Z.objects.select_related('y__x').get(pk=z_pk) y = z.y x = y.x ... z.save() y.save() x.save() print(len(connection.queries)) Returns a length of 4. Is this normal or am I missing something? Moreover, with the transaction.atomic(), am I hitting my database less times? Its difficult to understand that based on the connection.queries. Note: The extra query in the atomic function is the following: {'sql': 'BEGIN', 'time': '0.000'} -
A Desktop application for Django
I'm making a little Time Attendance Django Application where I want two parts of that program. A web part and a Desktop part. The Web Part would be used to view the Employees, Manager them, get payroll whereas the Desktop Part would be used to add attendance and stuff. And before anyone asks, I'll be using facial recognition for the attendance. So unless there's a way to use good and fast facial recognition on the web, I can't do it all on the Web. I was thinking of using electron. Make things simple. Have two login types, one for the web and the other for the Desktop. If that's the case, I would like to have different views and templates for the both of them without making a new app. Maybe add an option on the login page where you can choose either Desktop login or Web login. (Let me clear this here that by Web Login, I mean the Website like how you put in the URL in the browser and by Desktop Login, I mean the same thing but outside a browser. I understand that Electron is a browser but I hope you understand xD) If this is … -
How to fix empty custom user fields submission on custom user model requested from rest-auth/registration?
I am just a beginner here. Please help me out. I am using Django REST Framework, with django-rest-auth, I created a custom user model with extra fields. I want to use the django-rest-auth registration endpoint to register a new user in one request, and thus sending all the data to create a new user, including the data for the extra field but the extra field are not receiving the data. I am using AbstractBaseUser. This is how i created my custom user class UserManager(BaseUserManager): use_in_migrations = True def create_user(self, username, email, first_name, last_name, role_id, contact_num, opt_contact_num, citizenship_num, password=None): user = self.model( username=username, email=self.normalize_email(email), first_name=first_name, last_name=last_name, role_id=role_id, contact_num=contact_num, opt_contact_num=opt_contact_num, citizenship_num=citizenship_num, ) user.set_password(password) user.save(using=self._db) return user def create_staffuser(self,username, email, first_name, last_name, role_id, contact_num, opt_contact_num, citizenship_num, password): user = self.create_user( username=username, email=email, first_name=first_name, last_name=last_name, role_id=role_id, contact_num=contact_num, opt_contact_num=opt_contact_num, citizenship_num=citizenship_num, ) user.is_staff = True user.save(using=self._db) return user def create_superuser(self, username, email, first_name, last_name, role_id, contact_num, opt_contact_num, citizenship_num, password): user = self.create_user( username=username, email=email, first_name="True", last_name="True", role_id=0, contact_num=contact_num, opt_contact_num=opt_contact_num, citizenship_num=citizenship_num, password=password, ) user.is_staff = True user.is_admin = True user.save(using=self._db) return user #custom user table class User(AbstractBaseUser): username = models.CharField(max_length=120, unique=True) email = models.EmailField(unique=True) first_name = models.CharField(max_length=120) last_name = models.CharField(max_length=120) role_id = models.PositiveIntegerField(default=0) contact_num = PhoneNumberField(null=False, blank=False) … -
Start celery worker as daemons task doesn't run
I am new to celery and I am kinda confused. I followed tutorial from here I am able to start workers by systemctl start celeryd and viewing status celery -A jobs status worker1@c0326ddd3f01: OK worker2@c0326ddd3f01: OK Even on flower I can see those to worker online. However, when I run a task from my Flask app, nothing happens it gets stuck. In another terminal if I run celery worker -A jobs -l info another worker starts celery@c0326ddd3f01 and my task gets completed by this worker only. Even if I have multiple jobs queued up, this worker only runs them. I am not sure what I am doing wrong here. -
How to install dlib in django azure web app service
I want to publish a django web app in azure that uses dlib and opencv for processing images When trying to deploy the app through local code, it fails with dlib unable to install due to cmake not found. I tried using windows machine also but received same error. CMake must be installed to build the following extensions: dlib Expected to install dlib successfully in azure webapp -
django does not redirect in iframe
I have two websites(A and B). They both uses openID auth at the same provider. I want to embed one of those websites(B) to another(A) using <iframe>. But there is a problem with auth in embedded website(B). Problem: If I log in into A website and after this open B in another browser Tab, OpenID auth works fine and B automatically authenticate without providing login and password. And if I go to the page at A with embedded B: source in <iframe> recognize me as authenticated user. But If I open A without opening B in another browser Tab, <iframe> redirects me to openID provider login page and not redirect further. I'm trying to look at the Chrome Network debug and see some strange thing. How redirect look like in B if its open in another browser Tab: General section looks the same: Request URL: https://oidc.domain.com/openid/auth/?client_id=CLIENT_ID&redirect_uri=http://B.domain.com/callback&response_type=code Request Method: GET Status Code: 302 Found But Response headers section looks different. 1. If B is open in another browser tab: Connection: keep-alive Content-Length: 0 Content-Type: text/html; charset=utf-8 Date: Tue, 13 Aug 2019 17:53:32 GMT **Location: https://B.domain.com/callback?code=CODE** Server: nginx Vary: Cookie If B is opening like embedded part of A: Connection: keep-alive Content-Length: … -
Django Elastic Beanstalk Token Authentication
I've been working on an application which connects to a Django Server using Token authentication in Django Rest Framework, and Locally it's been working great, no problems with authentication and I've even been able to use the visual browsable api with authenticated users. Now when I deployed the project to Elastic Beanstalk I keep getting Unauthorized while using all urls that require authentication. I've tried the answers in here and in here. None seem to work, I'm using this document for http redirect as shown in aws's documentation. This are the authentication classes in my Django view: authentication_classes = (BasicAuthentication,TokenAuthentication,) And the permission classes: permission_classes = (gbets_permissions.IsSameUser,permissions.IsAuthenticated) I've already double checked settings.py and retried in local environment. Thanks for the help, I'm not used to working on back end, and I'm currently stuck, thank you. And if you need any more information let me know. -
How to update table row with jQuery-Tabledit and Django?
I am trying to create Crud app which creates,updates and delete data on same page after spending some time on internet I have ended up Jquery tabledit but I am not getting how to use django with Tabledit,Is there any way to to use django with Tabledit,Or any another way to create live edit in django ? -
Bootstrap Nav Dropdowns with Embedded CognitoForms
I'm learning Django and trying to model a web application where the end user of the application is to follow a process for a particular customer record. The process steps for the customer are shown using Bootstrap "Navs: Tabs with Dropdowns". Each step has the same drop down menu under it. The idea I'm going for is that when you click on any of the steps, it brings up an embedded web form (cognitoforms.com) relevant to that step of the process. If the customer record doesn't exist yet, end user populates the empty form and saves form data to the database using "Database Save". If the customer record does exist, end user can use "Database Pull" to repopulate the form with information from the database, end user can then edit and save updated form data back to the database using "Database Save". When end user is ready, they can click on "Generate Document" which will populate a template (.docx or similar) with the most up to date information from the database. Here is the Bootstrap code for creating "Step1" and "Step2" for Nav with Dropdowns. This works fine. <ul class="nav nav-tabs justify-content-center"> <li class="nav-item dropdown"> <a class="nav-link active dropdown-toggle" data-toggle="dropdown" … -
Import and Use django Model Class using variable
I am new to both python and django. First of all I am trying to do database query by using standard django feature . I dont want to use raw SQL. What i want to achieve: I want import and use Django Class dynamically by using variable like: dynamic_var = "Note" #import from .model import dynamic_var #query q = dynamic_var.objects.all() print(q) but above code are not working. I know there are some solid reason behind it, because dynamic_var is a String not a Class/model class. But how can i achieve this behavior. dynamic_var = "Note" from .model import Note ## Not work but i want to achieve info = dynamic_var.objects.all() # Working info = Note.objects.all() print(info) Error AttributeError at /note/ 'str' object has no attribute 'objects' -
How can I set Django model options in runtime?
I'm working on an admin panel and want to provide some configurable options for the end user. For example, when I have a User model I want an option in my panel to be able to select which fields are required. How can I change the model options in runtime and make it persistent? -
Add headers to a Django redirect
I am returning a file to the client via redirect(s3_url). According to the docs I should be able to add a custom header: response = redirect(s3_url) response['File-Name'] = file_name return response In chrome dev tools, I don't see any File-Name header in the response headers. -
DRF - upload CSV file and print each row
Using regular Django views I can iterate over each row in a CSV file like this: class FileUploadForm(forms.Form): file = forms.FileField() class ProductUpload(FormView): template_name = 'file_upload.html' form_class = FileUploadForm def form_valid(self, form): file = form.cleaned_data['file'] decoded_file = file.read().decode() io_string = io.StringIO(file) reader = csv.reader(io_string) for row in reader: print(row) messages.success(self.request, 'File uploaded successfully.') return redirect(self.request.path) But I can't figure it out using Django Rest Framework. I'm using parsers.FileUploadParser but not sure if this is correct - the form will be submitted through browser on the front end. class ProductUploadAPIView(views.APIView): parser_classes = (parsers.FileUploadParser,) def post(self, request, *args, **kwargs): file = request.data['file'] decoded_file = file.read().decode() # upload_products_csv.delay(decoded_file, request.user.pk) io_string = io.StringIO(decoded_file) reader = csv.reader(io_string) for row in reader: print(row) return Response(status=status.HTTP_204_NO_CONTENT) Here's my cURL request and the console output curl -v -X POST -u user:pass http://localhost:8000/api/file_upload/ -F "file=./file.csv" -H "Content-Type: text/csv" -H "Content-Disposition: attachment; filename=file.csv" ['--------------------------8e8e5ed08fe4ca99'] ['Content-Disposition: attachment; name="file"'] [] ['./file.csv'] ['--------------------------8e8e5ed08fe4ca99--'] -
Ordering choice fields
I'm creating an RESTFull Django API. I've created model, serializers and viewsets, just fine. Models.py: class Payer(AddressAbstract, ContactAbstract, DateTimeDBOps, SoftDeleted): CPF = '1' CNPJ = '2' DU_TYPE_CHOICE = ((CPF, _("CPF")), (CNPJ, _("CNPJ"))) DIGITACAO = 1 BANCO = 2 IMPORTACAO_ARQUIVO = 3 IMPORTACAO_CSV = 4 TYPE_ORIGIN = ( (DIGITACAO, 'Digitação'), (BANCO, 'Banco'), (IMPORTACAO_ARQUIVO, 'Importação arquivo'), (IMPORTACAO_CSV, 'Importação CSV') ) id = models.AutoField( verbose_name=_('Id'), primary_key=True, db_column='cd_pagador' ) covenant = models.ForeignKey( Covenant, verbose_name=_('Covenant'), on_delete=models.CASCADE, db_column='cd_convenio', blank=True, null=True ) name = models.CharField( verbose_name=_('Name'), max_length=40, db_column='nm_pagador' ) ... Viewset.py: class PayerCreateListView(ListCreateAPIView): serializer_class = PayerListSerializer ordering_fields = ('name', 'created_at', 'type_origin', 'type_origin_display', 'id', 'covenant', 'du', 'du_type', 'dt_last_access', 'email', 'phone', 'address', 'neighborhood', 'city', 'uf', 'cep') filter_fields = ['covenant'] filter_class = PayerFilter search_fields = ('name', 'du', 'covenant__code', 'covenant__bank__name', 'covenant__bank__id', 'covenant__company__name', 'covenant__company__du') def get_queryset(self): return Payer.objects.active() def get_serializer_class(self): if self.request.method == 'POST': return PayerCreateSerializer return PayerListSerializer Serializers.py: class PayerListSerializer(serializers.ModelSerializer): covenant = CovenantSerializer() du = serializers.SerializerMethodField('payer_du') type_origin_display = serializers.SerializerMethodField() class Meta: model = Payer fields = ('id', 'covenant', 'name', 'created_at', 'updated_at', 'du', 'du_type', 'email', 'phone', 'address', 'neighborhood', 'city', 'uf', 'cep', 'type_origin', 'type_origin_display') def payer_du(self, obj): return format_du(obj.du_type, obj.du) def get_type_origin_display(self, obj): return '{}'.format(obj.get_type_origin_display()) Now i want to order those fields on the viewset (ordering_fields). But when i try to order the … -
Django rest api to list values ManytoMany field
Im have two models, Manager and Template, one Template has n Managers, I want with the template id to get all the managers of the same I tried to make an api that takes the url template id, and filters the relationship table but it returns me empty This my models class Template(TimestampedModel, permissions.TemplatesPermission): title = models.CharField( max_length=1000, db_column='titulo', verbose_name='título', db_index=True, ) description = models.TextField( db_column='descricao', verbose_name='descrição', ) active = models.BooleanField( default=True, ) managers = models.ManyToManyField( Manager, through='TemplateManager', ) validity_date = models.DateTimeField( db_column='data_vigencia', verbose_name='data de vigência', ) class Meta: db_table = 'declaracoes_template' verbose_name = 'Template' verbose_name_plural = 'Templates' def __str__(self): return self.title class Manager(TimestampedModel): user = models.ForeignKey( settings.AUTH_USER_MODEL, models.CASCADE, verbose_name='usuário', ) file = models.CharField( max_length=1000, db_column='assinatura', verbose_name='assinatura do gestor', ) position = models.ForeignKey( Position, models.PROTECT, db_column='cargo', verbose_name='cargo', ) class Meta: db_table = 'declaracoes_gestor' verbose_name = 'Gestores' verbose_name_plural = 'Gestores' def __str__(self): return self.user class TemplateManager(TimestampedModel): manager = models.ForeignKey( Manager, on_delete=models.PROTECT, db_column='gestor_id', ) template = models.ForeignKey( Template, on_delete=models.CASCADE, db_column='template_id', ) This my view class TemplateManagerView(APIView): pagination_class = BasePagination def get(self, request, id): template = get_object_or_404(models.Template.objects.all(), id=id) managers = (models.TemplateManager.objects .filter(template=template) .all()) serializer = serializers.ManagerSerializer(managers, many=True) return Response(serializer.data) and my serializers class ManagerSerializer(serializers.ModelSerializer): position = PositionSerializer() user = serializers.CharField(source='user.first_name') class Meta: model … -
Which has optimal performance for generating a randomized list: `random.shuffle(ids)` or `.order_by("?")`?
I need to generate a randomized list of 50 items to send to the front-end for a landing page display. The landing page already loads much too slowly, so any optimization would be wonderful! Given the pre-existing performance issues and the large size of this table, I'm wondering which implementation is better practice, or if the difference is negligible: Option A: unit_ids = list(units.values_list('id', flat=True).distinct()) random.shuffle(unit_ids) unit_ids = unit_ids[:50] Option B: list(units.values_list('id', flat=True).order_by("?")[:50]) My concern is that according to the django docs, order_by('?') "may be expensive and slow" https://docs.djangoproject.com/en/dev/ref/models/querysets/#django.db.models.query.QuerySet.order_by We are using a MySQL db. I've tried searching for more info about implementation, but I'm not seeing anything more specific than what's in the docs. Help! -
Update Django Template if boolean is True
I am making a form using a model in Django. In that form, I have a boolean. When I click that boolean box, I want it to display some options and when I uncheck it, hide it. I have tried using the if blocks but it doesn't work. My boolean: employee_overtime_applicable = models.BooleanField() My if block that I did: {{ form.employee_overtime_applicable }} {% if form.employee_overtime_applicable %} <div class="row"> <label class="form-control-label">Overtime</label> <input type="text" placeholder="Overtime Rate"> </div> {% endif %} Don't worry, I know the overtime input box does nothing in Django. I just want to know how I can update it. -
Heroku CLI does not work after switching the shell
Heroku CLI was working well with my MacOS. Currently the login doesnot work via command line. mysystem% heroku-login zsh: command not found: heroku-login Only thing I remember is changing the shell from Bash to Zsh. Will it have any effect? Heroku exists and mysystem% heroku-login works -
How to design a structure that can lead user request to related app's view?
I am developing a web application which gets feedbacks from shifts in a factory by asking questions to employees and getting answers from them. Currently I am responsible for one area and it is called plastic injection. Afterwards plastic assembly, plastic connector and metal production areas will be in my list as well. We can consider areas as departments. In frontend I am using Vue.js and there is no problem so far. Backend side I created an API using Django with Django REST Framework. The API has two apps. First one is called 'core' app. It handles authentication and only has User model. This app will have another models and logics for feature plans. Second and last one is called 'plastic_injection' app. It has necessary models to get feedbacks from shifts. Problem I face shows up when I add another app for example 'plastic_assembly' or 'plastic_connector'. In frontend, users login then they answer some questions to send feedbacks of the shift they are inside of. At this point I need a logic to know which app user is belong to call endpoint of the app. The reason I created different app for different plastic areas(departments) is eventhough its models and … -
Using aggregate inside a subquery inside an annotate in Django
I have a queryset of objects (products). Each object has children (snapshot of the product at the moment of an order, for historical reasons); every child might or might not meet my requirements. Also, every child has a "quantity" field (the quantity of the product that was ordered). I want to sum the quantities of all the children that meet my requirements. So far I have the following query: qs = Product.objects.to_public_objects(to_country="USA", children__item_list__retailorder__isnull=False).annotate( quantity=Subquery( Product.objects.get( id=OuterRef("id") ).children.filter( item_list__retailorder__isnull=False ).aggregate( qq=Sum("quantity") )["qq"] ) ) This doesn't seem to be working as Django is complaining about not being able to use OuterRef because it's not in a Subquery (which I believe it's not). I'm not really sure why Django is complaining about that. The subquery, executed on it's own, works as expected, meaning, it returns the right quantity that has been sold to retail, for the product. How can I fix my query in order to use OuterRef? -
why it cost much time when call sorted function in django sometimes?
I added my logic code to django servers,and find somethin strange.When i using cat file,to send request to server,server will handle some requests slowly,and i print cost time,found that it will cost much time when call sorted function.But if i send this request alone,it is ok.Is it relevant with django server? using Django server 2.2.4 def generate(input_dict): id = input_dict["id"] title = input_dict["title"] starttime = datetime.datetime.now() word_list,nn_word_list = seg_title(title) seg_time = datetime.datetime.now() #print (nn_word_list) recall_tags = recall(word_list) recall_time = datetime.datetime.now() nn_tags = nn_ranking(id,nn_word_list,recall_tags) nn_time = datetime.datetime.now() print (seg_time-starttime,recall_time-seg_time,nn_time-recall_time) return nn_tags def recall(word): dict_result = {} starttime1 = datetime.datetime.now() word_fields = word.split(":") for i in range(0,len(word_fields)): if word_fields[i] == "": continue key = word_fields[i] if key in seg_dict: row_index = seg_dict[key] for i in range(csr_matrix.indptr[row_index],csr_matrix.indptr[row_index+1]): col_index = csr_matrix.indices[i] value = csr_matrix.data[i] if col_index not in dict_result: dict_result[col_index] = 0.0 dict_result[col_index] += value recall_tag = "" starttime2 = datetime.datetime.now() #pre_result = sorted(dict_result.items(),key=lambda x: x[1], reverse=True) pre_result = sorted(dict_result.items(), key=operator.itemgetter(1),reverse=True) starttime3 = datetime.datetime.now() for i in range(0,len(pre_result)): tag_id = index_dict[pre_result[i][0]] recall = recall+tag_id+":" if i >=19: break starttime4 = datetime.datetime.now() print (starttime2-starttime1,starttime3-starttime2,starttime4-starttime3) return recall_tag[:-1] client code is: for line in sys.stdin: line = line.rstrip('\n') fields = line.split("\t") id = fields[0] title = fields[1] … -
Is it possible that the check box that was previously selected was selected during the next edition
I want to create the opportunity to create a series of posts in my blog application. So far I've created the form and models, but I have a problem when editing already existing cycles, because in the check boxes I do not have selected previously selected posts belonging to the cycle. Do you have an idea how to fix it? models.py class Cycle(models.Model): title = models.CharField(max_length=200, unique=True) description = models.TextField(max_length=500, default="Brak opisu") date_created = models.DateTimeField(default=timezone.now) author = models.ForeignKey(User, on_delete=models.CASCADE) post = models.ManyToManyField(Post) def __str__(self): return self.title forms.py class CycleForm(BSModalForm): post = forms.MultipleChoiceField(required=False, widget=forms.CheckboxSelectMultiple, choices=((x.id, x.title) for x in Post.objects.all())) title = forms.CharField() description = forms.CharField() class Meta: model = Cycle fields = ['title', 'description', 'post'] views.py class CycleUpdateView(BSModalUpdateView): model = Cycle template_name = 'blog/cycle_update.html' form_class = CycleForm success_message = 'Success: Cycle was updated.' def get_success_url(self): reverse_user = self.request.user return reverse('profile', kwargs={'username': reverse_user}) html file <form method="post" action=""> {% csrf_token %} <div class="modal-header"> <h5 class="modal-title">Update Cycle</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> <span aria-hidden="true">&times;</span> </button> </div> <div class="modal-body"> {% for field in form %} <div class="form-group{% if field.errors %} invalid{% endif %}"> <label for="{{ field.id_for_label }}">{{ field.label }}</label> {{ field }} {% for error in field.errors %} <p class="help-block">{{ error }}</p> {% … -
Remove space of a string in template file
I'm new to python and django. I'm playing with a travel site with a different destination. I want to create a link for the destination from the name of it, some make it lower and delete the whitespace. The .lower work but .replace(" ", "") give me an error Could not parse the remainder: '(" ", "")' from 'dest.name.lower.replace(" ", "")' {% for dest in dests %} <div class="destination_title"><a href="{{ dest.name.lower.replace(" ","")}}.html">{{ dest.name }}</a></div> {% endfor %} If the city is San Francisco, the result should be sanfrancisco Thanks in advice and sorry for my English -
calling Django API using apache (mod_wsgi)
I have a frontend web tool which interacts with REST API written in danjgo. Each API calls take long time to process the call and is also CPU/GPU intensive. So I am planning to run only 1 call at a time and putting rest of the calls in queue. Now I am not sure if Celery with redis can be helpful here or should I stick with job queue approach at the java side. So, the tool would be used by multiple users and and so each user would have their jobs. So, I need to put the jobs in queue so that they can be processed one by one asynchronously. Would Celery be helpful here? -
How to get all value displayed from loop in javascripts using d3.js? always got last value displayed in charts
I'm using django for my personal projects, and using javascript to display value from database to charts model. But the problem is the value that displayed is always the last value of iteration When not using charts, I already checked that the value is displayed in every iteration. image When using charts, why charts can't be displayed in every iteration. image The code: <script src="//d3js.org/d3.v3.min.js"></script> {% block script %} {% for status in projects_status %} <h2>{{ status.task }} </h2> <div class="chart"></div> <script> let data=[{{status.user_requirements}},{{status.uat}},{{status.sit}},{{status.uat}},{{status.implementation}}]; let chart = d3.select(".chart"); let bar = chart.selectAll("div"); let barUpdate = bar.data(data); let barEnter = barUpdate.enter().append("div"); barEnter.style("width", function(d) { return d * 10 + "px"; }); barEnter.text(function(d) { return d; }); </script> {% endfor %} {% endblock %} I want each iteration to display charts Thanks for any help