Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
is django with mongodb using MongoEngine advisable for a banking application? [on hold]
Since django is made for relational database, how stable using mongodb for a banking type of application.I would like to know the advantages and disadvantages. From my search I found MongoEngine as the best option to integrate both, any other suggestions, thanks in advance for your guidance. -
Grappelli autocomplete widget shows raw_id instead of text with when used with super_inlines app
I want to use Grappelli autocomplete widget inside an inline. The following solution works fine, unless I add super_inlines app in my project's settings. models.py class HostModel(models.Model): # some model class MyModel(models.Model): name = models.ForeignKey('HostModel') admin.py class MyModelInline(SuperInlineModelAdmin, admin.TabularInline): model = MyModel form = SampleAttributeInlineForm raw_id_fields = ('name',) autocomplete_lookup_fields = {'fk': ['name',]} class HostModelAdmin(SuperModelAdmin): inlines = (MyModelInline) installed apps 'super_inlines.grappelli_integration', 'super_inlines', 'grappelli', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'articles', 'django_hstore', But when I use super_inlines, autocomplete widget works as if I did not write autocomplete_lookup_fields = {'fk': ['name',]} at all, i.e. it shows id in the widgets text area instead of text values and shows a corresponding text value on the right. I usesuper_inlines` app because I also have this inline inside another inline elsewhere in the code. How to have an autocomplete widget inside a nested admin inline? ` -
Django 1.9 error - 'User' object has no attribute 'profile'
So I recently added an optional user profile model that is linked to a user via a OneToOneField like so: class UserProfile(models.Model): # Creating class user = models.OneToOneField(User, on_delete=models.CASCADE) This worked fine, and my current UserProfile models were intact both before and after I added in this field to link the profile to a user. The problem comes when I log in on the website as a valid user, after submitting the log in form comes an error: AttributeError at /login/ 'User' object has no attribute 'profile' I have searched through my files for anything along the lines of 'User.profile', and similar queries, however I can't seem to find anything that would allow me to properly log in as a user. I wasn't sure what other code to post here, and didn't want to spam this question with potentially unnecessary code so tell me if there's anything more specific (views.py, models.py, forms.py, etc) that will be needed to solve this sort of problem. Thank you -
Imagefield: can't upload image
Django 1.10 Could you help me upload image. Request is done via ajax. The view inherits from some mixins. They are just auxiliary to add action or enctype. Well, to present the form as it is rendered. I haven't shown the mixins here. The result is that the form can't pass validation. Error message for image field: This field is required. And "No file chosen" appears near the "Choose file" button. All symptoms as if testify for absence of 'enctype="multipart/form-data"'. Look: in the network tab I can't see that the file was transmitted to the server. And if I try to serialize the form, there is no file mentioned. This seems not the problem of Django at all: I somehow can't correctly pass the form from the client to the server. The problem seems has something with html. Well, could you give me a kick here? ajax_form.html <form id="object_form" action="{{ action }}" {% autoescape off %}{{ enctype }}{% endautoescape %} method="post"> {% csrf_token %} <table> {{ form.as_table }} </table> <a id="submit" href="javascript:void(0)"><span class="glyphicon glyphicon-floppy-save" aria-hidden="true">Save</span></a> <a id="cancel" href="javascript:void(0)"><span class="glyphicon glyphicon-remove-sign" aria-hidden="true">Cancel</span></a> </form> views.py class PictureCreate(AddEnctypeMixin, ActionCreateMixin, GetForeignKeyFromUrlMixin, CreateView): model = Picture form_class = PictureForm foreign_key_from_url = "sheet" urls.py urlpatterns = … -
Form validation failing in Django
Im trying to get a form to validate with a Charfield but using the Select widget. Here is my view.py code: def mpld3plot(request): form = PlotlyPlotForm() form.fields['plot_file'].widget.choices = own_funcs.uploaded_files(string=False) if request.method == 'POST': print(form.is_valid()) if form.is_valid(): return HttpResponseRedirect('/mpld3') else: pass else: pass return render(request, 'evert/plot.html', {'plottype': 'MPLD3', 'form': form}) Below is my forms.py code: class Mpld3PlotForm(forms.Form): plot_file = forms.CharField(widget=forms.Select(choices=[('', 'a'), ('', 'b')])) The form does not validate on submit. I update the choices dynamically based on uploaded files. Any help would be appreciated. -
Querying haystack index with 30 million records, query slow, how to speed this up?
I have a django-haystack model index with around 30 million records in it, here's the index: class MoviePeopleIndex(indexes.SearchIndex, indexes.Indexable): text = indexes.NgramField(document=True, use_template=False) person_imdb_id = indexes.CharField(model_attr='people__imdb_id') token = indexes.CharField(model_attr='token', null=True) in_summary = indexes.CharField(model_attr='in_summary') movie_imdb_id = indexes.CharField(model_attr='movie__imdb_id') movie_type = indexes.CharField(model_attr='movie__type') movie_votes = indexes.IntegerField(model_attr='movie__votes', null=True) movie_rating = indexes.DecimalField(model_attr='movie__rating', null=True) movie_certification = indexes.CharField(model_attr='movie__certification', null=True) movie_genres = indexes.MultiValueField(null=True) def prepare_movie_genres(self, obj): return tuple(obj.movie.genres.all().values_list('name', flat=True)) def get_model(self): return MoviePeople def get_updated_field(self): return 'modified_on' The purpose of the index was so that I could query elasticsearch rather than postgres doing alot of joins. Here's an example query: SearchQuerySet().filter( person_imdb_id__in=primary_cast_imdb_ids, in_summary=True, movie_certification__in=['PG', '12'], movie_type__in=['some-type'], movie_genres__in=['genre-1', 'genre-2'], token=Raw('[* TO *]') ).exclude(movie_imdb_id__in=exclude_ids).models(MoviePeople) The problem is, queryies like the above are taking between 3000 and 4000 milliseconds to run (1 elasticsearch node with 16GB of RAM) What can I do to improve this? -
Django restframework, Django channels, Ionic 2 - websocket handshake error
I am currently working on a project using the technologies mentioned in the thread title. I got it all running from the browser (the application is hosted on heroku), but when I try to connect to the websockets from my Ionic 2 application, I always get an error establishing handshake. 2016-09-17T15:02:03.200133+00:00 app[web.1]: 2016-09-17 15:02:03,199 DEBUG Connection http.response!uvRVDyvolYEG did not get successful WS handshake. 2016-09-17T15:02:03.200498+00:00 app[web.1]: 2016-09-17 15:02:03,200 DEBUG WebSocket closed before handshake established 2016-09-17T15:02:03.169206+00:00 heroku[router]: at=info method=GET path="/1/" host=musicmashup-jukebox.herokuapp.com request_id=c46960d7-bb8f-45bf-b8be-5a934c771d96 fwd="212.243.230.222" dyno=web.1 connect=0ms service=7ms status=400 bytes=74 Now one idea was, that it could be a CORS problem. So I installed django-cors-middleware in hope this could solve the problem - well it did not. But I think the app does not add any headers to the Daphne server at all. At the moment I have no idea anymore, if the problem is on the client or on the server side. Has anyone experienced similar problems? -
How to save an output into Django Model
I am trying to save the output from "Django forms upload" into a django field called "phonelist". The steps for are as follows: 1. upload a CSV file on a django form. 2. On submitting the form, the content of the CSV will be saved, read and converted to a python LIST. My question is that I need to save this list in a django model field (an ArrayField -supported by PostGres) named "phonelist" Please I need help on how to get it done. Model.py from django.db import models from django.forms import ModelForm from django.db import models from django.contrib.postgres.fields import ArrayField class Upload(models.Model): name = models.CharField(max_lenght=80) #phonelistfile = models.FileField("phonelistfile", upload_to="phonelistfile/") upload_date=models.DateTimeField(auto_now_add =True) phonelist = ArrayField(models.TextField()) views.py from django.http import HttpResponseRedirect from django.shortcuts import render from .forms import UploadFileForm import csv import io # Imaginary function to handle an uploaded file. def handle_uploaded_file(f): with open('some/file/name.txt', 'wb+') as destination: for chunk in f.chunks(): destination.write(chunk) destination.close() #csvfile = f csvfile = io.TextIOWrapper(f) # Python 3 Only #dialect = csv.sniffer().sniff(codecs.EncodedFile(csvfile, "utf-8").read(1024)) dialect = csv.sniffer().sniff(csvfile.read(1024), delimiter=";,") #csvfile.open() csvfile.seek(0) #csvreader = csv.reader(codecs.EncodedFile(csvfile, "utf-8"), delimiter=',', dialect=dialect) csvreader = csv.reader(csvfile, dialect) return list(csvreader) def upload_file(request): if request.method == 'POST': form = UploadFileForm(request.POST, request.FILES) if form.is_valid(): handle_uploaded_file(request.FILES['file']) ###what code … -
Django - Zip file not created
i'm using the code above to generate zip file when download a file. But the zip file does not created. What did i miss? img_name = img.original_filename path = "/data/example/zip/" url = str (img.file) filepath = path+url filenames = [filepath] import zipfile import StringIO zip_subdir = "data" zip_filename = "%s.zip" % zip_subdir # Open StringIO to grab in-memory ZIP contents s = StringIO.StringIO() # The zip compressor zf = zipfile.ZipFile(s, "w") for fpath in filenames: # fdir, fname = os.path.split(fpath) zip_path = os.path.join(zip_subdir, fname) zf.write(fpath, zip_path) print fpath print zip_filename # Must close zip for all contents to be written zf.close() -
python django run bash script in server
I would like to create a website-app to run a bash script located in a server. Basically I want this website for: Upload a file select some parameters Run a bash script taking the input file and the parameters Download the results I know you can do this with php, javascript... but I have never program in these languages. However I can program in python. I have used pyQT library in python for similar purposes. Can this be done with django? or should I start learning php & javascript? I cannot find any tutorial for this specific task in Django. -
Django REST Framework Swagger - Authentication Error
I followed the instructions in the docs. So here's my view: from rest_framework.decorators import api_view, renderer_classes from rest_framework import response, schemas from rest_framework_swagger.renderers import OpenAPIRenderer, SwaggerUIRenderer @api_view() @renderer_classes([OpenAPIRenderer, SwaggerUIRenderer]) def schema_view(request): generator = schemas.SchemaGenerator(title='Bookings API') return response.Response(generator.get_schema(request=request)) And I added the following to my urls.py: url(r'^docs/', views.schema_view), When I went to the /docs/ page of my project, I got the following error: 401 : {"detail": "Authentication credentials were not provided."} http://127.0.0.1:8000/docs/?format=openapi In the browser console I got this message: Unable to Load SwaggerUI init.js (line 57) When I set the permission_classes of my schema_view to AllowAny, I was able to view my api docs. However, I'm not sure if this is the right way of doing this. Isn't there a way to login as an admin, or any other user to view the docs. Also, how do I provide the auth tokens when viewing this in the browser? Maybe I miss something in the docs. -
What is the recommended method for deploying Django settings to a production environment?
I have looked everywhere and all I could find was outdated or bits and pieces from several different sources, but none of them had a complete step of how to deploy Django to a production environment. What I would like to see is the recommended way to deploy Django utilizing git, local and production settings.py, .gitignore specifically for Django. How would I implement the settings to accommodate both environments and what should be added to the .gitignore file so that only necessary files are sent to the git repository? Please note that I want to know best practices and up to date methods of using local and production settings. Such questions arise when deploying Django apps. Where should the settings be stored? How can I properly implement the settings to accommodate both environments? What sensitive information should be separate from the main settings? -
Django models, adding new value, migrations
I worked with django 1.9 and added a new field (creation_date) to myapp/models.py. After that I run "python manage.py makemigrations". I got: Please select a fix: Provide a one-off default now (will be set on all existing rows) Quit, and let me add a default in models.py." I choose 1-st option and added value in wrong format '10.07.2016'. After this mistake I couldn't run "python manage.py migrate". So I decided to change models.py and add a default value "datetime.now". But after that I still have problems with "python manage.py makemigrations". I see such things like that: django.core.exceptions.ValidationError: [u"'10.07.2016' value has an invalid format. It must be in YYYY-MM-DD HH:MM[:ss[.uuuuuu]][TZ] format."] How to solve this problem? -
Uploading Large files to AWS S3 Bucket with Django on Heroku without 30s request timeout
I have a django app that allows users to upload videos. Its hosted on Heroku and the uploaded files stored on an S3 Bucket. I am using JavaScript to directly upload the files to S3 after obtaining a presigned request from Django app. This is due to Heroku 30s request timeout. Is there anyway that i can possibly upload large files through Django backend without using JavaScript and compromising the user experience? -
How to I pass values into dependent models with Factory Boy in Django?
Im' working on an open source django web app, and I'm looking to use Factory Boy to help me setting up models for some tests, but after a few hours reading the docs and looking at examples, I think I need to accept defeat and ask here. I have a Customer model which looks a bit like this: class Customer(models.Model): def save(self, *args, **kwargs): if not self.full_name: raise ValidationError('The full_name field is required') super(Customer, self).save(*args, **kwargs) user = models.OneToOneField( settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, related_name='customer', null=True ) created = models.DateTimeField() created_in_billing_week = models.CharField(max_length=9) full_name = models.CharField(max_length=255) nickname = models.CharField(max_length=30) mobile = models.CharField(max_length=30, default='', blank=True) gocardless_current_mandate = models.OneToOneField( BillingGoCardlessMandate, on_delete=models.SET_NULL, related_name='in_use_for_customer', null=True, ) I am also using the standard Django User Model, from django.contrib.auth. Here's my factory code: class UserFactory(DjangoModelFactory): class Meta: model = get_user_model() class CustomerFactory(DjangoModelFactory): class Meta: model = models.Customer full_name = fake.name() nickname = factory.LazyAttribute(lambda obj: obj.full_name.split(' ')[0]) created = factory.LazyFunction(timezone.now) created_in_billing_week = factory.LazyAttribute(lambda obj: str(get_billing_week(obj.created))) mobile = fake.phone_number() user = factory.SubFactory(UserFactory, username=nickname, email="{}@example.com".format(nickname)) In my case, I want to be able to generate a customer like so CustomerFactory(fullname="Joe Bloggs") And have the corresponding user generated, with the correct username, and email address. Right now, I'm getting this error: AttributeError: The … -
Elasticsearch: search on dynamically added fileds and filters
I am new to Elasticsearch, and want explain my task and advice on the best way to solve it with Elasticsearch. Example text: "Movie is bad. Actors plays terrible. But scenarios is good and ending is marvelous." The task is sentiment classification by multiple classes. So for this simple text i want define 2: 1) "Angry" and 2) "Cheerful" So, for very class i need full text search: Here for Angry class: { "query_string" : { "default_field" : "content", "query" : "bad AND terrible OR stupid" } } AND also for Cheerful class: { "query_string" : { "default_field" : "content", "query" : "good AND marvelous" } } Note, that there are a lot of classes with different search terms combinations. The result is all documents, matched all these classes of emotions with ability to filter by classes: i.e. sidebar filter to narrow not by terms in query but by category (show me content that belong to angry category, cheerful category etc). So this categories is not fields that already in database, they added in search query dynamically. Now i am reading https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl.html but still cannot combine posibilities with my task. Use Django + Haystack -
Django Slugfield removing articles ('the', 'a', 'an', 'that')
I'm using django 1.10 and I have this setup: class Chapter(models.Model): title = models.CharField(max_length=200, unique=True) slug = models.SlugField(unique=True) date_completed = models.DateTimeField(blank=True, null=True) completed = models.BooleanField(default=False) def __str__(self): return self.title In the admin, I have : class ChapterAdmin (admin.ModelAdmin): list_display = ('title','slug', 'date_completed',) list_filter = ['title', 'date_completed'] prepopulated_fields = {"slug": ("title",)} I noticed that when I create a chapter with a title like "A boy and the pig", I get "boy-and-pig" as the slug. Django is stripping of the articles from my slug. Why is this so and what's the way around it? -
How to design an archive of individual records in a database? In a school records management system
How do I design a table that can hold all student grade records for an entire academic career? I figured once the current semester ends, there's no need to keep grade records in separate tables. Why not move it to a new model -
Why is django channels inmemory backend restricted to capacity 10
Is there any reason (except from memory space) why django channels capacity settings for the inmemory backend is set to 10 instead of (say 10000)? The same question for redis backend: Here we have a default capacity of 100 and we think about increasing this value. Is there any severe problem in doing so? -
Django REST framework browsable API sends GET again after POST
settings.py has following code. INSTALLED_APPS = ( 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'user_management', 'rest_framework' ) REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.SessionAuthentication', ), 'DEFAULT_RENDERER_CLASSES': ( 'rest_framework.renderers.BrowsableAPIRenderer', 'rest_framework.renderers.JSONRenderer', ), 'DEFAULT_PARSER_CLASSES': ( 'rest_framework.parsers.JSONParser', ), } No extra middleware because of DRF. My urls.py has from django.conf.urls import url from . import views urlpatterns = ( url(r'^signup', views.Users_Signup.as_view()), ) Finally my views.py has class Users_Signup(APIView): def post(self, request, format = None): params = json.loads(request.body) try: validate(params, user_management_valid) except Exception as e: response = {"status":"error", "message": str(e)} return HttpResponse(json.dumps(response), content_type='application/json') ... (so on) Two things I observed were I have only OPTIONS button visible in browsable API view. In the below POST form, if I send the data, the POST request goes to the API, returns the correct response but the browsable API page reloading causing the response to be lost. I am again presented with the GET page. This is exactly the error I am facing. But there is no solid answer. -
Django Admin panel does not allow to save field which is unique=True,null=True,blank=True
I have a field like this in my model. phone = models.CharField( unique=True, max_length=10, null=True, blank=True, db_index=True, help_text=_("User's phone number.")) It works fine but when I try to save from admin panel it gives error on this field User with this Phone already exists. I tried to define save_model() in admin.py for user but it does not get called. def save_model(self, request, obj, form, change): if not obj.phone: obj.phone = None if not obj.email: obj.email = None obj.save() But this does not work. Can anyone help ? -
Django, send response from two servers
The idea is that I'm sending data with ajax to my class view, when class view receives data it sends it to an external server, this server gives me a response that I display in my template view by success function in my ajax post method. It works awesome, no problem at all. However, when my view gets POST request it also creates some data in my DataBase via module (it is for search history records). I save this data, then I want to get all this data and display it. So, what I need is to return two sets of data, one is json for success function of my ajax method and another one from my database. I have no idea how to put them together, or how to send them separately... Is there any ideas? view class user_id = request.session['user_id'] jresponse = response.json() query = QueryHistory(userID = user_id, queryID = jresponse['query'], query = keyword) query.save() query_list = QueryHistory.objects.all() return HttpResponse(json.dumps(jresponse), content_type="application/json") saerch.js success : function(response) { $('#id_keyword').val(''); // remove the value from the input console.log(response); // log the returned json to the console if(response.hasOwnProperty('results')){ for (var i = 0; i < response.results.length; i++){ $("#search-table").append("<tr><td>alldatagoeshere</td></tr>"); } } // handle … -
Django Select Widget Options Won't Update
I have an integer field in my forms.py for which I use a Select as a widget: times = forms.IntegerField(min_value=1, max_value=max_options, required=True, widget=forms.Select( attrs={ 'class': 'form-control', 'placeholder': 'How many times?' }, choices=[(x+1, x+1) for x in range(max_options)]), label='How many times?' where max_options is a variable which I keep in the db as a singleton with django-solo. The problem is, when I update max_options from admin, the options on the view don't get updated. I have tried to move it into __init__: def __init__(self, *args, **kwargs): super(SomeCreateForm, self).__init__(*args, **kwargs) self.fields['times'].choices = [(x+1, x+1) for x in range(max_options)] as suggested here, but to no use. I have tried to remove caching, but again, didn't work. Any thoughts? -
How can I optimize this really slow query generated by Django?
here's my Django ORM query: Group.objects.filter(public = True)\ .annotate(num_members = Count('members', distinct = True))\ .annotate(num_images = Count('images', distinct = True))\ .order_by(sort) Unfortunately this is taking over 30 seconds even with just a few dozen Groups. My database backend is Postgres and here's the SQL and explain: Executed SQL SELECT ••• FROM "astrobin_apps_groups_group" LEFT OUTER JOIN "astrobin_apps_groups_group_members" ON ( "astrobin_apps_groups_group"."id" = "astrobin_apps_groups_group_members"."group_id" ) LEFT OUTER JOIN "astrobin_apps_groups_group_images" ON ( "astrobin_apps_groups_group"."id" = "astrobin_apps_groups_group_images"."group_id") WHERE "astrobin_apps_groups_group"."public" = true GROUP BY "astrobin_apps_groups_group"."id", "astrobin_apps_groups_group"."date_created", "astrobin_apps_groups_group"."date_updated", "astrobin_apps_groups_group"."creator_id", "astrobin_apps_groups_group"."owner_id", "astrobin_apps_groups_group"."name", "astrobin_apps_groups_group"."description", "astrobin_apps_groups_group"."category", "astrobin_apps_groups_group"."public", "astrobin_apps_groups_group"."moderated", "astrobin_apps_groups_group"."autosubmission", "astrobin_apps_groups_group"."forum_id" ORDER BY "astrobin_apps_groups_group"."date_updated" ASC Time 30455.9268951 ms QUERY PLAN GroupAggregate (cost=5910.49..8288.54 rows=216 width=242) (actual time=29255.329..30269.284 rows=27 loops=1) -> Sort (cost=5910.49..6068.88 rows=63357 width=242) (actual time=29253.278..29788.601 rows=201888 loops=1) Sort Key: astrobin_apps_groups_group.date_updated, astrobin_apps_groups_group.id, astrobin_apps_groups_group.date_created, astrobin_apps_groups_group.creator_id, astrobin_apps_groups_group.owner_id, astrobin_apps_groups_group.name, astrobin_apps_groups_group.description, astrobin_apps_groups_group.category, astrobin_apps_groups_group.public, astrobin_apps_groups_group.moderated, astrobin_apps_groups_group.autosubmission, astrobin_apps_groups_group.forum_id Sort Method: external merge Disk: 70176kB -> Hash Right Join (cost=15.69..857.39 rows=63357 width=242) (actual time=1.903..397.613 rows=201888 loops=1) Hash Cond: (astrobin_apps_groups_group_images.group_id = astrobin_apps_groups_group.id) -> Seq Scan on astrobin_apps_groups_group_images (cost=0.00..106.05 rows=6805 width=8) (actual time=0.024..12.510 rows=6837 loops=1) -> Hash (cost=12.31..12.31 rows=270 width=238) (actual time=1.853..1.853 rows=323 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 85kB -> Hash Right Join (cost=3.63..12.31 rows=270 width=238) (actual time=0.133..1.252 rows=323 loops=1) Hash Cond: (astrobin_apps_groups_group_members.group_id = astrobin_apps_groups_group.id) -> Seq Scan on astrobin_apps_groups_group_members … -
Streaming mp3 files in Django through Nginx
I have a website based on Django framework. I am running website via Nginx webserver (uWSGI,Django,Nginx). I want to stream mp3 files on my website with Accept-Ranges header. I want to serve my mp3 files with Nginx. I need my API to look like this http://192.168.1.105/stream/rihanna This must return mp3 file with partial download (Accept-Ranges). My mp3 files are stored in : /home/docker/code/app/media/data/ When I run the server with these configurations and browse to 192.168.1.105/stream/rihanna , Django returns 404. My Nginx conf: # mysite_nginx.conf # the upstream component nginx needs to connect to upstream django { server unix:/home/docker/code/app.sock; # for a file socket # server 127.0.0.1:8001; # for a web port socket (we'll use this first) } # configuration of the server server { # the port your site will be served on, default_server indicates that this server block # is the block to use if no blocks match the server_name listen 80 default; include /etc/nginx/mime.types; # the domain name it will serve for server_name .example.com; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { autoindex on; sendfile on; sendfile_max_chunk 1024m; internal; #add_header …