Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Google App Engine Apis
I am new to Google App Engine. I am building a web app using python and django rest api framework. I am using cloud bigtable, cloud sql and cloud storage for my databases and storage. I wanted to know if I have to use Google App Engine Apis or can I build my own in django to read and write data to the databases and get files from cloud storage? I want to try to keep costs low and not lock in to Google App Engine. -
How do I get different django admins to play nicely with each other?
I am trying to use django-import-export and django-admin-sortable2, but they do not seem to work simultaneously. I first had this: from import_export.admin import ImportExportActionModelAdmin class PageAdmin(ImportExportActionModelAdmin): And import and export both appear and function as expected. I then added SortableAdminMixin: from import_export.admin import ImportExportActionModelAdmin from adminsortable2.admin import SortableAdminMixin class PageAdmin(SortableAdminMixin,ImportExportActionModelAdmin): The sortable functionality appeared, but this seemed to conflict with the import functionality, and the import button disappeared. I tried re-ordering: from import_export.admin import ImportExportActionModelAdmin from adminsortable2.admin import SortableAdminMixin class PageAdmin(SortableAdminMixin,ImportExportActionModelAdmin): And this time the items were no longer sortable, but the import button re-appeared. I've also tried separating import and export: from import_export.admin import ExportActionModelAdmin, ImportMixin from adminsortable2.admin import SortableAdminMixin class PageAdmin(ImportMixin,SortableAdminMixin,ExportActionModelAdmin): But to no avail. How do I get these admin mixins to play nicely with each other? -
Django ListView: Would a lock be reduntant in this example?
class MyView(ListView): def get(self, request, *args, **kwargs): self.a_class = MyClass() return super().get(request, *args, **kwargs) def ...(self): my_number = self.a_class.is_a_lock_reduntant_here() ... class MyClass(object): def __init__(self): self.counter = 0 def is_a_lock_reduntant_here(self): return self.counter += 1 The above is not a thread safe practice and I would normally use a lock. However, django states that: Each request served by a class-based view has an independent state; therefore, it is safe to store state variables on the instance (i.e., self.foo = 3 is a thread-safe operation). I do not completely understand the above quotation because storing state variables is an atomic operation. In my case I read and replace at the same time. The same quotation, reads that each view has an "independent state". Is it safe to consider the use of a lock redundant? -
I need to build a planner app, does one language have open source advantages?
I want to build an application for group leaders to make individual schedules for their group members. I imagine there must be an open source project that already has most of these features/abilities. Or, several projects that can be hacked together. Features it needs: Nested account creation (leader creates accounts for members) Calendar View (leader can view all group member schedules) Planner View (list of everything leader/member needs to do and when) Lesson plans (assignments and notes) Duplicate lessons/lesson templates Facebook logins for leaders and members Ability to import/view Google Calendar Questions: Is there an open source project that already has most of these features? Would this be easier to do in Rails or Django? Any estimation of how much $ this would cost to outsource? -
Django return render template and Json response
How could i render template in django and as well make a JsonResponse in one return? return render(request, 'exam_partial_comment.html', {'comments': comments, 'exam_id': exam}) Im trying to compine this with JsonResponse or something like this so it would render exam_partial_comment.html and also return JsonResponse({"message": message}), so I could display message with ajax success funtction ( console.log(data.message) -
Django prefetch_related a large dataset
I am right now having an issue with django's prefetch related. To give an example, let's imagine those models from django.db import models class Client(models.Model): name = models.CharField(max_length=255) class Purchase(models.Model): client = models.ForeignKey('Client') Let's imagine we have a few clients, something like 200, but they buy a lot so we have millions of purchases. If I have to create a webpage displaying all the clients and the number of purchases for each client, I would have to write something like that from django.db.models import Prefetch from .models import Purchase, Client purchases = Purchase.objects.all() clients = Client.prefetch_related(Prefetch('purchase_set', queryset=purchases)) The problem here is that I will query the big purchases database and that query might take even more than one minute, or worse create a MemoryError on the server. So, I tried to select only a batch of that database with purchases = Purchase.objects.all()[:9] but as we could expect, Django does not like it much and launches this kind of exception Traceback (most recent call last): File "project/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 149, in get_response response = self.process_exception_by_middleware(e, request) File "project/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 147, in get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "project/venv/lib/python3.6/site-packages/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, **kwargs) File "project/venv/lib/python3.6/site-packages/django/utils/decorators.py", l ine 67, … -
Django folder explorer implementation
This maybe a really stupid idea, but im trying to implement a folder explorer structure to my django project. At the moment it has 3 models. A project, container, and a file upload.A project can have many containers, and a container can have many file uploads. What i want to do now is to add folder structure to my current project model. There can't a folder without a project, but a folder can contain multiple containers or even more folders. Im having problems figuring out how the URLS should be structured and how the model should look like with the views. -
Error when extending user django model and define permissions for each model
I would like to have 2 types of users, NormalUsers and ArtistUser, with different permissions for each one: NormalUsers: create playlist ArtistUser: add songs, albums and create playlist too. I've tried extending the django user model and my codes is this: models.py from __future__ import unicode_literals from django.db import models from django.contrib.auth.models import User class ArtistUser(models.Model): id_artista = models.AutoField(primary_key=True) user = models.OneToOneField(User, on_delete=models.CASCADE) is_artist = models.BooleanField(default=True) name = models.TextField(max_length=50) class NormalUser(models.Model): id_user = models.AutoField(primary_key=True) user = models.OneToOneField(User, on_delete=models.CASCADE) is_artist = models.BooleanField(default=False) name = models.TextField(max_length=50) class Album(models.Model): id_album = models.AutoField(primary_key=True) name_album = models.TextField(max_length=50) artista = models.ForeignKey(ArtistUser) class Song(models.Model): id_song = models.AutoField(primary_key=True) name_song = models.TextField(max_length=50) artist = models.ForeignKey(ArtistUser) album = models.ForeignKey(Album) class Playlist(models.Model): id_playlist = models.AutoField(primary_key=True) name_playlist = models.TextField(max_length=50) user = models.ForeignKey(User) songs = models.ManyToManyField(Song) Someone could help me with the code to get this. Thank in advance. -
saving chat conversations in python3 list
i have a Django chatbot application that chats as a customer server in a electronic store.So, the django view calls a function in the back-end files which is chatbot.py to take the user input and return the matching output. So, every time the user write a question it is appended to the list and the chabot responce will be appended to the list as well. What i'm thinking now is to make the cabot remember the previous asked questions using the list: chatting_log =[] #funtion for check the DB: def check_data(Input): global chatting_log if Input in chatting_log: return True else: return False def new_data(Input): global chatting_log chatting_log.append(Input) def run_conversation_male(user_in): H = user_in NN= "H:"+str(user_in) new_data(NN) last_B = chatbot_secLast() New_H= ' '.join(PreProcess_text(H)) if check_data(NN) == True: return "you've asked this question before" else: reply = cursor.execute('SELECT respoce FROM Male_Conversation_Engine WHERE request REGEXP?',[New_H]).fetchone() if reply: B8=reply[0] NN= "B:"+str(B8) new_data(NN) return B8 But when i tested it it keep answering the question as it his first time. -
How do I manually deserialize a Dict using a Django-Rest serializer?
I am trying to create a new "Action" from a Dict using Django-Rest. I don't know what type of action we will have before we get the data from the client. I wrote a function to get the correct serializer and some custom logic to deserialize it. Given a string 'action_type', I can get the correct serializer: class ActionField(serializers.Field): @staticmethod def get_serializer(obj): types = { 'dashboard.staticaction': StaticActionSerializer, 'dashboard.surveyaction': SurveyActionSerializer, 'dashboard.linkaction': LinkActionSerializer, # etc } return types.get(obj, None) When I receive a model that contains one of these surprise actions, I call this code: class MyModelSerializer(serializers.HyperlinkedModelSerializer): """ Serializer for `MyModel` """ action = ActionField() action_id = serializers.IntegerField(allow_null=True) class Meta: model = MyModel fields = '__all__' def validate(self, data: dict) -> dict: # some other junk return super(MyModelSerializer, self).validate(data) def create(self, validated_data: dict) -> MyModel: """ Create and return a new `MyModel` instance, given the validated data. """ validated_action = validated_data.pop('action') rel_model = MyModel.get_action_class_mapping(validated_data['action_type']) rel_serializer = ActionField.get_serializer(rel_model._meta.label_lower) # this line fails here: # create() missing 1 required positional argument: 'validated_data' action = rel_serializer.create(validated_action) validated_data['action_id'] = action['id'] return MyModel.objects.create(**validated_data) As you can see it throws an error when I try to create and persist the action based on its type: TypeError at /rest/actions/ … -
Physical representation of python array in html
I have physical 2-d array of widgets that are cataloged in a database according to the following format: database id | plate | loc_index ------------------- 0 | g394e | 4 1 | d23e9 | 16 2 | f98d9 | 8 3 | w2340 | 3 4 | fl120 | 7 5 | f990d | 1 6 | e19f9 | 13 7 | t20f9 | 10 I would like to represent the plates on a django webapp according to their physical position (index) in the world. UI display Plate representation col1 col2 col3 col4 f990d e19f9 t20f9 w2340 fl120 g394e f98d9 d23e9 django views.py def index(request): plates = [(0, 'g394e', 4), (1, 'd23e9', 16), (2, 'f98d9', 8), (3, 'w2340', 3), (4, 'fl120', 7), (5, 'f990d', 1), (6, 'e19f9', 13), (7, 't20f9', 10)] context = { 'plates': plates } return render(request, 'index.html', context) index.html <script> $( function() { $( "#selectable" ).selectable(); } ); </script> <ul class="ul-col-4" id="selectable"> {% for plate in plates %} <li class="ui-widget-content">{{ plate }}</li> {% endfor %} </ul> additional.css .ul-col-4 { columns: 4; -webkit-columns: 4; /* Chrome, Safari, Opera */ -moz-columns: 4; /* Firefox */ list-style-type: none; /* no bullets */ } With this html code, the plates just … -
Django: PIL image upload from form issue
Django: Image upload issue I am writing a simple news app that will take an image as part of the article. I have chosen to modify the image creating a thumbnail to be stored in the form with the clean_(field name) function provided by Django Forms. The issue I am encountering is that after going through the submit and the clean_image function the ImageField validation kicks me back with an error stating that the file extension " is not allowed. I know the issues is coming from the clean_image function after I save the PIL image and then hand it back over to Django ContentFile to process. Any help will be great. Form: from django.conf import settings from django.core.files.base import ContentFile from django.utils.translation import ugettext_lazy as _ from .models import News from ironcsd.validators import MimetypeValidator from datetime import date from PIL import Image, ImageEnhance from io import StringIO, BytesIO import hashlib, mimetypes import os, io import logging, logging.config logger = logging.getLogger(__name__) class NewsForm(forms.ModelForm): class Meta: model = News widgets = { 'details': forms.Textarea(attrs={'id': 'smde-editor'}), } fields = ('title','details','image') def clean_image(self): image = self.cleaned_data.get('image') md5 = hashlib.md5() md5.update(repr(image.name).encode('utf-8')) file_name = md5.hexdigest() if image._size > 30 * 1024 * 1024: raise forms.ValidationError(_('File … -
Django; how to forward last form's details to the next?
I am fairly new to Django and I am a bit confused about how to make forms work. I am able to make a single form based on a model I have, but how can I make 3 forms where the second form is generated based on the first form's inputs and the third form is based on the 2nd form's inputs? For example: First form: Input your location (city, state, country) Second form (generated after the first form completed): If country X, then exclude A field in the second form or if the country in the previous form was Y, then exclude B field in the second form. Third form: similar idea as second form. Lastly, how do I prevent any database commits until all 3 forms are completed? Thanks for any inputs! -
Django and oracle TypeError: environment can only contain strings
I have a project that I need to connect to an oracle database When I use sqlite3 it brings by default with the following string: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } The application works without problems but when I connect with oracle with the following string: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.oracle', 'NAME': 'DBARS_PROD', 'USER': 'userhere', 'PASSWORD': 'passhere', } } Django gives me the following error: Traceback (most recent call last): File "manage.py", line 22, in <module> execute_from_command_line(sys.argv) File "C:\Users\nbueno\Envs\nworksdev\lib\site-packages\django\core\management\__init__.py", line 363, in execute_from_command_line utility.execute() File "C:\Users\nbueno\Envs\nworksdev\lib\site-packages\django\core\management\__init__.py", line 355, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\nbueno\Envs\nworksdev\lib\site-packages\django\core\management\base.py", line 283, in run_from_argv self.execute(*args, **cmd_options) File "C:\Users\nbueno\Envs\nworksdev\lib\site-packages\django\core\management\commands\runserver.py", line 62, in execute super(Command, self).execute(*args, **options) File "C:\Users\nbueno\Envs\nworksdev\lib\site-packages\django\core\management\base.py", line 330, in execute output = self.handle(*args, **options) File "C:\Users\nbueno\Envs\nworksdev\lib\site-packages\django\core\management\commands\runserver.py", line 101, in handle self.run(**options) File "C:\Users\nbueno\Envs\nworksdev\lib\site-packages\django\core\management\commands\runserver.py", line 110, in run autoreload.main(self.inner_run, None, options) File "C:\Users\nbueno\Envs\nworksdev\lib\site-packages\django\utils\autoreload.py", line 332, in main reloader(wrapped_main_func, args, kwargs) File "C:\Users\nbueno\Envs\nworksdev\lib\site-packages\django\utils\autoreload.py", line 303, in python_reloader exit_code = restart_with_reloader() File "C:\Users\nbueno\Envs\nworksdev\lib\site-packages\django\utils\autoreload.py", line 289, in restart_with_reloader exit_code = subprocess.call(args, env=new_environ) File "c:\python27\Lib\subprocess.py", line 523, in call return Popen(*popenargs, **kwargs).wait() File "c:\python27\Lib\subprocess.py", line 711, in __init__ errread, errwrite) File "c:\python27\Lib\subprocess.py", line 959, in _execute_child startupinfo) TypeError: environment can only contain strings … -
ImportError: No module named django.core.wsgi
I am getting this error while deploying django project on uwsgi and nginx server ImportError: No module named django.core.wsgi -
Tree with checkboxes | DJANGO
I have model Program with ManyToManyField function. I need too create form where as in the picture below where users can select and add functions to program. In other words I need TreeView with checkbox. How create such widget? I found django-mptt app which as I understand allows me to create tree. Is this app good for my task? Can someone who worked with this app show me example cause I am comfused with concept of this app. Finally is this app can give flexible configuration inside template? I mean I need to add checkboxes. models.py: class Group(models.Model): name = models.CharField(_('Name'), max_length=250) class Task(models.Model): name = models.CharField(_('Name'), max_length=250) group = models.ForeignKey(Group, on_delete=models.CASCADE) class Function(models.Model): name = models.CharField(_('Name'), max_length=250) task = models.ForeignKey(Task, on_delete=models.CASCADE) class Program(models.Model): name = models.CharField(_('Name'), max_length=250) function = models.ManyToManyField("Function") -
Map Django Model to External API
The context I have an external API providing data and allowing to post new data or patch existing one. Example of API response : response = requests.get('http://api/band/4/') print(response.json()) # {id: 4, name: 'The Beatles'} When I manipulate these objects the API provides I want them to be like instances of a Django model : I want to use them like objects and to be able to create relations to these models the API provides. I could smartly duplicate data in my database by creating standard models and populating when needed but I don't realy want to. Example of manipulation: class Band(Model): # should not create a new table in database but use API requests instead id = PrimaryKey() name = StringField() class User(Mode): favourite_band = ForeignKey(Band) # I want to be able to give an instance or directly the pk of a Band The issue Do you have any idea of how I could do that ? Maybe writing my own database Engine for these specific models whose instances are provided by the api ? or just a custom ModelField ? -
Django Session Variables Not Transferring Between Views - ONLY SOME USERS
This is weird: session variables are correctly being saved for all users (and said variables are being transferred between views) except for one user account. The affected user is a superuser, so I'm not sure if that's the cause of the problem? For example, View1 is setting session variable chosen_date_year, but View2 is unable to access this variable, returning KeyError (i.e. not in sessions). View1 snippet def View1(request, **kwargs): from myforms import myform if request.method == 'GET': form = myform(request.GET) if form.is_valid(): chosen_date_year = form.cleaned_data.get('chosen_date_year') #e.g. 2017 (int) request.session['chosen_date_year'] = chosen_date_year request.session.modified = True request.session.save() *** foo code *** return render(request, "View1.html", context) View2 snippet def View2(request, **kwargs): chosen_date_year = request.session['chosen_date_year'] # results in KeyError *** foo code *** return render(request, "View2.html", context) Most of the problems with sessions that I've found online revolve around people forgetting to install required middleware or a HTML template error. But I haven't found an example of this 'user-specific' session problem before. I've had the user delete cookies related to the site but this did not help. Calling request.session.modified = True and request.session.save() did not help either. I have also tested View1 by inserting print request.sessions.keys() and chosen_date_year does appear. When I insert the … -
Service Worker served from different port on a local machine
I'm trying to develop a PWA for our sites. In production and staging, we serve everything from one domain. However, in development on a local machine we serve HTML from one port using Django server eg http://localhost:8000 And the assets (including JS) using Grunt server from another port: http://localhost:8001 The problem is that the scope of the service workers is therefore only limited to assets, which is useless, I want to offline-cache pages on the 8000-port origin. I have somewhat been able to go around this by serving the service worker as a custom view in Django: # urls.py url(r'^(?P<scope>.*)sw\.js', service_worker_handler) # views.py def service_worker_handler(request, scope='/'): return HttpResponse(render_to_string('assets/sw.js', { 'scope': scope, }), content_type="application/x-javascript") However, I do not think this is a good solution. This code sets up custom routing rules which are not necessary for production at all. What I'm looking for is a local fix using a proxy, or something else that would let me serve the service worker with grunt like all the other assets. -
Loop through checkboxes in Django Form
I have a Django form which is based on a Django model (ie forms.ModelForm). I have a ManyToMany field which I would like to render as checkboxes and format very specifically and since I cannot workout how to do it in the form (I don't think I can format the checkbox label within widgets), I'm trying to do it in the template. The following looks just as I want it except, if there is an error in the form it doesn't highlight the previously checked fields. This is what I was trying to do with {% if c.value %} active {%endif%} <div class="feed-element"> <div class="row"> <div class="col-lg-3"> <h4>Goals</h4> </div> <div class="col-lg-9"> <div class="btn-group btn-select-new" data-toggle="buttons"> {% for c in form.goal %} <label class="btn btn-default {% if c.value %} active {%endif%}">{{c}}</label> {% endfor %} {{ form.goal.errors }} </div> </div> </div> </div> How do I pick out which boxes have been checked so I can relay that back to the user in the form. Also, this code is clearly wrong because it creates a template with 2 input statements for each item, for example: <label class="btn btn-default "> <label for="id_goal_0"> <input type="checkbox" name="goal" value="1" checked="" id="id_goal_0"> Traffic </label> </label> -
enable to initialize other python module before first request (Django + mod_wsgi + apache)
I am pretty new to web applications and recently working on a simple demo system using Django + mod_wsgi. the project looks like this: django/ |- manage.py |- mysite/ | |- url.py | |- setting.py | |- wsgi.py |- myapp/ |- views.py |- taggers.py |- ... and here is the myapp.conf configuration WSGIDaemonProcess init python-home=/usr/local/.../3.5.0 python-path=/usr/local/.../django WSGIProcessGroup init WSGIImportScript /usr/local/.../django/mysite/wsgi.py process-group=init application-group=%{GLOBAL} WSGIApplicationGroup %{GLOBAL} WSGIScriptAlias /myapp /usr/local/.../django/mysite/wsgi.py <Directory /usr/local/.../django/mysite> <Files wsgi.py> Require all granted </Files> </Directory> what this demo do is quite simple. given an input, press a 'compute' button, calculate the result using a pre-loaded machine learning model, which is loaded at the beginning of views.py. Loading the machine learning model will get involved with other python modules such as sklearn along with some self-made modules located in the same directory such as tagger.py. When I test this demo using 'python3 manage.py runserver', everything goes just fine. Django first loads the machine learning model to RAM, which is quite time-consuming, and wait for the incoming requests and then calculates. But when I use mod_wsgi + apache, I notice that when I start apache, only wsgi.py is executed. The machine learning model is not loaded (which means views.py is not … -
Order by field value saved in ManyToMany through
I have two models, Note and Category, linked with a ManyToManyField through another model, NoteCategoryData, so that I can add a field order. This allows me to assign a different order to notes for each category they belong to. Example: noteA can then be in 3rd position in categoryA but in 1st position in categoryB. Now, I would like to query all the notes in one of categoryA, and order this query based on each note's order related to the categoryA. Something like: notes_query = categoryA.note_set.all() notes_query = notes_query.order_by('order_in_categoryA') How can I achieve this? -
Image upload in web page with correct orientation
I have been going through a few SO questions and getting bits and pieces from it, but the solution is not fully apparent. What I have is a Django web server and in my model for posts I have an ImageField. I have then created my form and that renders fine. I can use the input tag just fine, and it will upload to the server and location saves to the database. The problem comes when I take an image on an iOS device in portrait and view it on another platform e.g. Android, Desktop, the image will be rotated. Now I have found out that when you take an image on iOS it will keep the resolution as landscape and put into the EXIF data the orientation of the image to make it portrait. So finally my question is, what is the best approach to this issue. The options I have seen as are: In the webpage where the upload occurs, transform the image and reset the orientation then upload to the server. Upload as is, and in my view do the transform/reset before the save. Again upload as is, and I render the image in the view detail … -
rename `key` to `access_token` in django-rest-auth?
My django-rest-auth on authentication sends the following response {"key":"XXXXXXXXXXXXXX"} Now i am using ember-simple-auth specifically oauth2-password-grant for authentication which expects access_token in response body. After looking at source code of ember js here, i found out that the value access_token is defined as string and cannot be changed. My question is how can i implement authentication using the two stack. Is there any way that i can rename key to access_token in django ? -
Django - one-to-one modelAdmin
I am moderately proficient with django and trying to use modelAdmin for an intranet project. Essentially, I have a "Resources" model, which is populated by another team. Second model is "Intake", where users submit request for a resource. It has one-to-one mapping to resource. Objective is to only allow 1 resource allocation per intake. Now, Intake modelAdmin shows the form, with a drop-down field to resource, with a caveat that it shows all resources, regardless of previous allocation or not. ex. if resource is taken by an intake, save button detects that disallow saves. This is expected, but then the drop-down should not show that resource in the first place. How can I do this, i.e. do not show resource already allocated ? class Resource(models.Model): label = models.CharField(max_length=50, primary_key=True) created = models.DateTimeField(auto_now_add=True) created_by = models.ForeignKey('auth.User', default=1) class Meta: verbose_name_plural = "Resource Pool" def __str__(self): return self.label class Intake(models.Model): order_id = models.AutoField(primary_key=True) requestor = models.ForeignKey('auth.User', default=1) resource = models.OneToOneField(Resource, verbose_name="Allocation") project = models.CharField(max_length=50) class Meta: verbose_name_plural = "Environment Request" def __str__(self): print("self called") return self.project