Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Proper way to annotate a rank field for a queryset
Assume models like this: class Person(models.Model): name = models.CharField(max_length=20) class Session(models.Model): start_time = models.TimeField(auto_now_add=True) end_time = models.TimeField(blank=True, null=True) person = models.ForeignKey(Person) class GameSession(models.Model): game_type = models.CharField(max_length=2) score = models.PositiveIntegerField(default=0, blank=True) session = models.ForeignKey(Session) I want to have a queryset function to return total score of each person which is addition of all his games score and all times he has spent in all his sessions alongside with a rank that a person has relative to all persons. Something like below: class DenseRank(Func): function = 'DENSE_RANK' template = '%(function)s() Over(Order by %(expressions)s desc)' class PersonQuerySet(models.query.QuerySet): def total_scores(self): return self.annotate(total_score=some_fcn_for_calculate).annotate(rank=DenseRank('total_score')) I could find a way to calculate total score, but dense rank is not what I want, because it just calculates rank based on persons in current queryset but I want to calculate rank of a person relative to all persons. I use django 1.11 and postgres 10.5, please suggest me a proper way to find rank of each person in a queryset because I want to able to add another filter before or after calculating total_score and rank. -
Struggling to save objects to Django database thorough Celery Beat on Digital Ocean
I'm struggling to save objects through Celery Beat to my Django app (showing OHLC data). This script works fine on local environment (saves 3M objects) but not on VPN like Digital Ocean. It saves a certain amount of objects (roughly 200K objects or 2GB) but then it removes other objects to add each new object, which is totally confusing. My stack Django Redis Supervisor Ubuntu I'm NOT using Supervisor on my local, so I think this is causing the issue but can't identify. Any feedback / help would be really appreciated. Script @periodic_task( # run_every=(crontab(minute='*/1')), run_every=(crontab(minute='*/60')), name="load_data", ignore_result=False ) def load_data(): # Forex OHLC TOKEN = MYTOKEN con = fxcmpy.fxcmpy(access_token = TOKEN, log_level = 'error') start = dt.datetime(2010, 1, 1) stop = dt.datetime.today() df = pd.DataFrame(list(DatasourceItem.objects.filter(datasource__sub_category__exact='Forex').values('symbol'))) for i in df['symbol']: datasource_item_obj = DatasourceItem.objects.get(symbol=i) Ohlc.objects.filter(datasource = datasource_item_obj).delete() if datasource_item_obj.base_symbol: base_symbol = datasource_item_obj.base_symbol tar_symbol = datasource_item_obj.tar_symbol mod_symbol = base_symbol + "/" + tar_symbol sys_symbol = base_symbol + tar_symbol else: sys_symbol = datasource_item_obj.symbol mod_symbol = datasource_item_obj.symbol data = con.get_candles(mod_symbol, period='D1', start=start, stop=stop) del data['askopen'] del data['askclose'] del data['askhigh'] del data['asklow'] del data['tickqty'] data.columns = ['Open', 'Close', 'High', 'Low'] data = data[['Open', 'High', 'Low', 'Close']] data.insert(loc=0, column='Symbol', value=sys_symbol) data.reset_index(level=0, inplace=True) data.dropna() # .values = … -
Django issue with json : 'tuple' object has not attribute '_meta'
I'm trying to export data from queryset to different file formats and I'm getting an issue with json format. This is my little piece of code : from django.core import serializers def export_categories_json(request): with open("categories.json", "w") as out: data = serializers.serialize("json", Category.objects.all().values_list('id', 'name')) out.write(data) Then, I set in my template a button which call this function and should download the json file. But I have this issue : Traceback: File "/home/val/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 41. response = get_response(request) File "/home/val/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request) File "/home/val/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/val/Bureau/Projets/Publication/publication/src/web/views/exports.py" in export_categories_json 276. data = serializers.serialize("json", Category.objects.all().values_list('id', 'name')) File "/home/val/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/core/serializers/__init__.py" in serialize 129. s.serialize(queryset, **options) File "/home/val/.pyenv/versions/Publication3.6.2/lib/python3.6/site-packages/django/core/serializers/base.py" in serialize 84. concrete_model = obj._meta.concrete_model Exception Type: AttributeError at /Category/json Exception Value: 'tuple' object has no attribute '_meta' Something is wrong in my code ? I can't send a queryset to json file ? -
Cant change how Username field validates when changing user in Django Admin
(Im using allauth to handle user authentication in my djando app) For several reasons I need to modify how the username validates when users sign up and when they are modified in de admin interface. Ive managed to override allauth username validator using the ACCOUNT_USERNAME_VALIDATORS = [] in settings.py This works fine for when users sign up through the sign up form. The problem is that when I try to change user information in Django admin interface, I cant save users that cant be validated with de built-in username validators. error message when trying to update users that contain spaces or special characters I've also tried changing how clean_username function works in adapters. py but still doesn't work. How can I override or change how the username validates when saving/updating users through the admin interface? Here is my admin.py class UserAdmin(BaseUserAdmin): inlines = [PerfilInline] list_display = ( 'equipo', 'usuario1', 'usuario2', 'plataforma', 'email', 'comentario', 'ver', 'prekills', 'postkills') def ver(self, obj): return obj.perfil.VERIFICACION_2 ver.boolean = True def equipo(self, obj): return obj.perfil.equipo def prekills(self, obj): return obj.perfil.prekills_1 def postkills(self, obj): return obj.perfil.postkills_1 def usuario2(self, obj): return obj.first_name def usuario1(self, obj): return obj.username def plataforma(self, obj): return obj.last_name def comentario(self, obj): return obj.perfil.comentario #get_author.short_description … -
select distinct values from JSON using django-mysql
I'm using this library and my model looks like this: class PhoneTest(Model): data = JSONField() My JSON obj looks something like this (in a real obj there are way more fields): { "deviceStatus": true, "officerCode": 123456, "imei": 123456789123456 } For instance, I want to get a list of all officerCodes. How do I do that ? All I've tried so far has not worked. For example this did not: tests = PhoneTests.objects.all() tests.distinct('data__mOfficerCode') It gives me the following error: NotSupportedError: DISTINCT ON fields is not supported by this database backend But it's because I'm using this new library, not the native django mysql backend. What are possible workarounds? I would greatly appreciate any help. -
Django Error (Attribute): 'CharField' object has no attribute 'is_related'
I am trying to make a description to every user, in my new project. But i get an error when i try to makemigrations. I do not know how to fix it. I have tried different things but nothing worked, my coding is maybe very bad, but i am also new to python and django. The Error: C:\Users\bruger\Dropbox\min-login-web\web_login>python manage.py makemigrations Traceback (most recent call last): File "manage.py", line 15, in <module> execute_from_command_line(sys.argv) File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line utility.execute() File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 316, in run_from_argv self.execute(*args, **cmd_options) File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 353, in execute output = self.handle(*args, **options) File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\management\commands\makemigrations.py", line 143, in handle loader.project_state(), File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\loader.py", line 322, in project_state return self.graph.make_state(nodes=nodes, at_end=at_end, real_apps=list(self.unmigrated_apps)) File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\graph.py", line 378, in make_state project_state = self.nodes[node].mutate_state(project_state, preserve=False) File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\migration.py", line 87, in mutate_state operation.state_forwards(self.app_label, new_state) File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\operations\models.py", line 85, in state_forwards list(self.managers), File "C:\Users\bruger\AppData\Local\Programs\Python\Python37\lib\site-packages\django\db\migrations\state.py", line 377, in __init__ if field.is_relation and hasattr(field.related_model, '_meta'): AttributeError: 'CharField' object has no attribute 'is_relation' My Models file: from django import forms from django.db import models from django.db.models.signals import post_save from django.contrib.auth.models import User from PIL import Image class Profile(models.Model): user … -
Django ValueError: The 'image' attribute has no file associated with it
I have done this in my base template: {% if user.profile.image %} <img class="user-image" src="{{ user.profile.image.url }}/" alt="User profile picture"> {% else %} <img class="user-image" src="{% static 'userprofile/download (1).jpg' %}"> {% endif %} And in profile details page have done this: {% if profile_details.image %} <img class="profile-user-img img-responsive img-circle" src="{{ profile_details.image.url }}/" alt="User profile picture"> {% else %} <img class="profile-user-img img-circle" src="{% static 'userprofile/download (1).jpg' %}"> {% endif %} And in my models I have done this: from PIL import Image from io import BytesIO from django.core.files.uploadedfile import InMemoryUploadedFile import sys class Profile(models.Model): Date = models.DateTimeField(auto_now_add=True) Full_Name = models.CharField(max_length=32,blank=True) Name = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE) E_mail = models.EmailField(max_length=70,blank=True) image = models.ImageField(upload_to='user_images', null=True, blank=True) def save(self, *args, **kwargs): imageTemproary = Image.open(self.image) outputIoStream = BytesIO() imageTemproaryResized = imageTemproary.resize( (128,128) ) imageTemproaryResized.save(outputIoStream , format='JPEG', quality=150) outputIoStream.seek(0) self.image = InMemoryUploadedFile(outputIoStream,'ImageField', "%s.jpg" %self.image.name.split('.')[0], 'image/jpeg', sys.getsizeof(outputIoStream), None) super(Profile, self).save(*args, **kwargs) The problem is coming in this line of code: imageTemproary = Image.open(self.image) Can anyone explain me what is wrong in my code -
At what particular date, How many users are logged in at what time in DJANGO
I need to find out the list of all users logged in to django at what time. At what particular date, How many users are logged in -
Django - JavaScript from static files not loading, but other resources are
I just started playing around with Django and the JavaScript in my static files don't seem to load, while other another resources like logo.png seems to load fine. Am I missing something really basic? Or what I am doing wrong? My setup Using Docker with Python 3.5 with and Django 2.1 as described here My static files are in app\static\app as described here I have a JavaScript file (d3.js) and an image (logo.png) in the above directory In my template I have <script type="text/javascript" href="{% static 'app/d3.js' %}"></script> In settings.py I have DEBUG = True (default) On a Windows environment Tried solutions I can find the files with python manage.py findstatic app/d3.js I tried adding STATICFILES_DIRS as suggested here, but that didn't seem to work. -
Django multiple queries with foreign keys
Let's say I have two different apps : teacher/models.py: Teacher(models.Model): name = models.CharField(max_length=300) class/models.py: Class(models.Model): name = models.CharField(max_length=300) teacher = models.ForeignKey(Teacher) students = models.ManyToManyField(Student) I want to get all the teachers with classes and all classes attached. The result I want: {[ teacher: '3L' #Teachers Id classes: ['20L','14L','30L'] #list of Class objects or ids with the above teacher ], [# similar to above] } Is this possible to do? This is what I am currently doing: classes = Class.objects.all() teachers = Teacher.objects.filter(id__in=classes.value_list('teacher',flat=True).distinct()) for teacher in teachers: classes_for_teachers = classes.objects.filter(teacher=teacher) In the above code, there are four queries made with a loop which certainly increases the time complexity. Is there a better solution to this? Thanks in advance. -
filter nested serializer in django rest framework
Here is my view: class SectorListAPI(generics.ListAPIView): queryset = SectorModel.objects.all() serializer_class = SectorSerializer Here is my serializers: class OrganizationSerializer(serializers.ModelSerializer): class Meta: model = GroupProfile fields = ('title','slug',) class DepartmentSerializer(serializers.ModelSerializer): organizations = OrganizationSerializer(many=True, read_only=True) class Meta: model = DepartmentModel fields = ('title', 'organizations',) class SectorSerializer(serializers.ModelSerializer): # title = serializers.CharField() departments = DepartmentSerializer(many=True, read_only=True) class Meta: model = SectorModel fields = ('title','departments',) in the OrganizationSerializer I want the GroupProfile according to the request.user. But now it gives me all the GroupProfile . How can I get my desired queryset, please explain. -
Security in Django
I am completely new to Python and Django. I am sorry if my question is way too vast. Like security filters in Tomcat to filter out the malicious requests, is there any equivalent of those filters in Django. Also is it possible to use my Tomcat security filters for preprocessing the incoming requests to my Django server?. Thank you!. -
Django shows datetime different then system datetime
When i check "datetime.today().date()" in my views.py i get yesterday's date. But when i check the date from linux system with "date" command i get today's date. I think Django is not updating current date. I have to get current date in the view to make some comparisons, also to print into the view. I am using python 2.7 and Django 1.9 def assistant_page(request, assistant=None): notes = AssistantNotes.objects.filter(notedate=datetime.today().date()).order_by("time") file_write(datetime.today().date()) if request.method == 'GET': return render(request, "assistant_page.html", {'generalnotes': notes}) -
django-csvimport not populating id field
I'm using django-csvimport to populate my app's models, but I just noticed that the autogenerated id field isn't being populated. Whenever I query for {{ model.id }} I just get a blank. When I'm uploading the csv files, the mapping does not mention the id field, but since the autogenerated field should be an AutoField I expected it to self-populate. Is this intended or maybe I'm doing something wrong? -
I have two test classes in django testcases . i want to use use one testclass function to another testclass in python
in python django testcases for restframework class TestExample(APITestCase): def test_ex(self): print "test_ex" class TestSample(APITestCase): def test_sample(self): print "test_sample" how can use test_ex function in test_sample function -
Get the records between two dates
I have a table called Billing which contains the below columns id| resource_name | start_date | end_date | total_amount This table is populated with data for different resource with different start_date and end_dates. I need to retrieve the data from the tables. Sample Record id| resource_name | start_date | end_date | total_amount 1 | abc | 2018-09-15 03:00:00 | 2018-09-15 04:00:00 | 20 If I write a query like this it will return the above result select * from billing where start_date >= '2018-09-15 03:00:00' and end_date <= '2018-09-15 04:00:00' But the below doesn't select * from billing where start_date >= '2018-09-15 03:00:00' and end_date <= '2018-09-15 03:30:00' Is there any way to retrieve the same . I am using Django ORM to do the same -
How to Store images in Django deployed on Google App engine?
I have just started using GAE and cant figure out how to store images. I created Django project and deployed on GAE using the methodology in this tutorial which use cloud SQL (my project uses mysql) Now I want to add a django model for images. I cant figure out how to go about it. I used AWS extensively earlier and stored links in mysql database while file on buckets. Do I need to use google cloud storage? What is the GAE way to do it. -
Keeping Spark Context alive in Django web application
I am building a Django application where I would like my Django server to submit tasks to my Spark cluster (may or may not be managed by Yarn) and get back results as a JSON. These tasks would be asynchronous (so I will be using Celery for it, and also to get progress updates from there). The problem I am currently facing is every time a task is called, a Spark Context is initialised, takes quite some time to start and then my task would run. Is there a way I can keep my SparkContext() alive so that I can just keep submitting tasks to it? Is it possible to submit jobs to a Spark Cluster without calling spark-submit? I am trying to read about Spark Job Server and Apache Livy but I am not understanding how I can use it in my architecture. Note : I am using spark-submit from inside of celery tasks to run the jobs as of now. -
Django:Run pylint on all the apps in one go
I want to run pylint recursively on all my project apps from the root of the app. If i use : pylint . on the root of the project base, it gives me the error that there is no init. How can I run the pylint from my project root without having to specify all the apps one by one. -
Django - Annotate with count across ManytoMany relationships
I am not able to find the way to annotate a queryset with a count of how many times an element is used in a many-to-many relationship. class Profile(models.Model): [...] # Profile can have multiple roles roles = models.ManyToManyField('Role', blank=True) [...] class Role(models.Model): company = models.ForeignKey(Company, on_delete=models.CASCADE) name = models.CharField(blank=True, max_length=30) description = models.CharField(blank=True, max_length=300) [...] For example I would have 5 roles: Role1, Role2, Role3, Role4 and Role4 And 2 profiles: Profile1 and Profile 2. Those 2 Profiles have the following roles assigned: Profile 1: Role 1, Role 2 Profile 2: Role 1, Role 3, Role 4 I want to query the Role model and annotate with the number of profile that have that role. So return a queryset like Role1: company, name, description, profile_count=2 Role2: company, name, description, profile_count=1, etc... I have tried that but it does not work: Role.objects.annotate(profile_count=Count('profile__roles')) It seems to return an overall count and not a count per role. Any idea if that can be done natively in Django or if a raw SQL request is necessary? Thanks! -
Django-Markdownx MarkdownxModelAdmin template error
I tried to test Django-Markdownx in my django project, but it keeps makes error. Here is my models.py. from django.db import models from django.utils import timezone from markdownx.models import MarkdownxField # Create your models here. class Article(models.Model): title = models.CharField(max_length=200) author = models.ForeignKey('auth.User', on_delete=models.CASCADE) article = MarkdownxField() created_date = models.DateTimeField(default=timezone.now) published_date = models.DateTimeField(blank=True, null=True) def publish(self): self.published_date = timezone.now self.save() def __str__(self): return self.title And, here is my admin.py. from django.db import models from django.contrib import admin from markdownx.admin import MarkdownxModelAdmin from .models import Article # Register your models here. admin.site.register(Article, MarkdownxModelAdmin) I did as docs example said, but it keeps make an error like below, when I tried to select any Article objects in admin page. enter image description here I confirmed that if I change the Admin.admin to TextField(), it works properly. Need help.. -
Django: IntegrityError: NOT NULL constraint failed: main_app_tamanioscantidades.usuario_id
I had a model to which I wanted to add current user as a field. Original model: (This model had some records saved in DB before modification) class TamaniosCantidades(models.Model): TAMANIOS = (('498', '2" x 2"',), ('499', '3" x 3"',), ('500', '4" x 4"',), ('501', '5" x 5"',)) CANTIDADES = (('100', '50',), ('100', '100',), ('150', '150',)) tamanios = models.CharField(max_length=10, choices=TAMANIOS) cantidades = models.CharField(max_length=10, choices=CANTIDADES) Modified model: class TamaniosCantidades(models.Model): TAMANIOS = (('498', '2" x 2"',), ('499', '3" x 3"',), ('500', '4" x 4"',), ('501', '5" x 5"',)) CANTIDADES = (('100', '50',), ('100', '100',), ('150', '150',)) usuario = models.ForeignKey(User, on_delete=models.DO_NOTHING) tamanios = models.CharField(max_length=10, choices=TAMANIOS) cantidades = models.CharField(max_length=10, choices=CANTIDADES) Everything works well when done through the admin. But if I try to save a new record of this model I get this Error: IntegrityError at /post_url_tamanioscantidades/ NOT NULL constraint failed: main_app_tamanioscantidades.usuario_id I also modified the views.py file to save the current user to the model when submitting form: Original View: def post_tamanioscantidades(request): tamanioscantidades_form = TamaniosCantidadesForm() if tamanioscantidades_form.is_valid(): tamanioscantidades_form.save(commit = True) return HttpResponseRedirect('/') After modifications: def post_tamanioscantidades(request): tamanioscantidades_form = TamaniosCantidadesForm(request.POST) if tamanioscantidades_form.is_valid(): tamanioscantidades = tamanioscantidades_form.save(commit = False) tamanioscantidades.user = request.user tamanioscantidades.save() return HttpResponseRedirect('/') What am I forgetting? -
DATA_UPLOAD_MAX_MEMORY_SIZE does not limit the size of data sent by POST
In my django project, the value of DATA_UPLOAD_MAX_MEMORY_SIZE is 2.5MB. But I can send +10MB data (base64 image) to my server using curl. How can I limit the requests and why that env does not work in my case? -
Triggering Django management command with post_save signal on multiple servers
I have created a model and post_save signal which will invoke a management command with something like this from django.core import management management.call_command('your_command', your_options) In that management command, I have to restart a process. I have same 15 machines serving with similar django server. If I change the model, post_save will be triggered on one of those machines and process will be started on that machine. However I want that post_save signal to be triggered in all machines. How can I do this? -
Replace some text to bold in django template
I have a variable text = "replace some word" in view. I want to replace 'some' to bold. Like this: "replace some word" How to handle that variable in django template?