Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to integrate postgresql 10/11 declarative table partitioning (i.e. PARTITION BY clause) in a Django model?
PostgreSQL 10 introduces declarative table partitioning with the PARTITION BY clause, and I would like to use it to a Django model. In principle all what I would need to do is introduce the PARTITION BY clause at the end of the CREATE TABLE statement that the Django ORM creates. CREATE TABLE measurement ( city_id int not null, logdate date not null, peaktemp int, unitsales int ) PARTITION BY RANGE (logdate); Is it possible to insert this clause into the model? I thought that maybe there is a way to somehow append custom SQL to the query that the ORM generates, e.g. using the Meta: class Measurement(models.Model): ... class Meta: append = "PARTITION BY RANGE (logdate)" As far as I am concern the above is not possible. I have also look into the architect library, but it does not use the new PARTITION BY clause. Instead, it uses inheritance and triggers so the code does not suggest any way in which I could append the clause (neither it does for other databases, e.g. MySQL). I have also though of customizing the migrations, by adding an ALTER TABLE... operation, e.g.: operations = [ migrations.RunSQL( "ALTER TABLE measurement PARTITION BY RANGE (logdate)", … -
Heroku Postgres "relation does not exist"
I am migrating my production Django app on Heroku from AWS (RDS) to Heroku Postgres, in an attempt to reduce the web app's network latency. I ran migrations to the Heroku Postgres instance, and everything executed fine. I connected to the Heroku Postgres database via pdAdmin4 and I can see all of the tables and relations just fine, but when I try to run my Django app locally, I cannot get anything to load due to "relation does not exist" error in browser. What's the issue? -
Enforcing user authentication on entire include of URL
So I have this pip app that I installed on my Django environment, and made it available on my urls.py: from django.conf.urls import url,include from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'newapp/', include('newapp.urls')), ] Turns out this app has some sections that one can access without being logged in. How could I enforce a login required decorator on the entire include url? -
How to display additional fields about the M2M field in Django or override select2-results__option
I am looking to implement a feature to allow users to select other users and add them to their many-to-many field. The current state works in what I want it to do, but I want to be able to display additional information about the user they are selecting, and not just their username. Things like first_name, last_name, profile information, etc. - Ideally it would all be in the one line they see when adding the user to their m2m queue, such as: John Smith | j.smith | j.smith@example.com as opposed to: j.smith I see the element select2-results__option and I'm curious if I can just override it with the additional information. If there is a clean way to do it through django, I'd prefer to do it that way, but open to anything really. Views.py class UserAutocomplete(autocomplete.Select2QuerySetView): def get_queryset(self): if not self.request.user.is_authenticated: return User.objects.none() qs = User.objects.all() if self.q: qs = qs.filter(Q(username__icontains=self.q) | Q(first_name__icontains=self.q) | Q(last_name__icontains=self.q)) return qs forms.py class UserSurveyQueueForm(forms.ModelForm): def __init__(self, *args, **kwargs): user = kwargs.pop('user') super().__init__(*args, **kwargs) self.fields['queue'].queryset = User.objects.all().exclude(pk=user) self.fields['queue'].required = False class Meta: model = UserSurveyQueue fields = ('queue',) widgets = { 'queue': autocomplete.ModelSelect2Multiple( url='user-autocomplete', ), } -
SyntaxError: Generator expression must be parenthesized / python manage.py runserver
When I want to run the server at the first time on cmd it appear this error: "SyntaxError: Generator expression must be parenthesized" -
Dynamically generating Django form based on CharField choice
I have a user profile model that is storing configurations for a number of third party API keys, and I'm trying to determine how to best go about dynamically generating forms based on the choice that the user makes. The app supports only a subset of services, so I'm using a CharField (+ CHOICES) to narrow down what the user is trying to submit a configuration for. The user can submit as many duplicates as they would like (3 sets of Cloud Service 1 keys, for example) I have this model: class ServiceIntegration(models.Model): profile = models.ForeignKey( Profile, on_delete=models.CASCADE ) CS1 = 'CS1' CS2 = 'CS2' SERVICE_CHOICES = ( (CS1, 'Cloud Service 1'), (CS2, 'Cloud Service 2'), ) alias = models.CharField(max_length=255) service = models.CharField( max_length=255, choices=SERVICE_CHOICES, default=CS1 ) config = JSONField() In a form, the user has a dropdown whose QuerySet is set to this model's objects. When the user makes a selection, I'd like to reach out to an endpoint and dump some form HTML in a predetermined location. Presumably, I could have a form set up for each integration, and simply have a view that takes in the choice, looks up the form, and renders it back out (same … -
Prepopulating fields by ID
using datatables (https://datatables.net/) and django, what is the best way to set an initial value (e.g. a models field) to the search box? This box, as far as I understand is added via JS/jQuery. -
connect unity and django
I am working on a project where someone has developed a game on unity platform and wants to send real time data which should be stored in a database created in my project using django (deployed on heroku ). I just want that if there exists an API or something like that to which we can POST and GET data simultaneously without any redundancy preferably json data from two different platforms respectively. I have looked for myjson.com which support cross-origin resource sharing (CORS) and SSL. But I am not sure if this is perfect for me. -
I have cloned a Django project from gitHub and I get this error when I run : **python3.6 manage.py runserver**
Unhandled exception in thread started by .wrapper at 0x1057916a8> Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper fn(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run autoreload.raise_last_exception() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 248, in raise_last_exception raise _exception[1] File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/core/management/init.py", line 337, in execute autoreload.check_errors(django.setup)() File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper fn(*args, **kwargs) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/init.py", line 24, in setup apps.populate(settings.INSTALLED_APPS) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/registry.py", line 89, in populate app_config = AppConfig.create(entry) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/django/apps/config.py", line 90, in create module = import_module(entry) File"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/ importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "", line 994, in _gcd_import File "", line 971, in _find_and_load File "", line 953, in _find_and_load_unlocked ModuleNotFoundError: No module named 'rest_framework' -
How to get the final source code that will be executed in django class based views?
In django class based generic views, they have defined few functions by default. We can override some or all of them as required. As each new cbv that we write, we will override diff functions based on the requirements. As cbv are object oriented, understanding which piece of code will be executed is a real pain to find out as the actual source of the cbv is scattered across diff files. It would become a lot easier to understand if we could just see the final source code of the cbv that we have written. Maybe just give a call to a function and see the full source code? Just like getting sql that will be executed by a django orm query. Does something like this exists? If not, how to write this damn function? I could figure out which functions are defined on which class and the mro would also be useful and required. How to get the source of the functions? Is there a better way to do this? Thanks. -
Following a tutorial: cannot run Docker container of Django
I am following this tutorial to create Django API and run it on docker. The API runs well without docker, however I have issues with running it on docker. The tutorial says that Dockerfile should be created: # Dockerfile # FROM directive instructing base image to build upon FROM python:2-onbuild # COPY startup script into known file location in container COPY start.sh /start.sh # EXPOSE port 8000 to allow communication to/from server EXPOSE 8000 # CMD specifcies the command to execute to start the server running. CMD ["/start.sh"] # done! Then a docker container should be built: docker build -t davidsale/dockerizing-python-django-app . Until now everything is ok. But the last command fails: docker run -it -p 8000:8000 davidsale/djangoapp1 Error: Starting Gunicorn. [2018-11-01 19:24:16 +0000] [1] [INFO] Starting gunicorn 19.6.0 [2018-11-01 19:24:16 +0000] [1] [INFO] Listening at: http://0.0.0.0:8000 (1) [2018-11-01 19:24:16 +0000] [1] [INFO] Using worker: sync [2018-11-01 19:24:16 +0000] [7] [INFO] Booting worker with pid: 7 [2018-11-01 19:24:16 +0000] [7] [ERROR] Exception in worker process Traceback (most recent call last): File "/usr/local/lib/python3.6/site-packages/gunicorn/arbiter.py", line 557, in spawn_worker worker.init_process() File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 126, in init_process self.load_wsgi() File "/usr/local/lib/python3.6/site-packages/gunicorn/workers/base.py", line 136, in load_wsgi self.wsgi = self.app.wsgi() File "/usr/local/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi self.callable … -
Shopify graphene query
I can't see how to format this graphene query for shopify. I need to replicate this curl query with graphene in Django: curl -X POST \ "https://<shop>.myshopify.com/api/graphql" \ -H "Content-Type: application/graphql" \ -H "X-Shopify-Storefront-Access-Token: <storefront-access-token>" \ -d ' { shop { collections(first: 5) { edges { node { id handle } } pageInfo { hasNextPage } } } } ' So far I have: access_token = 'some_token' headers = ( { "Content-Type": "application/graphql" }, { "X-Shopify-Storefront-Access-Token": access_token}, ) schema = graphene.Schema(query=Query) print(schema) result = schema.execute('{ catsinuniform { collections(first: 5) { edges { node { id handle } } pageInfo { hasNextPage } } }'') print(result.data['catsinuniform']) This syntax is wrong for graphene but I don't get how it should look? Once I have the data in the right format I can then do a requests post to get the informaiton I want from the shopify storefrontapi -
Why I can get user attribute from this request?
Currently, I am working on a project using Django with OAuth for logging in function. The client is AngularJS. It includes 2 steps: client posts form to server, the keys including client_id, client_secret, grant_type, password, username. If success, receives response including access_token, refresh_token, token_type, expires_in, scope. client GET a url without any query params. The server using the code as below to process: def get(self, request): response = {} user = request.user //why it can get user? print dir(request) if user.id: response['user'] = UserSerializer(user).data else: raise AuthenticationFailed({'message': "Authentication error"}) return Response(response) if sucessful, client receives account info including first_name, photo, is_active and so on. My main question the GET request in step2, this request without any query params, and I checked this GET in wireshark, its body and header do not have a key named user, as below: Frame 68: 569 bytes on wire (4552 bits), 569 bytes captured (4552 bits) on interface 0 Ethernet II, Src: Apple_a5:b8:d0 (6c:40:08:a5:b8:d0), Dst: D-LinkIn_92:3d:72 (e4:6f:13:92:3d:72) Internet Protocol Version 4, Src: 10.0.0.102, Dst: 184.69.72.19 Transmission Control Protocol, Src Port: 62335, Dst Port: 8080, Seq: 1, Ack: 1, Len: 503 Hypertext Transfer Protocol GET /media/profiles/agents/_single-house-active_in.png HTTP/1.1\r\n [Expert Info (Chat/Sequence): GET /media/profiles/agents/_single-house-active_in.png HTTP/1.1\r\n] [GET /media/profiles/agents/_single-house-active_in.png HTTP/1.1\r\n] … -
Django Firebase subscirbe/unsubscribe to topics painfully slow
I'm using a Django server (localhost) to subscribe/unsubscribe users to Firebase topics. The setup is very simple, inside here: @csrf_exempt def req_api(request): if request.method == 'POST': I run this function: def subscribe_to_topics(user): for topic in user.topics messaging.subscribe_to_topic(user.token, '/topics/' + topic) with the extracted data from the HTTP POST request. It hangs on this line messaging.subscribe_to_topic() for a second or so for each topic. Same with unsubscribe. -
How to return to a filtered page using a Class Based UpdateView in Django
I am trying to create a Django admin like interface where a user can directly enter data through a model form but not through the admin interface. The Django admin interface returns to a list page or a filtered list page (based on where from admin started editing). While I can redirect the success url to the "all-queryset" list page, I could not figure out how to redirect the success url to a "filtered" list page. Following the answer here: Returning to page that brought you there Django this is my code so far: class MyModelUpdateView(LoginRequiredMixin, SuccessMessageMixin, UpdateView): template_name = 'catalog/mymodel_change.html' form_class = MyModelForm queryset = MyModel.objects.all() filter_class = MyModelListFilter def get_context_data(self, **kwargs): context = super(MyModelUpdateView,self).get_context_data(**kwargs) context['return_url'] = self.request.GET.get('return') return context def get_success_message(self, cleaned_data): return self.success_message % dict( cleaned_data, display_name=self.object.display_name, id=self.object.pk) def get_success_url(self): return_url = self.request.GET.get('return') if return_url: return return_url return 'home/mymodellist/' My form html <form method="POST"> {% csrf_token %} {{ form.as_p }} <input type="hidden" name="return" value="{{ request.path }}"> <button type="submit">Save</button> </form> I have also tried request.path_info. The success url redirects to the list view page , e.g., home/mymodellist/, but I want to return home/mymodel/?fliterlist='search-term' if the user start editing from a filtered list page. I have tried HttpRequest.META as … -
Django- filtering results where one or more reverse foreign keys meet criteria
Lets say I have the following (simplified) models: class Job(models.Model): name = models.CharField(max_length=255) class JobDateStatus(models.Model) job = models.ForeignKey(Model, on_delete=models.CASCADE) date = models.DateField() status = models.CharField(max_length=6) Is there a way to select all Job objects where any (one or more) of its JobDateStatus children have a particular status? e.g.: active_jobs = Job.objects.filter(<ONE OR MORE OF jobdatestatus_set HAS status='active'>) -
How to write Django Unit Test for Authentication Protected REST APIs?
I have looked at two answers on SO that may already have my answer but frankly I just don't understand them. These SO post are: Django Rest Framework API Authentication Test and Django authenticate unit test. Both of the SO questions use different approaches. The approach I am attempting to use is this. I have created this class: from rest_framework.test import APIClient from django.test import testcases from django.contrib.auth.models import User class RemoteAuthenticatedTest(testcases.TestCase): client_class = APIClient def setUp(self): self.username = 'mister_neutron' self.user = User.objects.create_user(username='mister_neutron', email='mister_neutron@example.com', password='F4kePaSs0d') super(RemoteAuthenticatedTest, self).setUp() My unit test looks like this: class InfoViewTestCase(RemoteAuthenticatedTest): def create_info_record(self): from random import randint blade = 'test-blade-host-name-%s' % (randint(0, 100)) breachs = randint(0,100) dimm = 'test dimm slot %s' % (randint(0,100)) url = reverse('info:info_creat') data = { 'blade_hostname': blade, 'breach_count': breachs, 'dimm_slot': dimm, } response = self.client.post(url, data, format='json', REMOTE_USER=self.username) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(Info.objects.count(), 1) self.assertEqual(Info.objects.get().blade_hostname, blade) I have this in my settings.py file: #Authentications REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': ( 'rest_framework.authentication.BasicAuthentication', 'rest_framework.authentication.SessionAuthentication', 'rest_framework.authentication.TokenAuthentication', ), 'DEFAULT_PERMISSION_CLASSES': ( 'rest_framework.permissions.IsAuthenticated', ), } When I run my test I get this result: Failure Traceback (most recent call last): File "/myproject/info/tests.py", line 686, in create_info_record self.assertEqual(response.status_code, status.HTTP_201_CREATED) AssertionError: 401 != 201 What am I doing wrong. -
Handle(read) image and text data from a POST request in a Django app?
I have a requirement where in the user enters both image and text data and sends it via POST request. Now, on a Django app, how do i handle both of them. I've first tried to access image file as described in this link after that when I call request.body, it gives below error: lib\site-packages\django\http\request.py", line 255, in body raise RawPostDataException("You cannot access body after reading from request's data stream") django.http.request.RawPostDataException: You cannot access body after reading from request's data stream -
Django Annotate - get list of distinct field values
I have 3 models - Book, Page and Language. class Book(models.Model): title = models.CharField(max_length=100) author = models.CharField(max_length=100) class Page(models.Model): image = models.ImageField(upload_to='Uploaded_images') language = models.ForiegnKey(Language, models.CASCADE) book = models.ForeignKey(Book, models.CASCADE) class Language(models.Model): name = models.CharField(max_length=20) There's a one-to-many relationship between Book and Page objects. Some pages in a book can be of a different language, so I added the ForeignKey in Page model and not in the Book model. I want to get the list of all books along with the languages its pages were written in. I tried this: books = Page.objects.annotate( name=F('book__title') ).values( 'name', 'book_id' ).distinct( ).annotate( page_count=Count('id'), ).values( 'name', 'book_id', 'language' ) If there are 2 languages in the book, I get 2 entries for the same book title. I just need 1 entry for the book and be able to show its languages as a list. Ex: If there's a book with pages in either of the 2 languages, Spanish and English, I'd like to extract the book title and the languages in it as ['English', 'Spanish']. Is this doable? -
Giving Docker access to db file outside container
I'm trying to test a Django app managed by Docker. Since it's a development project only used by me, I'm using a sqlite3 database backend. However, because I'll be populating this test database with a lot of generated data, and because I don't fully trust Docker, I want to store this sqlite3 db file outside of the container in my home directory, to ensure it doesn't get deleted or lost. However, by design, Docker makes it difficult for programs inside containers to access files outside of those containers. How do I update my Docker configuration to allow access to this one specific db file in my home directory? -
whenever i create a venv and then pip freeze still all the global packages are shown
I am new to Django and was following a tutorial where a venv was setup for the project directory but after doing pip freeze only pip was there but in my all the global packages are still shown. -
Unable to bring up docker project
I'm following this Docker tutorial, which creates a simple Docker-managed Django site, and when I try to run docker-compose up to launch my docker project, I get the ambiguous error: ERROR: Couldn't connect to Docker daemon at http+docker://localunixsocket - is it running? The error suggests that the Docker daemon isn't running, but service docker status shows the Docker daemon is running. If instead I run sudo docker-compose up, then it succeeds, but it chowns a lot of my local development files to the root user, which is easy enough to fix, but annoying. Why does Docker require root access just to start a local Django development server? How do I fix this? My versions: Docker version 18.06.1-ce, build e68fc7a docker-compose version 1.11.1, build 7c5d5e4 Ubuntu 16.04.5 LTS -
Django: I want to save the dropdown selected display value into databse instead of the name of selected option
In the forms.py, I have created a application_status widget and added it to the customerForm. it is as below: APPLICATION_STATUS = [ ('new', 'New'), ('processing', 'Processing'), ('reject', 'Rejected'), ('complete', 'Completed'), ] class CustomerForm(forms.ModelForm): application_status = forms.CharField(label='What is your application status?', widget=forms.Select(choices=APPLICATION_STATUS)) # email_one = forms.EmailField() class Meta: model = Customer fields = ('application_status') And then in the view.py I save selected value in the database, but it saved the name of selected option not display value. For example,I select the option 'Processing' and I want the value 'Processing' to be saved in database. But I see 'processing' is saved. The code is as below: def customer_new(request): if request.method == "POST": form = CustomerForm(request.POST) if form.is_valid(): customer = form.save(commit=False) customer.status = request.POST.get('application_status') customer.save() return redirect('customer_detail', pk=customer.pk) else: form = CustomerForm() return render(request, 'customer_edit.html', {'form': form}) Please help a little bit. -
How to return email verified along with user endpoint?
I have implemented DRF and don't want to force email validation, but still use it(remind them on screen). I would like to just return account_emailaddress.verified along with the user endpoint. What is the best way to achieve this? I tried taking hints from this post, but didn't have success. Serializer: class UserSerializer(serializers.ModelSerializer): class Meta: model = models.CustomUser fields = ('id', 'first_name', 'last_name', 'email', 'phone_number', 'avatar') View: class UserView(generics.RetrieveUpdateDestroyAPIView): queryset = CustomUser.objects.all() serializer_class = UserSerializer def get(self, request, *args, **kwargs): try: user = self.queryset.get(pk=kwargs["user"]) return Response(UserSerializer(user).data) except CustomUser.DoesNotExist: return Response( data={ "message": "User with id: {} does not exist".format(kwargs["user"]) }, status=status.HTTP_404_NOT_FOUND ) def put(self, request, *args, **kwargs): try: user = self.queryset.get(pk=kwargs["user"]) serializer = UserSerializer() user = serializer.update(user, request.data) return Response(UserSerializer(user).data) except CustomUser.DoesNotExist: return Response( data={ "message": "User with id: {} does not exist".format(kwargs["user"]) }, status=status.HTTP_404_NOT_FOUND ) -
How to prevent users from seeing data that does not belong to them in Django DetailView?
I have a web app where a user signs in and begins entering items for a ToDoList. The base.html is wrapped in an is_authenticated check so users cannot see anything in the app until they have logged in. I was doing some testing with: Logging in as User2 Adding a new ToDoListItem Redirecting to the DetailView In this case, the URL = http://localhost:8000/to_do_list/to_do_item/72 At this point I realized that the DetailView would allow User2 to see the details for any ToDoListItem for User1 just by entering in an existing pk into: http://localhost:8000/to_do_list/to_do_item/<int:pk>. urls.py includes path('to_do_item/<int:pk>', views.ToDoListItemDetail.as_view(), name='todo-item-detail'), views.py class ToDoListItemDetail(DetailView): model = ToDoListItem todolistitem_detail.html {% extends 'base.html' %} {% block content %} <a href="/">Home</a> <h1>DetailView for 'ToDoListItem' model</h1> <p>TaskTitle: '{{ object.title }}'</p> <p>Complete: '{{ object.is_complete }}'</p> <p>User: '{{ object.user}}'</p> {% endblock %} What is the recommended way to prevent this from happening? I am considering the following: I could completely remove the DetailView and direct to a different URL that only returns the user's data (using something like ToDoListItem.objects.filter(user=request.user)) I could check that the name of the user logged in matches the name of the user that owns the ToDoListItem. I could override get_context_data() for the DetailView and check user …