Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How do I force elasticsearch to index the test database in django unit tests?
I'm using elasticsearch in my django app through the haystack module, and I've set up two indexes, main-index and test-index. In addition to my normal development database local_dev, the test cases build and drop a test_local_dev. I would like for main-index to only index local_dev and for test-index to only index test_local_dev, but I don't see where in my settings to tell test-index to use a different database. Versions: elasticsearch 2.4.6 django-haystack==2.7.0 django-haystack-elasticsearch==0.1.0 elasticsearch==2.4.1 elasticsearch-dsl==6.1.0 elasticsearch2==2.5.0 -
Django unexpected characters in URL
I am making a blog and trying to make pagination operations on post listing page. When I run my app, my URL contains unexpected characters. For example ; http://127.0.0.1:8000/blog/%5E$ I couldn't understand why %5E$ is there. Here my urls.py (this is in blogapp): from django.urls import path,include from django.contrib import admin from . import views urlpatterns = [ path(r'^$',views.getPosts,name="bloghome"), path(r'^(?P<selected_page>\d+)/?$',views.getPosts,name="bloghome"), path('<slug>',views.postDetailPage,name="post_detail") ] getPost function in views.py def getPosts(request,selected_page=1): # latest_post = Posts.objects.get(id=1) posts = Posts.objects.all().order_by('-pub_date') pages = Paginator(posts,5) #Show 5 post per page try: returned_page = pages.page(selected_page) except EmptyPage: returned_page = pages.page(pages.num_pages) #content = pages.page(selected_page) return render(request,'blog.html',{'page':returned_page, 'posts':returned_page.object_list }) And finally, this bloglist page is being entered from homepage with <a> tag. Here it's one line of code: <a href="{% url 'bloghome'%}">Blog</a> -
Query a certain number of objects django
I am creating a website that allows user to follow certain stocks and view articles related to what they follow. In 'index.html' I would only like to show the last 5 Articles for each Stock the user follows. How can this be accomplished? models.py: class Stock(models.Model): name = models.CharField(max_length = 50) ticker = models.CharField(max_length = 50) def __str__(self): return self.name class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) followed_stocks = models.ManyToManyField(Stock, blank=True) def __str__(self): return self.user.username @receiver(post_save, sender=User) def update_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) instance.profile.save() class Article(models.Model): stock = models.ForeignKey(Stock, on_delete=models.CASCADE, default = 0 ) title = models.CharField(max_length = 200) url = models.URLField() description = models.TextField() def __str__(self): return self.title views.py: def index(request): stocks_user_follows = list(request.user.profile.followed_stocks.all()) articles_to_display = Article.objects.filter(stock__in = stocks_user_follows) return render(request, 'core/index.html', {'stocks_user_follows':stocks_user_follows, 'articles_to_display':articles_to_display}) index.html: <div class="container"> <div class="row"> {% for stock in stocks_user_follows %} <div class="col-md-4"> <div class="card"> <h2>{{ stock }}</h2> <ul> {% for article in articles_to_display %} {% if article.stock == stock %} <li><a href="{{article.url}}">{{ article.title }}</a></li> {% endif %} {% endfor %} </ul> <a href="#" class="btn btn-light w-50 mx-auto mb-4">All {{ stock }} News</a> </div> </div> forms.py: class StockFollowForm(forms.Form): stocks = forms.ModelMultipleChoiceField(required =False, widget=forms.CheckboxSelectMultiple, queryset=Stock.objects.all(), label= "", ) -
Django Rest Framework: logic in view vs serializer
I have a case where I'm saving a user's AWS credentials, but before I do that I want to make sure they work by performing a simple call with boto. Right now I'm overloading the view's create() method and putting it in there but in a way this is "data validation" and therefore maybe it should be in the serializer's create(). Is that a better place for it? Edit: I don't believe this is a duplicate of Django Rest Framework Business Logic because the question and answers are not specific to view vs serializer placement. -
simple user login and file upload website in Django framework
I'm trying to create a website on my old laptop in which users can login and upload/download their files(pdf and image formats).All the network and web server configuration was done now I'm looking in to Django python to create my website.the thing is I'm beginner to Django and also python.So any books to refer and any learning tutorials to make my project live will be appreciated. -
JSONField, Django, PostgreSQL, retrieving data
I have a JSONField which seems to be successfully storing a JSON as a string in my database. How do I retrieve this data as a dictionary? class Package: node = JSONField(null=True, blank=True) packageInstance = Package.objects.get(id=packageId) print(packageInstance.node) -
Django - Override model save()
In our project we use soft delete (is_deleted attribute on a model). Before creating a new model I want to first check if it was already exist and deleted and if so - set is_deleted=False and save the already exists model. So I decided to override the model 'save' method. (not the View Create since it should work not only for requests) The problem is that 'save()' is called for all actions (create, update, delete). Is there a way I can call the overridden method only on create? class Model(BaseModel): title = models.CharField(max_length=200) identifier = models.CharField(max_length=15) def save(self, *args, **kwargs): try: existing_model = Model.objects.active_and_deleted().get( identifier=self.identifier) if self.is_deleted is False: existing_model.is_deleted = False existing_model.title = self.title existing_model.created_at = self.created_at existing_model.updated_at = self.updated_at existing_model.created_by = self.created_by existing_model.deleted_at = None super(Model, existing_model).save(args, kwargs) except Model.DoesNotExist: # in case the Nodel wasn't already exist - continue pass super(Model, self).save(*args, **kwargs) -
How to create a subform for Django ModelForm
I'm having a problem to add a ManyToMany field with ModelForm, the problem is that I don't know how to create a subform to add this data to my primary form. I want to create a subform to be saved when I click a button, and I want the data that was saved to be selected to use in the primary form. my model class Teste(models.Model): name = models.CharField(max_length=255) parent_institution_name = models.CharField(max_length=255) laboratory_departament = models.CharField(max_length=255, null=True, blank=True, verbose_name="Laboratório") cep = models.CharField(max_length=255, verbose_name="Cep") cnpj = models.CharField(max_length=255, verbose_name="CNPJ") lat = models.FloatField(blank=True, null=True) lng = models.FloatField(blank=True, null=True) institution_name = models.CharField(max_length=255, null=False, verbose_name="Nome da instituição") parent_institution_name = models.CharField(max_length=255, blank=True, null=True, verbose_name="Nome da instituição vinculada") coordinator = models.CharField(max_length=255, verbose_name="Pessoa Responsavel") email = models.CharField(max_length=255, verbose_name="E-mail") website = models.CharField(max_length=255, blank=True, null=True, verbose_name="Website") rad_operating_time = models.IntegerField(verbose_name="Tempo de atuação ") research_line = models.ManyToManyField('researcher.ResearchLines') my ModelForm class TestForm(forms.ModelForm): class Meta: model = Test exclude = ('employee_number', ' revenues', 'filter_grade', 'grade', ' knowledge_grade', 'application_grade', 'ie_grade', ' ia_grade', 'final_grade', 'inactive', 'last_coordinator_access', ' hr_count', 'hr_returned', ' match_hr_count', 'general_grade', 'lat', 'lng' 'tokens', 'iso_certification', 'other_certification', 'researchers', 'thematic_network', ) widgets ={ 'name':forms.TextInput(attrs={ 'class':'form-control', 'placeholder': 'Nome da UBC' }), 'parent_institution_name': forms.TextInput(attrs={ 'class':'form-control', 'placeholder':'Nome da Instituição à qual a UBC é vinculada' }), 'laboratory_departament': forms.TextInput(attrs={ 'class':'form-control', 'placeholder':'Laboratório/Departamento' }), … -
python manage.py migrate Segmentation fault: 11
Specs: OS: macOS High Sierra 10.13.2 Interpeter: Python 3.6.5 When i run the command python manage.py migrate this is the output i get: Segmentation fault: 11 Django: 'python manage.py runserver' gives "Segmentation fault" error There is another post out there with similar questions and suggestions but its never resolved. And i tried suggestions from there but none of it worked out for me. This is part of my settings.py file regarding migration DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'djangoproject', 'USER': 'root', 'PASSWORD': 'test123', 'HOST': '/Applications/MAMP/tmp/mysql/mysql.sock', 'PORT': '3306' } } Any new suggestions? I am stuck. -
how to copy data from heroku postgres DB to django cookie cutter docker postgres DB
I have some data of my application on heroku postgres, I recently xwitched to docker. I am using django cookie cutter for my project. How can I copy all the data from heroku DB to my doccker DB -
ERD Tool that generates Django models?
I want to visually design the database, tables, fields, relationships (conceptual model) in tools like Visual Pardigm, or ERDPlus, and be able to generate Django code for the models, instead of SQL DDL statatements. I know it is possible to generate the schema for any database and then use inspectdb, but want to avoid the additional steps. There are many ways to get the ERD (UML, etc) representation of a Django model in graphic format, but I look for the opposite... -
How to gather relative articles depends on the title in Django Python
I'm building a news website.In news detail page,I want to fetch out(gather) the relative news depend on the news title. I only want to gather all the news that have 3 or 4 same words in the title,and it doesn't matter what words are they. For example,if some of the news title have the 3 same words"python AI future",and these news will be relative news to each other. Any friend could help? Here is my Model: class News(models.Model): title = models.CharField(max_length=100, verbose_name='标题') Here is my newsDetail view: def newsDetailView(request, news_pk): news = get_object_or_404(News, id=news_pk) return render(request, "news_detail.html", { 'news': news, }) -
Value to use for SAML2 AssertionConsumerServiceURL for local intranet application
I'm using SSOCircle IDP to test the integration of a Django application with SAML2 (using django-saml2-auth). The application is not accessible via the public internet, only my local intranet. I'm being routed to the idp.ssocircle.com page just fine, but I'm then seeing an error message: "Unable to do Single Sign On or Federation." Does my SAMLRequest payload look correct? Is it an issue that the URLs I'm passing in the payload are not web accessible? <?xml version='1.0' encoding='UTF-8'?> <ns0:AuthnRequest xmlns:ns0="urn:oasis:names:tc:SAML:2.0:protocol" xmlns:ns1="urn:oasis:names:tc:SAML:2.0:assertion" AssertionConsumerServiceURL="https://172.18.76.140:8280/my_app/user/sso/acs/" Destination="https://idp.ssocircle.com:443/sso/SSORedirect/metaAlias/publicidp" ID="id-vwAPqXFdIM8qwAzyF" IssueInstant="2018-04-26T18:35:57Z" ProtocolBinding="urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST" Version="2.0"> <ns1:Issuer Format="urn:oasis:names:tc:SAML:2.0:nameid-format:entity">https://172.18.76.140:8280/my_app/user/sso/acs/</ns1:Issuer> <ns0:NameIDPolicy AllowCreate="false" Format="urn:oasis:names:tc:SAML:2.0:nameid-format:transient" /> </ns0:AuthnRequest> -
Remote PostgreSQL connection with pgAdmin
I'm trying to set up a remote connection through PostgreSQL running on my server , based on Ubuntu 16.04. So far, when I click on the Save button on pgAdmin, it sort of freezes, does nothing. After typing .../manage.py runserver My_droplet_IP:5432, I try the webpage, and it is accessible. I followed this tutorial after creating my droplet. https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-16-04 Then I edited the settings.py; pg_hba.conf; postgresql.conf files settings.py: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresqlpsycopg2', 'NAME': '.....', 'USER': '....', 'PASSWORD': '....', 'HOST': '127.0.0.1', 'PORT': '5432', STATICROOT = os.path.join(BASE_DIR, 'static/') - at the end of the page And, ofcourse changed the ALLOWED HOSTS = ['....'] with my droplet ip aswell. postgresql.conf listen_address is set to '*' pg_hba.conf file: # Database administrative login by Unix domain socket local all postgres peer # TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all peer # IPv4 local connections: host all all 0.0.0.0/0 md5 # IPv6 local connections: host all all ::1/128 md5 Also allowed firewall, and made an exception to 5432 to be allowed. Any ideas? -
Correct way to pass data from one view to another in django?
Currently Im trying to build an app using Django that pull data from fitbit. Im in the process of getting the Oauth2.0 connection to work. So I generated a authorization url from a bunch of user inputs: fitbit url, fitbit api, fitbit client id, fitbit client secret and redirect url. Once the link is generated and visited, fitbit returns with an access code in the URL like: https://localhost:8000/?code=#= Im able to extract the access code, but Im not sure what is the correct way to obtain the previous user input data in the newly redirected view. One way I can think of is saving the first user inputs into a DB and then pulling it out when redirected with the new access code. But Im not sure what other options are available and what the standard way of doing this is. Im still new to django, so any help is appreciated. Thanks! -
Django + PostgreSQL - 1000 inserts/sec, how to speed up?
I did some diagnosis and found that on htop: python save_to_db.py takes 86% of the CPU postgres: mydb mydb localhost idle in transaction takes 16% of the CPU. My code for save_to_db.py looks something like: import datetime import django import os import sys import json import itertools import cProfile # setting up standalone django environment ... from django.db import transaction from xxx.models import File INPUT_FILE = "xxx" with open("xxx", "r") as f: volume_name = f.read().strip() def todate(seconds): return datetime.datetime.fromtimestamp(seconds) @transaction.atomic def batch_save_files(files, volume_name): for jf in files: metadata = files[jf] f = File(xxx=jf, yyy=todate(metadata[0]), zzz=todate(metadata[1]), vvv=metadata[2], www=volume_name) f.save() with open(INPUT_FILE, "r") as f: dirdump = json.load(f) timestamp = dirdump["curtime"] files = {k : dirdump["files"][k] for k in list(dirdump["files"].keys())[:1000000]} cProfile.run('batch_save_files(files, volume_name)') And the respective cProfile dump(I only kept the ones with large cumtime): ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 881.336 881.336 <string>:1(<module>) 1000000 5.325 0.000 844.553 0.001 base.py:655(save) 1000000 14.574 0.000 834.125 0.001 base.py:732(save_base) 1000000 10.108 0.000 800.494 0.001 base.py:795(_save_table) 1000000 5.265 0.000 720.608 0.001 base.py:847(_do_update) 1000000 4.522 0.000 446.781 0.000 compiler.py:1038(execute_sql) 1000000 23.669 0.000 196.273 0.000 compiler.py:1314(as_sql) 1000000 7.473 0.000 458.064 0.000 compiler.py:1371(execute_sql) 1 0.000 0.000 881.336 881.336 contextlib.py:49(inner) 1000000 7.370 0.000 62.090 0.000 lookups.py:150(process_lhs) 1000000 3.907 … -
Django several apps using same model
I have a model. main_app PersonEntity(models.Model) name = models.CharField(max_length=255) phone = models.CharField(max_length=255) city = models.CharField(max_length=255) # etc I have also several different apps which perform completely different things on this data. As far as I understand django philosophy says that all apps should be independent. But here I clearly need several apps to access(and modify) same data. So I have several options: Put all these apps in one app. Clone this data for every app. Somehow process same model from different apps Which solution is the best? If 3rd, how do I do that? -
Exclude Django Installed Apps while execute custom django command
I have "xxx" in installed apps (settings.INSTALLED_APPS) Inside this applications, I check on existing schema in Postgresql If schema not exists I throw the exception. I request a user to execute Django custom command (for create schema) python manage.py create_schema .... But this command can't execute because of django.setup() Django runs xxx app, which run exception about absent schema I wanna temporarily exclude xxx app from Django while execute my custom command, or maybe You have any suggestion -
django unittest throws exceptions
I have a problem because when I create very simple test in Django I have exceptions like this: import import unittest from django.test import Client code class ModelTest(unittest.TestCase): def setUp(self): self.client = Client() def test_basic(self): response = self.client.get('/login/') self.assertEqual(response.status_code, 200) exceptions FAILED (errors=1) Error Traceback (most recent call last): File "C:\Python27\lib\unittest\case.py", line 329, in run testMethod() File "C:\inetpub\wwwroot\portal\ateris\portal\module\RiskCard\tests\test_models.py", line 11, in test_basic response = self.client.get('/login/') File "C:\Python27\lib\site-packages\django\test\client.py", line 500, in get **extra) File "C:\Python27\lib\site-packages\django\test\client.py", line 303, in get return self.generic('GET', path, secure=secure, **r) File "C:\Python27\lib\site-packages\django\test\client.py", line 358, in generic data = force_bytes(data, settings.DEFAULT_CHARSET) File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 48, in __getattr__ self._setup(name) File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 42, in _setup % (desc, ENVIRONMENT_VARIABLE)) ImproperlyConfigured: Requested setting DEFAULT_CHARSET, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings. I don't know what I am doing wrong... I use unittest documentation. Python ver: 2.7.12 Django ver: 1.8.5 -
Django: TruncYear and annotate/aggregate?
I'm having the following model: class Order(models.Model): placed_at = models.DateTimeField() amount = models.DecimalField() And I want to know the total of amount for each year. When running: Order.objects.annotate(year=TruncYear('placed_at')) .values('year') .annotate(total=Sum('amount')) Django returns a queryset with the year and total for every record in the database (so total == amount). When using aggregate: Order.objects.annotate(year=TruncYear('placed_at')) .values('year') .aggregate(total=Sum('amount')) Django returns the grand total ({'total': Decimal('72822.41')}). The result I'm looking for should be the total broken down per year. Like <QuerySet [{'year': 2016, 'total': Decimal('20000.00')}, {'year': 2017, 'total': Decimal('30000.00')}] Any idea what I'm overlooking here? -
Django CreatView with Foreignkey limit options
In my model I have Person: name Sneaker: owner(FK) = Person.id size colour Booking: customer(FK) = Person.id sneaker(FK) = Sneaker.id time price The logic is after a person login, he can start booking service for one of his sneakers. I have no problem setting the customer to request.user. But somehow in create view for booking, one can see all the sneakers record in the database(include his and other customers'), but I want the customer can only select his own sneaker. Can I put a limit on this? My create view is shown as below. Thx! class Booking_Create_View(CreateView): fields = ['sneaker','time','price'] model = Booking def form_valid(self, form): form.instance.customer = self.request.user return super(Booking_Create_View, self).form_valid(form) success_url = reverse_lazy("booking_system:index") -
Django Templates: Add something below a variable
I have this html django template: {{ header }} {{ content }} {{ footer }} This means the context takes these variables: header, content, footer. Is there something possible like this: Template(...).render( dict(header='foo', content='bar', footer='blu', content__post='this should be between content and footer') The needed result: foo bar this should be between content and footer blu I don't want to modify all templates to contain content__post, since there are a lot of templates. How to "inject" snippets post/pre (below/above) the place of other variables? -
Django Admin won't work after i changed AUTH_USER_MODEL
I am trying to make a small authentication app using django, the admin page won't work any more, the problem happened after i changed the AUTH_USER_MODEL inside settings.py , now if i typed python manage.py createsuperuser it gives me different fields than it was before, more than that, if i tried to access the admin page, it asks for my phone number as login not username. Also inside database i can't see any tables that belongs to auth_user, in fact, i did the migration but its just recognizing Client model but not Admin model. Is there any way to make both of my models work ?? Here are my codes: settings.py AUTH_USER_MODEL = 'authiz.Client' AUTHENTICATION_BACKENDS = ( 'django.contrib.auth.backends.ModelBackend', 'authiz.models.Client', ) # Login settings LOGIN_URL = 'login' LOGOUT_URL = 'index' LOGIN_REDIRECT_URL = 'profile' models.py class Client(AbstractBaseUser, PermissionsMixin): email = models.EmailField(null=True, blank=True, unique=True) phone = models.CharField(max_length=18,unique=True) joined = models.DateTimeField(auto_now_add=True) first_name = models.CharField(max_length=200, null=True, blank=True) last_name = models.CharField(max_length=200, null=True, blank=True) is_staff = models.BooleanField(default=False) password = models.CharField(default='', null=False, max_length=255) USERNAME_FIELD = 'phone' objects = UserManager() def get_username(self): return self.first_name def get_short_name(self): return self.last_name def __str__(self): return self.phone def natural_key(self): return (self.get_username(),) def set_password(self, raw_password): self.password = make_password(raw_password) def check_password(self, raw_password): def setter(raw_password): self.set_password(raw_password) self.save(update_fields=["password"]) … -
Using GROUP_CONCAT with other annotations in Django
I use an annotation which counts upvotes/downvotes while returning a list of articles: queryset = queryset.annotate( upvotes_count=models.Sum( models.Case( models.When(likes__like_state=1, then=1), default=0, output_field=models.IntegerField() ) ) ).annotate( downvotes_count=models.Sum( models.Case( models.When(likes__like_state=-1, then=1), default=0, output_field=models.IntegerField() )) ) But each article also has a few categories as ManyToMany related field and I needed to return those categories comma-separated, so I wrote this function: class GroupConcat(models.Aggregate): function = 'GROUP_CONCAT' template = "%(function)s(%(distinct)s %(expressions)s %(separator)s)" def __init__(self, expression, distinct=False, separator=', ', **extra): super(GroupConcat, self).__init__( expression, distinct='DISTINCT' if distinct else '', separator="SEPARATOR '%s'" % separator, output_field=models.CharField(), **extra ) And added it to my annotation: queryset = queryset.annotate(category=GroupConcat('categories__name')) It works fine but upvotes_count and downvotes_count went crazy and started to multiply(!) results by amount of categories. So the question is: "Is there a way to use GROUP_CONCAT in Django without breaking down SUM annotations?" -
Django Form Boolean with Extra Parameter
I am writing a Django (version 1.11) application that takes attendance. As such I have a boolean input field as the indication of presence, however I also need to tie an image to that boolean field for recognition by the person taking attendance. How can I accomplish this in a Django form? Below is a snippet of my current code. self.fields['member_{}'.format(family[member].id)] = forms.BooleanField( label=family[member].first_name, initial=True, ) When rendering this to a view I would like it to be able to use something like: {% for field in form %} <label for="id_member_{{ member.id }}"> <img src="{{ field.profile_pic }}" /> </label> {{ field }} {% endfor %} How can I accomplish this?