Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Mock Django QuerySet - unit test
I'm testing a Django application with unit test, and I need to mock a QuerySet like this one. def function_to_be_tested(): qs = myModel.objects.get_last(param_1, param_2) .filter(category__in=[category_1, category_2]) return qs myModel class myModel(models.Model): id = models.IntegerField(primary_key=True) category = models.CharField(max_length=255) ... objects = myModelManager() myModelManager class myModelManager(models.manager): def get_last(self, param_1, param_2): # Do some stuff Following my attempt, which uses mock with mock.patch('path.of.module.myModel.objects.get_last') as my_model_data: my_model_data.return_value = myModel.objects.none() result = module.function_to_be_tested() This case works fine, because returns an empty list. Now, i would like mock the QuerySet in order to return some data. with mock.patch('path.of.module.myModel.objects.get_last') as my_model_data: mock_data = mock.MagicMock(spec=myModel) mock_data.category = 'category_x' my_model_data.return_value = [mock_data] result = module.function_to_be_tested() This attempt fails with the following error: AttributeError: 'list' object has no attribute 'filter' I understand that this happens because i'm mocking only the get_last function and not all QuerySet. I tried to add also filter to the path like this: with mock.patch('path.of.module.myModel.objects.get_last.filter') as my_model_data: but raises AttributeError: <bound method myModelManager.get_last of <path of.myModelManager object at 0x04EF49F0>> does not have the attribute 'filter' Is it possible mock this kind of QuesrySet? if yes, what am I missing? -
Django CSRF cross site AJAX issue
I have a backend server at localhost:8000 and a frontend server at localhost:3000. Backend is Django and I have the corsheaders package installed and I make GET requests without issues. When I open my site, the first GET request successfully sets a CSRF token (I can see in dev tools). I have this in settings.py: CORS_ORIGIN_WHITELIST = ( 'localhost:3000' ) Now I'm trying to make a POST request and I'm constantly turned down by the server: 403 - Forbidden (CSRF cookie not set.): /myapp/myview/ and this is what my JS looks like: let csrftoken = utils.getCookie('csrftoken'); // logged this on the console, the token is really here jQuery.ajaxSetup({ beforeSend: function(xhr, settings) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } }); // this might be wrong - I did read it is used only if it's not cross site, but I still gave it a try jQuery.ajax({ url: url, type: 'POST', data: {attr: value, csrfmiddlewaretoken: csrftoken} })... I've tried only with csrfmiddlewaretoken in the AJAX data. I've tried only with settting the request header. I've tried both. Still no luck. I'm obviously missing something; I've never before tried doing a POST in Django across sites. Any suggestions? -
django queryset orm - from same table fetch data in particular format
I am having my models.py is something like this CHOICES = ( (1,'Test1'), (2,'Test2'), (3,'Test3'), (4,'Test4'), (5,'Test5'), ) class A(models.Model): x = models.CharField('A', max_length=256) def __unicode__(self): return self.x class B(models.Model): emp = models.ForeignKey(User, verbose_name='emp') y = models.ForeignKey(A, verbose_name='a') z = models.IntegerField('testing', choices = CHOICES) I have some data inside these tables in database. User.objects.values_list('username', flat=True) [u'user1', u'user2', u'user3'] A.objects.values_list('x', flat=True) [u'python', u'java', u'php', u'javascript'] B.objects.values_list('emp__username', 'y__x', 'z') [(user1', u'python', 3L), (user2', u'php', 5L), (u'user1', u'javascript', 5L), (u'user3', u'python', 5L), (u'user3', u'java', 5L)] I want output something like this employees = { 'user1':{ 'Test5':"javascript", 'Test4': "", 'Test3':"python", 'Test2':"", 'Test1':"" }, 'user2':{ 'Test5':"php", 'Test4': "", 'Test3':"", 'Test2':"", 'Test1':"" }, 'user3':{ 'Test5':"python, java", 'Test4': "", 'Test3':"", 'Test2':"", 'Test1':"" }, } I can do it but with multiple database calls. Can we achieve the same with minimum queryset / database calls? Thanks -
Is it necessary to add `index_together` same as `ordering` in Django model meta?
Does Django will handle ordering smartly by adding union index? Like: from django.db import models class Musician(models.Model): first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) instrument = models.CharField(max_length=100) class Meta: ordering = ['first_name', 'last_name', 'instrument'] index_together = ['first_name', 'last_name', 'instrument'] Is there performance difference with/without the last line index_togeter declaration? -
How to remove QueryDict from Dictionary in python2.7?
This is my dictionary <QueryDict: {u'karnataka': [u'bangalore', u'tumkur']}> I want to remove the QueryDict and i want as {u'karnataka': [u'bangalore', u'tumkur']} -
New list_display after list_filter enabled Django 1.10
I have been playing around with the Django admin pages and got stuck and can't seem to find a solution in the documentation. I have defined three different models: Location, RestaurantFeature and Employee. There three models are all linked to one another. This question is specific for my Employee model: class Employee(models.Model): restaurant = models.ForeignKey(RestaurantFeature, on_delete=models.CASCADE) first_name = models.CharField(max_length=50) last_name = models.CharField(max_length=50) age = models.IntegerField() address = models.CharField(max_length=80) prior_experience = models.BooleanField(default=False) hour_contract = models.IntegerField() hourly_salary = models.FloatField() def _get_full_name(self): return '%s %s' % (self.first_name, self.last_name) full_name = property(_get_full_name) def __str__(self): return "%s" % self.full_name class Meta: verbose_name_plural = "Employees" The custom admin layout which I have: class EmployeeAdmin(admin.ModelAdmin): list_display = ('full_name','restaurant','prior_experience',) list_filter = ('restaurant',) search_fields = ('first_name', 'last_name', ) admin.site.register(Employee, EmployeeAdmin) This works great! The display which is defined works and so does the filter. What I would like: is a new list_display once the restaurant filter is enabled! This way more detailed information could be provided when required. Could someone please help me out to define a new list_display once the list_filter is applied? Thanks! MORE CLEARER: The list_display now defines the full name, restaurant and prior experience. I would like these variables to change to full name, restaurant, … -
Django migrations: relation does not exist
I have a working site and database on my dev server which I am trying to setup on a live server. I am doing the following: Git clone the repository from the dev server to the live server Create empty database on live server (Posgres) Update settings.py on the live server for all the relevant database etc settings delete all migration files/folders and delete all *.pyc files python manage.py makemigrations I get the error: django.db.utils.ProgrammingError: relation "myapp_mytable" does not exist. I can't seem to get the initial migration to happen. The only solution I have found is to go into my settings.py file and comment out all my apps within INSTALLED_APPS and go into my main urls.py file and comment out all my urls. After commenting out those sections, I am able to do the initial migration. After that I can then uncomment my apps and start migrating them one by one, ie: python manage.py makemigrations appname then python manage.py migrate So I have a workaround but it is far from ideal. Surely there is a way to tell django that I have created a brand new empty database so it needs to do the initial migration first. I am … -
Subquering in Django
I have a Django 1.9 project implementing small chat app. All messages from a certain recipient are grouped into dialogs, so the models are defined as follows: class Dialog(models.Model): # Some fields class Message(models.Model): dialog = models.ForeignKey(Dialog, ...) text = models.TextField() is_read = models.BooleanField(default = False) My goal is to render a template with a table that renders dialogs. And for each dialog in the table, I need to see the number of unread messages and the text of the last message. To illustrate, consider mock-data below: Input: id dialog_id message is_read 1 1 Hello, sir false 2 1 My name is true 3 1 Jack true 4 2 This site false 5 2 is perfect false 6 2 Cheers false Desired output: dialog_id last_message_in_dialog unread_messages_count 1 Jack 1 2 Cheers 3 In pure mysql, I would write a query like this: select a.dialog_id, text as last_message_in_dialog, (select count(*) from message where dialog_id = a.dialog_id and is_read = false) as unread_messages_count from message a where id in (select max(id) from message group by dialog_id) In Django terms, I have the code below: max_id_qs = Message.objects.\ values('dialog__id').\ annotate(max_id = Max('id'),).values('max_id') qs = Message.objects.filter(id__in = max_id_qs).\ values('dialog__id', 'text') The problem now is … -
Django Media url returns 404 NOT FOUND
I have a django project. In order to set up my media url, I followed the django doc Django doc - Managing static files: For example, if your MEDIA_URL is defined as /media/, you can do this by adding the following snippet to your urls.py: from django.conf import settings from django.conf.urls.static import static urlpatterns = [ # ... the rest of your URLconf goes here ... ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) my settings.py: # .... BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_URL = 'http://127.0.0.1:8000' # ... MEDIA_URL_REL = '/media/' MEDIA_URL = BASE_URL + MEDIA_URL_REL MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') my urls.py: from django.conf import settings from django.conf.urls import url, include from django.conf.urls.static import static urlpatterns = [ # ... ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) My uploaded files are found in media root, but when I access media url [ http://127.0.0.1:8000/media/proof/img.pdf]for it, it returns HTTP 404 NOT FOUND. -
How to fetch last inserted record for perticular is?
Apologies, I am completely new to Django. My question is that i have 20 records in my datbase table and suppose 10 record is of same id and i want to fetch last inserted record for that id i have date column in my table please help me...tahnks in advance -
Are prefetched tasks in celery being acknowledged?
I have following setup: RabbitMQ broker + Django Celery with CELERYD_PREFETCH_MULTIPLIER=32 (I have a lot of small task thus prefetching them makes a lot of sense from performance standpoint) CELERY_ACKS_LATE=False (tasks are not idempotent) I run celery in docker container, so when I rebuild docker celery workers are not gracefully shut down. This is ok, if tasks are not acknowledged as broker will sent them back once workers be up again in new docker container, but in other case they - will be lost. In flower admin panel prefetched tasks have status received. I had carefully read official documentation and related question and intuitively I feel that prefetched tasks in my setup are acknowledged. Is it so? -
Matplot lib image properties in web (Django)
I'd like to output the image to the web created in matplotlib having the very same functionality like it has on desktop when you run the image.show(), for example scaling, moving along the plot more thoroughly. I've checked out #stack and got old post only offering static images or gif or matplotlib.animate() I aslo had a look at matplotlib widgets, but those are for desktop GUI only as far as I can see. Please share some experience or ideas regarding how can I achieve it. Thanks -
Django admin - inlines, remove label from display
I am using a tabular inline in my admin page, and the first value of the field has odd sizing and a label under neath it, as per below photo. I thought it would have been removed when I added the fields in admin, but its still there.. error: admin: class ComponentsInline(admin.TabularInline): model = Components fields = ('part_no', 'serial_no', 'description') class InventoryAdmin(admin.ModelAdmin): search_fields = ['get_site','device'] list_display = ('get_site','device') ordering = ('location',) fieldsets = ( ('Device Details',{ 'fields' : ( ('location','device','contract_id'), ('contract_start_date','contract_end_date',), ) }), ) inlines = [ ComponentsInline, ] def get_site(self, obj): return obj.location.location get_site.admin_order_field = 'site_data' #Allows column order sorting get_site.short_description = 'Site Name' #Renames column head class Meta: ordering = ('location__location',) admin.site.register(Inventory, InventoryAdmin) model: class Inventory(models.Model): location = models.ForeignKey(SiteData, on_delete=models.PROTECT) device = models.CharField(max_length=200) contract_id = models.CharField(max_length=200,verbose_name="Contract No", blank=True,null=True) contract_start_date = models.DateField(blank=True,null=True) contract_end_date = models.DateField(blank=True,null=True) class Meta: verbose_name = "Inventory" verbose_name_plural = "Inventory" def __unicode__(self): return '{0} - {1}'.format(self.location, self.device) class Components(models.Model): device = models.ForeignKey(Inventory, on_delete=models.PROTECT) part_no = models.CharField(max_length=200) serial_no = models.CharField(max_length=200) description = models.CharField(max_length=200,blank=True,null=True) class Meta: verbose_name = "Components" verbose_name_plural = "Components" def __unicode__(self): return '{0} - {1}'.format(self.device, self.part_no) also, is it possible to have the components inline capitalized instead of all capitals? -
Django annotate price per month
I want to refer this to my other Question here on SO, so I'm trying to display price per month and make a table with 12 months, a row with total project income per month, so I have successfully calculated my price using context["price_aux"] = Project.objects.annotate().aggregate(Sum("price_aux")) I can display my data in a table and just make a row of total for that, but as you can see from my previous question I want to annotate this per month and make a table and then display total for month, but I have really hard time understanding how can I extract this from my database, so what is the proper way for doing this in django==1.8+, I see that in recent version they have added Extract, but I can not use it in 1.8, second thing is django extra they stated here Use this method as a last resort, so can you explain me how can I make this work. -
Django query to list the count of field values distinctly
I have a model which has a field named 'state'. It has values like 'completed', 'in_progress', 'failed', 'created'. I need to know if I can write a django query which will through me a list like {'completed': 2, 'in_progress': 5, 'failed': 0, 'created': 2} for the table values id | order_id | state --------------------------- 1 | 23 | completed 2 | 23 | completed 3 | 23 | in_progress 4 | 23 | created 5 | 23 | created 6 | 23 | in_progress 7 | 23 | in_progress 8 | 23 | in_progress 9 | 23 | in_progress I tried running the below query order_items = OrderItems.objects.filter(order=order) order_states = order_items.filter( state__in=['in_progress', 'completed', 'failed', 'created'] ).values('state').annotate(Count('state')) But it gave me a list which came up like this [{'state': u'completed', 'state__count': 8}, {'state': u'failed', 'state__count': 1}, {'state': u'in_progress', 'state__count': 1}] -
Django CMS: How can I show from database in setting window
I want to show data in dropdown from database in setting window. Right now I am showing from hard-coded array. MY_CHOICES = ( ('a', 'Cat1'), ('b', 'Cat2'), ) categories = models.CharField("Survey", help_text="Select Survey", choices=MY_CHOICES, max_length=3, blank=True) How to fill categories from db value? -
Apache2- How to call multiple project using django
apache2.conf Include sites-enabled/ WSGIPythonPath /var/www/project1/project1 #WSGIPythonPath /var/www/project2/project2 ServerName 107.170.00.90 <Directory "/var/www/"> options Indexes FollowSymLinks AllowOverride None # Require all granted </Directory> /etc/apache2/sites-available$ vi default <VirtualHost *:80> ServerName testdomian.com ServerAlias www.testdomian.com ServerAdmin testdomian@gmail.com Errorlog /var/www/logs/error.log CustomLog /var/www/logs/custom.log combined WSGIScriptAlias /project2 /var/www/project2/project2/authtest/wsgi.py <Directory "/var/www/project2/project2"> Options FollowSymLinks AllowOverride None </Directory> WSGIScriptAlias / /var/www/project1/project1/blog/wsgi.py # Set access permission <Directory "/var/www/"> Options FollowSymLinks AllowOverride None </Directory> </VirtualHost> if i call www.testdomian.com is working fine and then www.testdomian.com/project2 it's showing same page. How to solve this problem. -
how can i use a single code from different views by sending variable in django
I want to delete images everytime the user updates by calling the same code.Can i send a variable for the image path and execute the same code from different views? -
How to take drop down menu value to views.py in django?
I am using drop down menu.I want to take selected option on views.py but as i am passing id as value and when i use request.POST.get("oprator") to get selected item's name..it is displaying id instead of name please check below code on template and when i take value=operators.name it gives me operator name but i want id alsO for passing to another function... should i take both and splits them if yes pls suggest solution <select class="form-control" id="sel1" name="oprator" data-live-search="true" onchange="getOprInfo(this.value)" > <option value=" ">Select Operator</option> {%for operator in operators%} <option value="{{operator.id}}">{{operator}}</option> {%endfor%} </select> -
How to iterate dictionaries and save in to the database in python2.7?
This is my dictionary {u'krishna': [u'vijayawada', u'gudivada', u'avanigada']} I want to iterate items and save in the database,my Models is class Example(models.Model): district = models.CharField(max_length=50,**optional) taluk = models.CharField(max_length=20,**optional) It should save as: ----------------------------- |district | taluk | |-----------|--------------- | |krishna | vijayawada | |-----------|----------------| |krishna | gudivada | |----------------------------| |krishna | avanigada | ------------------------------ -
Django Admin Serving Old static design and not new
I just launched my site and when I go to the Ip address of the site, like the actual 11 integers, I get the new clean look of the admin, but when I go to the actual address like thissite.com/admin/ I get a different design of the admin, that has a much older look. I have collected the static files on the server and everything else is being served correctly. I would like to know why this is happening! Thank You! -
Unable to load CSS files into Django 1.10
I am new to Django. I am developing a dynamic website which include CSS in it. I am using template inheritance.When I try to include CSS files I am getting a "404 Page not Found" error for CSS file. Please help me. Here's my project structure: project structure Here's my urls.py file: from django.conf.urls import url, include from django.contrib import admin from django.contrib.staticfiles.urls import staticfiles_urlpatterns from . import views urlpatterns = [ url(r'^courses/', include('courses.urls')), url(r'^admin/', admin.site.urls), url(r'^$', views.hello_world), ] urlpatterns += staticfiles_urlpatterns() Here's my settings.py file: import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/1.10/howto/deployment/checklist/ # SECURITY WARNING: keep the secret key used in production secret! SECRET_KEY = '5%#*z=7o5iap!lnr7(%*(2rsl#*b-ufjy!ia$8z08d(6d8!j-#' # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True ALLOWED_HOSTS = [] # Application definition INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'courses', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] ROOT_URLCONF = 'learning_site.urls' TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': ['templates',], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] WSGI_APPLICATION = 'learning_site.wsgi.application' # Database # https://docs.djangoproject.com/en/1.10/ref/settings/#databases … -
Pass + operator in requests.get method
I am trying to send + in URL request but it replaced with empty space. import urllib args = {"key": "+ 919191xxxx", "secret": "+ 98989"} url = "http://127.0.0.1:5000/data?{}".format(urllib.urlencode(args)) Anyone can help me to resolve this? -
Django setting many_to_many object while doing a bulk_create
I am using Django 1.9 and am trying the bulk_create to create many new model objects and associate them with a common related many_to_many object. My models are as follows #Computational Job object class OT_job(models.Model): is_complete = models.BooleanField() is_submitted = models.BooleanField() user_email = models.EmailField() #Many sequences class Seq(models.Model): sequence=models.CharField(max_length=100) ot_job = models.ManyToManyField(OT_job) I have thousands of Seq objects that are submitted and have to be associated with their associated job. Previously I was using an iterator and saving them in a for loop. But after reading realized that Django 1.9 has bulk_create. Currently I am doing DNASeqs_list = [] for a_seq in some_iterable_with_my_data: # I create new model instances and add them to the list DNASeqs_list.append(Seq(sequence=..., )) I now want to bulk_create these sequence and associate them with the current_job_object. created_dnaseqs = Seq.objects.bulk_create(DNASeqs_list) # How do I streamline this part below for a_seq in created_dnaseqs: # Had to call save here otherwise got an error a_seq.save() a_seq.ot_job.add(curr_job_obj) I had to call "a_seq.save()" in for loop because I got an error in the part where I was doing "a_seq.ot_job.add(curr_job_obj)" which said ....needs to have a value for field "seq" before this many-to-many relationship can be used. Despite reading the other questions … -
d3 tsv not working with django
I know this question is asked multiple times, but my problem seems to be unique: my django view: def data(request): event = Event.objects.get(pk=request.GET.get('epk')) occurrences = event.occurrences.all() response = HttpResponse(content_type='text/tsv') response['Content-Disposition'] = 'filename="data.tsv"' response.write('date\tclose\n') for occ in occurrences: response.write('%s\t%s\n' % (occ.timestamp.strftime('%Y-%m-%d %H:%M:%S'), occ.counter)) return response and js: <script src="https://d3js.org/d3.v4.min.js"></script> <script> var svg = d3.select("svg"), margin = {top: 20, right: 20, bottom: 30, left: 50}, width = +svg.attr("width") - margin.left - margin.right, height = +svg.attr("height") - margin.top - margin.bottom, g = svg.append("g").attr("transform", "translate(" + margin.left + "," + margin.top + ")"); var parseTime = d3.timeParse("%Y-%m-%d %H:%M:%S"); var x = d3.scaleTime().rangeRound([0, width]); var y = d3.scaleLinear().rangeRound([height, 0]); var line = d3.line().x(function (d) { return x(d.date); }).y(function (d) { return y(d.close); }); d3.tsv("{% url 'data' %}?epk={{ event.id }}", function (d) { d.date = parseTime(d.date); d.close = +d.close; return d; }, function (error, data) { if (error) throw error; x.domain(d3.extent(data, function (d) { return d.date; })); y.domain(d3.extent(data, function (d) { return d.close; })); g.append("g") .attr("transform", "translate(0," + 1 + ")") .call(d3.axisBottom(x)) // <---- error place .select(".domain") .remove(); g.append("g") .call(d3.axisLeft(y)) .append("text") .attr("fill", "#000") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", "0.71em") .attr("text-anchor", "end") .text("Occurs"); g.append("path") .datum(data) .attr("fill", "none") .attr("stroke", "steelblue") .attr("stroke-linejoin", "round") .attr("stroke-linecap", "round") .attr("stroke-width", 1.5) .attr("d", line); …