Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Keep queryset annotations after combining tow querysets
I'm currently struggling with the behavior of django querysets with annotations and the included operators | and & I have Models like this: class Property(models.Model): value = models.IntegerField(null=True) obj = PropertyManager() class Profile(models.Model): name = models.CharField() values = models.ManyToManyField(Property) class PropertyManager(models.Manager): def included(self, profile): super(PropertyManager, self).filter(Q(property__profile=profile)).annotate(included_value=Value('true', output_field=CharField())) def excluded(self, profile): super(PropertyManager, self).filter(~Q(property__profile=profile)).annotate(included_value=Value('false', output_field=CharField())) def included_excluded(self, profile): return (self.excluded(profile) | self.included(profile)).distinct() I naively expected that the included_excluded function returns a joined queryset of both querysets with their annotations, like: property_id | included_value ------------|--------------- 1 | true 2 | false but it turns out, that the annotation is overwritten in my examples: j = Profile.objects.get(id=1) exc = Property.obj.excluded(profile=j) inc = Property.obj.included(profile=j) all = Property.obj.included_excluded(profile=j) len(inc) => 14 len(exc) => 17 len(all) => 31 all.values_list("included_value") => <QuerySet [('false',)]> exc.values_list("included_value") => <QuerySet [('false',)]> inc.values_list("included_value") => <QuerySet [('true',)]> all has obviously not all the correct values in the annotations, as I expected. So I'm wondering, if there is a method to join two querysets and keep the annotations I made earlier -
How to handle unauthorized Django Rest API calls
I have a difficulty to understand how to connect my Angular front-end with the Django Rest API backend. In the front-end I like to get read-only data from the API nonetheless the user is logged in or not. Both front-end and back-end run on the same server and Django Rest has: ALLOWED_HOSTS = ['127.0.0.1'] Do I have to authenticate the front-end app to the API? If so how do I keep my credentials secure? Or do I have to mark certain endpoints as unauthorized read only? -
Extending Django with sites app to provide current working app as SAAS
I have a working deployed Django application in production, with some users and various objects stored in the database. The client told me if the website could be replicated changing some minor things, such as templates, logos, etc. but maintaining some of the users and objects. I had heard of django.contrib.sites long ago, so after reading the description it seemed the perfect choice. I went straight hands on, adding django.contrib.sites to the INSTALLED_APPS, putting the SITE_ID in my settings.py, and performing the migrations. I chose to use a ManyToManyField for the sites, since the users might be able to login in one or more of the sites: sites = models.ManyToManyField(Site) Since I have modified managers for some models, like the User, I had to override my custom get_queryset, to only return the current site users, so I added the filter(sites=settings.SITE_ID) to the returned queryset, as I saw in the documentation: from django.contrib.auth.models import BaseUserManager class UserManager(BaseUserManager): def get_queryset(self): return super(UserManager, self).get_queryset().filter( is_active=True).filter(sites=settings.SITE_ID) For other classes, since I didn't override the default manager, I just did override the objects: objects = CurrentSiteManager() Everything was working fine until I tried to test my current working app. Since the objects in the database … -
How to have 3 type of users in Django?
I'm developing a Django powered website in which i have three types of users: Teachers, Students and Companies. Teachers can add training stage offers. Students can apply for a training stage offer. Companies can also add training stage offers. My main question is how to have three types of users in Django, so that I can have three types of login? My current solution is that I have three models of each type of user (Model Student, model Teacher, model Company). I also have three types of different forms to allow companies, teachers or students to register. When a form is correctly compiled in my view I create manually(by coding), a user and I manually create a object for the relative model then save that object. The main problem is that the user I create is not differentiated by there type. class Azienda(models.Model): nome=models.CharField(max_length=200) sede=models.CharField(max_length=200) pIva=models.CharField(max_length=30) email = models.EmailField() user = models.OneToOneField(User) #company model, in models.py #company form, in forms.py class AziendaForm(forms.Form): nome = forms.CharField(max_length=200) sede = forms.CharField(max_length=200) pIva = forms.CharField(max_length=30) email = forms.EmailField() user = forms.CharField(max_length=100) password = forms.CharField(widget=forms.PasswordInput) #view of company registration form def registratiAziende(request): if request.method=='POST': form=AziendaForm(request.POST) if form.is_valid(): form['nome'].value() azienda=Azienda() azienda.nome=form['nome'].value() azienda.sede = form['sede'].value() azienda.pIva = … -
How to run django runserver in background with circleci?
I am using circleci with my django project. I want to run python manage.py runserver in the background for some specific selenium tests. My config.yml is somewhat like version: 2 jobs: build: docker: - image: circleci/python:3.6.1-browsers - image: selenium/standalone-chrome working_directory: ~/myproject steps: - checkout - run: name: install dependencies command: | python3 -m venv venv . venv/bin/activate pip install -r requirements.txt - run: name: run unit tests command: | . venv/bin/activate python manage.py test - run: name: run selenium tests command: | . venv/bin/activate python manage.py migrate python manage.py runserver 8000 python manage.py run_selenium_tests I could make it work by running selenium tests inside django LiveServerTestCase. But I want to run selenium tests independently, for that I need runserver to be running in the background. Right now circleci stops execution at python manage.py runserver and eventually timeouts. Any idea to do this? -
Django Bootstrap front end preview button effects don't work
I use Python Django to implement the webserver and backend and use bootstrap to build the front end. My website is about a research group. In bootstrap, there is a custom button allowing to click to show and hide another element, whose source code is as shown: <a class="btn btn-primary" role="button" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria- controls="collapseExample"> Link with href </a> <button class="btn btn-primary" type="button" data-toggle="collapse" data-target="#collapseExample" aria-expanded="false" aria- controls="collapseExample"> Button with data-target </button> <div class="collapse" id="collapseExample"> <div class="well"> ... </div> </div> I use this button to show the abstract of the publications, whose data is collected from models.py: class Publication(models.Model): title = models.CharField(max_length=60, default='Title') first_author = models.CharField(max_length=60, default='Authors') cooperative_author = models.CharField(max_length=120, default='Authors') abstract = models.TextField(null=True) published_time = models.DateTimeField(auto_now=0) tag = models.CharField(max_length=60, default='Tag') pub_url = models.CharField(max_length=120, default='pub_url') and the code in my view.py is: def research_accomplishment(request): publications = models.Publication.objects.all() return render(request, 'research_spacecoupe/research_accomplishment.html', {'publications': publications} ) The code in the HTML is: {% for publication in publications %} <h2 class="blog-post-title">{{ publication.title }}</h2> <p class="blog-post-meta">{{ publication.published_time }} by <a href="#">{{ publication.first_author }}, {{ publication.cooperative_author }}</a></p> <a class="btn btn-primary" role="button" href="{{ publication.pub_url }}" aria-expanded="false" aria- controls="collapseExample" target="_blank"> Download </a> <button class="btn btn-primary" type="button" data- toggle="collapse" data-target="#{{ publication.tag }}" aria- expanded="false" aria-controls="#{{ publication.tag }}"> Abstract … -
Save user progress in DB with multiple Dates.
I want to make a table in my DB to store user score (Like SO does). I found this post How do I record daily rankings for a model in Django? and even though its exactly what I need, I cant get the accepted solution to work. I have problems to create a proper model. class Rank(models.Model): class Meta(object): unique_together = (("date", "user")) date = models.DateField(db_index=True, default=datetime.now()) user = models.OneToOneField(User) value = models.IntegerField(default=1) def value1(self): try: x = self.user.userprofile.get_point() #reference a function in another model except: x = 1 return x But this gives me a structure like this: While I need something like User/date: Date1| Date2 | Date3 ..... user1: 15 | 16 | 17..... user2: 2 | 6 | 12..... Where the numbers represent the points and Date1 represents 01.01.2017 and so on. Im using PostgreSQL. Please don't answer with "just use a ManyToManyField" or similar. I want to understand the subject and some details are appreciated. -
ValueError at / invalid literal for int() with base 10: 'Author'
I am trying to allow authors to chose the author of a blog post and although everything migrated properly (I think) and python will successfully run the server, when I go to the site I get this error: ValueError at / invalid literal for int() with base 10: 'Author' I have no idea whats going on, please help. Here is my models.py from django.db import models from django.contrib.auth.models import User from datetime import date # Create your models here. class UserProfileInfo(models.Model): user = models.OneToOneField(User) portfolio_site = models.URLField(blank=True) profile_pic = models.ImageField(upload_to='profile_pics',blank='True') def __str__(self): return self.user.username class UserPosts(models.Model): post_title = models.CharField(max_length=100,unique=True) post_sub_title = models.CharField(max_length=250,unique=False) post_author = models.ForeignKey('auth.User') post_date = models.DateField(default=date.today,blank=True) post_body = models.TextField(max_length=1000,unique=False) post_featured_pic = models.ImageField(upload_to='featured_pics',blank='True') def __str__(self): return str(self.post_title) -
Django 1.11.3 unable to create table in postgresql
i am trying to create table in postgresql by django(version 1.11.3). i entered to following information in settings.py DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'. 'NAME': 'postgres', # Or path to database file if using sqlite3. 'USER': 'postgres', # Not used with sqlite3. 'PASSWORD': 'password', # Not used with sqlite3. 'HOST': 'localhost', # Set to empty string for localhost. Not used with sqlite3. 'PORT': '5432', # Set to empty string for default. Not used with sqlite3. } } but it is always creating SqLite database. Can anyone answer me please?? -
How to say to foreign key model to use the same database as main model uses?
I ran into a case when model and fireign key model are both referencing to one DB but DB isn't default. F.e. class Ref(models.Model): id = models.IntegerField(primary_key=True) name = models.CharField() class Main(class.Model): id = models.IntegerField() ref_id = models.ForeignKey(Ref) ........... qs = models.Main.object.using('custom_db').all() Django attemps to get foreign key from default database and expectially catches an exception 'table or view doesnt exist' Is there a simple way to say to foreign key model use the same database as Main ? Thanks -
Translated group names
Django 1.11.4 I'm trying to organize groups. And I'd like groups to have translated names. I create groups by a django-admin command: initialize_project.py from django.utils.translation import ugettext_lazy as _ class Command(BaseCommand): def _create_groups(self): observers = Group.objects.create(name=_("observers")) def handle(self, *args, **options): try: groups = self._create_groups() except IntegrityError: ..... Well, the translation doesn't work. I had a look at Group class: class Group(models.Model): name = models.CharField(_('name'), max_length=80, unique=True) I suppose, it is impossible to make translated group names. Well, this is just a string database. It is not part of Django code. Could you tell me whether it is possible to organize translated group names (without breaking Django apart)? -
How to execute arithmetic operations between Model fields in django
Prologue: This is a question arising often in SO: Subtracting two annotated columns Django query with simple arithmetic among model fields and comparison with field from another model Django Aggregation: Summation of Multiplication of two fields And can also be applied here: Django F expression on datetime objects I have composed an example on SO Documentation but since the Documentation will get shutdown on August 8 2017, I will follow the suggestion of this widely upvoted and discussed meta answer and transform my example to a self-answered post. Of course I would be more than happy to see any different approach as well!! Question: Assume the following model: class MyModel(models.Model): number_1 = models.IntegerField() number_2 = models.IntegerField() date_1 = models.DateTimeField() date_2 = models.DateTimeField() How can I execute arithmetic operations between fields of this model? For example, how can I find: The product of number_1 and number_2 of a MyModel object? How to filter items where date_2 is 10 or more days older than date_1? -
Pycharm is not showing the project view or editor of a project
I'm having a bit of trouble with Pycharm. I tried opening a project with it, one that I know has files and folders in it, but for some reason, the project view and the editor is not visible at all. I checked one of the modules, and there is code in it, so I don't know why I can't get these things to show properly. -
Django SimpleListFilter max record
two models: project(models.Model): name = models.CharField... yearreport((models.Model) year = [2014,2015,2016] #example project = models.ForeignKey(project) """ examples: Project A has reports for 2014 Project B has reports for 2014,2015 Project C has reports for 2014,2015,2016 """ I now want to build a SimpleListFilter for the Django Admin, which filters for the highest/most recent report a project has. For example if I filter for 2015 only project B should show. (With a normal filter, this request would also show C, I don't want that) I tried some funky things... queryset.filter(Max('yearreport__year')==self.value())) but I just lost in not knowing the syntax.. Pleas end my guessing game :) Thanks -
Django account log in after restoring DB backup
I have a Django (1.8.17) with PostgreSQL (9.2.14). On my local machine there are some data and some user accounts (admin among them) I would like to identically reproduces all my local data in my dev server. I make DB backups like so: sudo -u postgres pg_dump --column-inserts --data-only strateole2_local > dev_backup.sql Then I restore data back on the dev server like so: sudo -i -u postgres psql mysitedb_dev < dev_backup.sql Everything is fine except log in, I can't login with the same passwords as on local machines. I think it is a problem of password hashes difference. But I can't spot it exactly. Is there any suggestion ? Am I forgetting something ? Is there any solution ? Note: when I reproduce the website on other local machines (with the same settings file) everything is alright. -
Django: Accessing list attributes in template
I have a SQL query in a django view and store the results in a variable. As far I know the result should be stored as a list. def assignments(request): cursor = connection.cursor() cursor.execute("SELECT o.article_id, o.amount, m.id, o.create_date, m.status FROM orders o, producers p, machines ma, matches m WHERE ma.producer_id=1 AND m.machine_id = ma.id AND m.order_id = o.id") articles = cursor.fetchall() context = {"article_list": articles} return render(request, 'assignments.html', context) Then I want to transfer that data row for row in a table in my template. {% block body %} <div class="container"> <table class="table"> <thead>...</thead> <tbody> {% for articles in article_list %} <tr> <td>{{ articles.article_id }}</td> <td>{{ articles.amount }}</td> <td>{{ articles.id}}</td> <td>{{ articles.create_date }}</td> <td>{{ articles.status }}</td> </tr> {% endfor %} </tbody> </table> </div> {% endblock %} Unfortunately the table is empty and is not showing any results. The query itself should be fine. I tested the query in my database workbench and it is yielding the correct results. How can I access the data stored in variable "articles" from my template? PS: I'm far from being a programmer. So I don't really know any programming concepts/language. Any help is highly appreciated. Thanks! -
How to set visible all methods for non-authenticated users in Django build-in documentation?
I have set built-in documentation in Django. When I open my app there aren't visible any methods. When I authenticate as a user or admin they become visible. Only then when I set none I still see them but I can't do all actions. Is it possible to set them to be seen by default without authentication? I mean when user open my documentation first time without authentication he will be able to see all the methods. Not need to log in and set none. Obviously user without authentication will not be able to do all operations but he will be able to see all methods. If something is not clear just let me know and I will try to explain it. -
Can't edit but can add new inline in Django admin
Here are my models class Note(): note = models.TextField(null=False, blank=False, editable=True) user = models.ForeignKey(to=User, null=True, blank=True) content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE) object_id = models.PositiveIntegerField() content_object = GenericForeignKey("content_type", "object_id") And an inline I created this model to incorporate in any admin is below class NoteInline(GenericTabularInline): model = Note extra = 0 What I need here is, I want to see all the current notes but don't want the logged in user to edit them. At the moment user can edit old and add new. So here is what I did, class NoteInline(GenericTabularInline): model = Note extra = 0 def get_readonly_fields(self, request, obj=None): if obj and 'change' in request.resolver_match.url_name: return ['note', 'user', ] else: return [] But now if user adds new note he sees a disabled (not editable) note text ares. However user can see old fields not editable. How to implement this functionality? -
Framework for REST API design for large data set
I am a beginner and I haven't worked a lot with APIs and backend stuff. I have a large set of data in JSON with about 100,000 records (say list of Users). I want to implement RESTful API through which one can search for a particular record using one of the keys (say UserName). The JSON file is nested to 2 levels. What would be the best way for implementing the above data set. Since the record is big I don't want any issues with the search time. I can have the following frameworks as I am proficient in JS and Python - Use NodeJS and MongoDB (MongoDB has map/reduce for quick search I suppose) Use Django with Rest Framework and Relational database. Which one should I use for this project? -
__init__() got multiple values for keyword argument 'content_type' whle trying to render image dynamically
I am trying to render Image dynamically ( Live video streaming). but i am seeing error __init__() got multiple values for keyword argument 'content_type' My Url.py url(r'^live$',views.video_feed,name='live'), View.py def gen(webcam): while True: frame = webcam.get_frame() yield (b'--frame\r\n' b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n') def video_feed(request): return HttpResponse(request,"clients/live.html",{'live_feed':gen(webcam())},content_type='multipart/x-mixed-replace; boundary=frame') cam.py ( It fetches frames and return frames to generator function) class webcam(object): def __init__(self): self.file_path = os.path.join(BASE_DIR, './clients/static/clients/live/') self.frames = open(self.file_path + 'live' + '.jpg', 'rb').read() def get_frame(self): return self.frames please advise what am i missing -
DjangoUnicodeDecodeError while running makemigrations
I'll post my models below, but please note that I'm using the 'u' prefix everywhere, and I've added the magic encoding line as the second line of my file. I'm using django 1.9 and python 2.7 MODEL #!/usr/bin/env python # -*- coding: utf-8 -*- from django.db import models from django.utils.translation import ugettext_lazy as _ class Organisaatio(models.Model): organisaation_nimi = models.CharField( verbose_name=_(u"Organisaation nimi"), max_length=128, null=True, blank=True, help_text=_(u"Organisaatiosi nimi"), ) organisaation_koko = models.IntegerField( verbose_name=_(u"Organisaation Koko"), null=True, blank=True, help_text=_(u"Arvio organisaatiosi jäsenmaräästä"), ) kuvaus = models.TextField( verbose_name=_(u"Tehtävän kuvaus"), blank=True, default="", help_text=_(u"Kuvaile tehtävää"), ) kesto = models.DecimalField( verbose_name=_(u"Tehtävään käytetty aika"), max_digits = 3, decimal_places=2, null=True, help_text=_(u"Arvioi tunneissa, kuinka kauan tehtävään käytetään keskimäärin aikaa"), ) kertaa = models.IntegerField( verbose_name=_(u"Kertaa per kuukausi"), null=True, blank=True, help_text=_(u"Arvioi kuinka monta kertaa kuukauden aikana tehtävä tehdään") ) def __unicode__(self): return self.organisaation_nimi After running the makemigrations command, it starts to create the make-file: Migrations for 'app': 0002_auto_20170809_1628.py: - Create model Kysymys - Add field kertaa to organisaatio - Add field kesto to organisaatio - Add field kuvaus to organisaatio - Alter field organisaation_koko on organisaatio - Alter field organisaation_nimi on organisaatio and then spits out an unicode error: django.utils.encoding.DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 21: ordinal not in range(128). You … -
Django - Media in production is not working
My static files are working well, but my media ones have a problem. I already deployed my website in heroku and it is online in production. (DEBUG=False) Look at the code: settings.py BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DEBUG = bool(os.environ.get('DJANGO_DEBUG', True)) STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, "static"), # '/var/www/static/' ] STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') MEDIA_ROOT = os.path.join(BASE_DIR, "media") MEDIA_URL = '/media/' import dj_database_url db_from_env = dj_database_url.config(conn_max_age=500) DATABASES['default'].update(db_from_env) STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' urls.py if settings.DEBUG: urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) models.py cartaz = models.FileField(default='/static/images/logo.jpg') index.html <img src="{{campeonato.cartaz.url}}" class="img-responsive"> My Github repository Here is the problem, I was uploading an image in the admin section and then it worked well, but suddenly after a couple minutes the image disappeared from the website and I got the following error: Not Found:/media/tdscampeonatos_rzFCbET.jpg Then I added a media repository inside my project in github with the images I had uploaded: My Github repository after I added the media Then the images I had added to the media repository worked, but the ones I didn't added was still not working. I uploaded some more images and I got the same error, because the images was not going directly to the media path I had … -
FieldErro:Cannot resolve keyword 'date_added' into field. Choices are: data_added, id, text, topic, topic_id
I'm a Django beginner.There is a strange problem. I don't know why? Here is the traceback. Environment: Request Method: GET Request URL: http://localhost:8000/topics/1/ Django Version: 1.11.4 Python Version: 2.7.12 Template error: In template /home/hu/learning_log/learning_logs/templates/learning_logs/base.html, error at line 0 Cannot resolve keyword 'date_added' into field. Choices are: data_added, id, text, topic, topic_id 1 : <p> 2 : <a href="{% url 'learning_logs:index' %}">Learning Log</a> 3 : <a href="{% url 'learning_logs:topics' %}">Topics</a> 4 : </p> 5 : 6 : {% block content %}{% endblock content %} I hide the traceback Exception Type: FieldError at /topics/1/ Exception Value: Cannot resolve keyword 'date_added' into field. Choices are: data_added, id, text, topic, topic_id -
Ubuntu server, Django 1.11.4, gunicorn, nginx can not find css files
Got problem with static css. I already did collectstatic. Css works fine if run ./manage.py runserver 0.0.0.0:8000 but can not be found if run gunicorn --bind 0.0.0.0:8000 hotel_main.wsgi:application settings.py STATIC_URL = '/static/' STATIC_ROOT = "/opt/static/" STATICFILES_DIRS = [ ('static', os.path.join(BASE_DIR, 'hotel_site/static'),), ('static', os.path.join(BASE_DIR, 'static'),), ] Urls.py from django.conf.urls import include, url from django.conf.urls.static import static from django.conf import settings from django.contrib import admin urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^ckeditor/', include('ckeditor_uploader.urls')), url(r'^', include('hotel_site.urls')), ] if settings.DEBUG: urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) Nginx file upstream 78.155.199.17 { server localhost:8000 fail_timeout=0; } server { listen 80; server_name 78.155.199.17; return 301 78.155.199.17$request_uri; } server { listen 80; server_name 78.155.199.17; location /static/ { root /opt/; } location /media/ { root /root/; } location / { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_redirect off; proxy_pass 78.155.199.17; } } Project tree opt/ └─ static/ ├─admin/ ├─ckeditor/ └─static/ └─foundation/ root/ └─ ramn_hotel/ -
Django rest auth manual login from registration
how can i manually login a user from registration, how to call authenticate or login function to generate the token from the save_user() in adapter.py in this case i am using custom adapter. Reason: When a user is trying to register for second time, i need to login the user manually