Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How does set_group_by works in Django?
I was writing the following query: claim_query = ClaimBillSum.objects.filter(claim__lob__in = lobObj)\ .annotate(claim_count = Count("claim__claim_id", distinct=True))\ .annotate(claim_bill_sum = Sum("bill_sum"))\ .values("claim__body_part", "claim_count", "claim_bill_sum")\ .order_by("claim__body_part") When I checked the query property, it was grouped by all properties of the tables related in this query, not only the ones selected in the values() function, when I only wanted to group by claim__body_part. As I searched for a way to change the group by instruction, I found the query.set_group_by() function, that when applied, fixed the query in the way I wanted: claim_query.query.set_group_by() SELECT "CLAIM"."body_part", COUNT(DISTINCT "claim_bill_sum"."claim_id") AS "claim_count", SUM("claim_bill_sum"."bill_sum") AS "claim_bill_sum" FROM "claim_bill_sum" INNER JOIN "CLAIM" ON ("claim_bill_sum"."claim_id" = "CLAIM"."claim_id") WHERE "CLAIM"."lob_id" IN (SELECT U0."lob_id" FROM "LOB" U0 WHERE U0."client_id" = 1) GROUP BY "CLAIM"."body_part" ORDER BY "CLAIM"."body_part" ASC But I couldn't find any information in Django documentation or anywhere else to better describe how this function works. Why the default group by is selecting all properties, and how .set_group_by() works, selecting exactly the property I wanted? -
NoReverseMatch: Reverse for 'account_confirm_email' not found. [dj-rest-auth]
I am new to Django. I'm trying to implement jet authentication along with social authentication. I'm following this tutorial https://jkaylight.medium.com/django-rest-framework-authentication-with-dj-rest-auth-4d5e606cde4d I tried to implement the same but its not working. I'm getting this error: django.urls.exceptions.NoReverseMatch: Reverse for 'account_confirm_email' not found. 'account_confirm_email' is not a valid view function or pattern name. My project level urls.py from drf_spectacular.views import ( SpectacularAPIView, SpectacularSwaggerView ) from django.contrib import admin from django.urls import path, include urlpatterns = [ # path('account/', include('allauth.urls')), path('admin/', admin.site.urls), path('api/user/', include('user.urls')), path('api/schema/', SpectacularAPIView.as_view(), name='api-schema'), path( 'api/docs/', SpectacularSwaggerView.as_view(url_name='api-schema'), name='api-docs' ), ] My App level urls.py from django.urls import path, re_path from dj_rest_auth.registration.views import RegisterView, VerifyEmailView, ConfirmEmailView from dj_rest_auth.views import LoginView, LogoutView from user import views app_name = 'user' urlpatterns = [ path('account-confirm-email/<str:key>/', ConfirmEmailView.as_view()), path('register/', RegisterView.as_view()), path('login/', LoginView.as_view()), path('logout/', LogoutView.as_view()), path('verify-email/', VerifyEmailView.as_view(), name='rest_verify_email'), path('account-confirm-email/', VerifyEmailView.as_view(), name='account_email_verification_sent'), re_path(r'^account-confirm-email/(?P<key>[-:\w]+)/$', VerifyEmailView.as_view(), name='account_confirm_email'), path('listusers/', views.ListUsers.as_view(), name='list-users'), ] When I try to register a user. It causes this error. I'm using dj-rest-auth package to implement authentication. -
how to solve unusual logical error in django
So I made this transaction process that will pass with the help of phone number. But the logic is not working properly. sender side is deducting way more numbers that i want to send. for example, if i want to send 100, it's deducting 500 and receiver is not receiving the amount. can anyone help me with this? models.py class extenduser(models.Model): ID= models.IntegerField(null=True, default=None) FirstName= models.CharField(max_length= 50) MiddleName= models.CharField(max_length=50) LastName= models.CharField(max_length=50) phone= models.CharField(max_length=20) user= models.OneToOneField(User, on_delete=models.CASCADE) class Status (models.Model): user_name = models.CharField(max_length=150, default=None) account_number = models.IntegerField() balance = models.IntegerField() phone_number= models.CharField(max_length=20,default=None) class MoneyTransfer(models.Model): enter_your_user_name = models.CharField(max_length = 150, default = None) enter_the_destination_account_number = models.IntegerField() enter_the_destination_phone_number=models.CharField(max_length=20, default=None) enter_the_amount_to_be_transferred_in_INR = models.IntegerField() views.py def randomGen(): # return a 6 digit random number return int(random.uniform(100000, 999999)) def index(request): try: curr_user = Status.objects.get(user_name=request.user) # getting details of current user except: # if no details exist (new user), create new details curr_user = Status() curr_user.account_number = randomGen() # random account number for every new user curr_user.balance = 0 curr_user.user_name = request.user curr_user.phone_number= extenduser.phone curr_user.save() return render(request, "epayapp/index.html", {"curr_user": curr_user}) def TransferMoney(request): if request.method == "POST": form = forms.MoneyTransferForm(request.POST) if form.is_valid(): form.save() curr_user = models.MoneyTransfer.objects.filter(enter_your_user_name=request.user).first() dest_user_acc_num = curr_user.enter_the_destination_account_number dest_phone_num= curr_user.enter_the_destination_phone_number temp = curr_user # NOTE: Delete this … -
How do I create a cookiecutter project for Django 4.x instead of the default 3.x?
I want to start a new django project with cookiecutter and would like to leverage the Django 4.0+ for it. Is this supported with cookiecutter, or is there a reliable workaround for it? -
django nested Prefetch with to_attr not working
so i have this kind of queryset: prods = Product.objects.prefetch_related( Prefetch( 'packs', queryset=Pack.objects.all(), to_attr='my_packs'), Prefetch( 'packs__orders', queryset=Order.objects.all(), to_attr='my_orders') ) pack has a foreign key to product, and pack with order has a m2m relation. so this prods[0].my_packs works. but there is no attribute called my_orders in my qs -> prods[0].my_orders why does this happen? and how can i make this work? -
django objects.create method is too slow How to make faster?
multiple tables are mapped and, when I create post request, it takes about 2~3 seconds. Is there any ways to fix it? I guess it takes a long time on: objects.create for loop product.objects.get however, I am not able to find the better ways.. models: #product, Order, OrderItems, ShippingAddress are mapped class Order(models.Model): user = models.ForeignKey(User, on_delete= models.CASCADE) order_date = models.DateTimeField(auto_now=True) is_paid = models.BooleanField(default=False) paid_at = models.DateTimeField(auto_now=False, null=True, blank=True) delivery_code = models.CharField(max_length=255, null=True, blank=True) is_delivered = models.BooleanField(default=False) delivered_date = models.DateTimeField(auto_now=False, null=True, blank=True) total_price = models.DecimalField(max_digits=7, decimal_places=2, null=True) shipping_price = models.DecimalField(max_digits=7, decimal_places=2, null=True) payment_method = models.CharField(max_length=255,null=True) def __str__(self): return str(self.user) class OrderItem(models.Model): order = models.ForeignKey(Order, on_delete= models.CASCADE, null=True, blank=True) product = models.ForeignKey(Product, on_delete= models.CASCADE) name = models.CharField(max_length=200, null=True) image = models.CharField(max_length=255, null=True) qty = models.IntegerField(default=0, null=True) price = models.DecimalField(max_digits=7, decimal_places=2, null=True) def image_preview(self): if self.image: return mark_safe('<img src="{0}" width="55" height="55" />'.format(self.image)) else: return '(No image)' def __str__(self): return str(self.product) class ShippingAddress(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) order = models.OneToOneField(Order, on_delete=models.CASCADE, null=True, blank=True) address = models.CharField(max_length=255, null=False) city = models.CharField(max_length=255, null=False) postal_code = models.CharField(max_length=255, null=False) country = models.CharField(max_length=255, null=False) def __str__(self): return str(self.user) view: @permission_classes(IsAuthenticated) @api_view(['POST']) def OrderCreate(request): data = request.data user = request.user order_items = data['orderItems'] #1.create order order = Order.objects.create( user … -
Django serializers.save() does not use postgres
I have the following function that runs automatically with cronjob, it makes predictions based on an anomaly detection model and it saves the results to a postgres database: def prediction_job(): """ This job will run once every batch_minutes minutes to predict if the IPs appear in these batch_minutes minutes are normal or malicious and save the results to postgres """ ip_estimator = IpStatePrediction() predictions = ip_estimator.predict_ip_state() predictions_dict_final = convert_dict_for_serializer(predictions) serializer_result = PredictionsSerializer(data=predictions_dict_final) if serializer_result.is_valid(): serializer_result.save() else: logger.debug("Error with data {}.".format(serializer_result.errors)) When I run it locally after running the commands python3 manage.py runserver and python3 migrate --database postgres, it saves the results to postgres. However on production I am getting the following error, when the function is executed: Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 84, in _execute return self.cursor.execute(sql, params) File "/usr/local/lib/python3.8/site-packages/django/db/backends/sqlite3/base.py", line 413, in execute return Database.Cursor.execute(self, query, params) sqlite3.OperationalError: unrecognized token: ":" The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 108, in debug_sql yield File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 98, in execute return super().execute(sql, params) File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 66, in execute return self._execute_with_wrappers(sql, params, many=False, executor=self._execute) File "/usr/local/lib/python3.8/site-packages/django/db/backends/utils.py", line 75, in _execute_with_wrappers return executor(sql, params, many, context) File … -
module 'django.contrib.admin' has no attribute 'action' this code is copied from the docs
from django.contrib import admin from .models import Post @admin.action(description="Publish selected Posts") def make_publish(modeladmin,request,queryset): queryset.update(status="published") -
Django FileField reading big CSV files effectively
Lets say that I have model that contains csv files: class MyModel: my_file = model.FileField(...) I can read my csv file data like this right now: import csv csv_data = self.my_file.read().decode('utf-8') reader = csv.reader(csv_data.splitlines()) However this method is not effective. It literally loads whole file into memory and tries to work with them. My CSV files are very big (more than 100MB) and they are hosted on S3 service. Is there any effective way to parse CSV data? -
Celery executes the task but not reflects them in the celery logs. Why?
For eg it runs perfect the first time but if we try to run the same thing again so if it ran 3 tasks at first it will show only 1 of them (first or last of the three). But will execute all of them. The command use to run celery here is celery -A project worker -B -l info --scheduler django_celery_beat.schedulers:DatabaseScheduler The logs in the celery looks like for the first time task received 1 task received 2 task received 3 For the second time it may be task received 3 or task recevied 1 #either of these 2 But will still execute all three of them. So what may be the thing preventing them -
Why this signal is not working as expected?
I would appreciate some help. Does anyone know why this signal is only being triggered only when the "xp value" is different from the one that already exist ? so eg. if a UserLeaderboardTracking objects already exist for that user with the value 10 the it won't create it otherwise it will . def create_user_leaderboard_tracking(sender, instance, *args, **kwargs): if instance.xp_collected == 0: pass else: UserLeaderboardTracking.objects.get_or_create(user=instance.user, xp_value=instance.xp_collected) -
Is `python_paths` a correct parameter for pytest.ini
I'm working on an undocumented Django application, as a new team (all previous developper went away , so no help is available) The pytest.ini looks like this : [pytest] python_paths = my_app According to pytest documentation, there is no python_paths option, only pythonpath. The tests are working fine by now, and I don't know what to do : Keep that option, it may somehow be used Rename to pythonpath, like it should always has been Delete, as it is unused anyway (I tested, it works fine without) I know StackOverflow isn't meant for opinions, I'll try to find a lead dev at my company for that. I'm just looking for confirmation that i'm not missing an usage -
I am having problem with installing SSL Certificate on my ubuntu server running on apache2
I have SSL Certificate, namecheap enabled it, and now I am trying to install it on my ubuntu server running on Apache2. I have my django project setup, and logs are showing that is django/Python side problem. logs. When I am running apache2 on port 80 it works completely fine. sites-available SSL config: ssl-config sites-available http config: http-config -
Django: how to override textarea globally with custom class?
I'm trying to override widgets (like textarea) globally in Django. For this, I defined my own django/form/widgets/textarea.html file. First, I wanted to modify the default number of rows. The textarea.html file will looked like this: <textarea rows="3" name="{{ widget.name }}" {% include "django/forms/widgets/attrs.html" %} >{% if widget.value %}{{ widget.value }}{% endif %}</textarea> This example above works perfectly. But what if I want to add a custom class now? <textarea rows="3" class="my-custom-class" name="{{ widget.name }}" {% include "django/forms/widgets/attrs.html" %} >{% if widget.value %}{{ widget.value }}{% endif %}</textarea> This won't work as expected because the attrs.html is supposed to handle extra attributes like class. Hence, defining a class attribute directly in textarea would break this behaviour and would erase existing classes. What is the "clean" way to add extra classes globally? I'd like to do it in HTML, not in Python. For example, I don't want to define a new Widget which would inherit from the base widget when I'm defining my forms, as I think this is pure markup topic, and should not interfere with form definition itself. Thanks. -
Django Meta class abstract changing from True to False
I understand that when a class inherits from an Abstract Django Model, it will not inherit the meta attribute abstract = True, which makes sense. However in the below example nothing has inherited from it, but yet it's Meta.abstract is False even though its defined to be True: from django.db import models from django.db.models.base import ModelBase class MyMeta(ModelBase): def __new__(cls, name, bases, attrs, **kwargs): Class = super().__new__(cls, name, bases, attrs, **kwargs) if not Class.Meta.abstract: print(Class) print('Class.Meta.ordering:', Class.Meta.ordering) # Sanity check print('Class.Meta.abstract:', Class.Meta.abstract) if not hasattr(Class, 'foo'): raise NotImplementedError('Please add a foo attribute') return Class class MyAbstractModel(models.Model, metaclass=MyMeta): name = models.CharField(max_length=250) class Meta: abstract = True Prints: <class 'myapp.models.base.MyAbstractModel'> Class.Meta.ordering: -name Class.Meta.abstract: False -
Django & Authorization Bearer Token
I'm working on Django for an app refrencial on job. I want to get data from an API Pole Emploi. I'm register on the web site, I got a client_id, client_secret etc. I do a get request to get my access_token. def login_explo_metier_pole_emploi(): params = { 'grant_type': 'client_credentials', 'client_id': 'XXX', 'client_secret': 'XXX', 'scope': 'api_explorateurmetiersv1 explojob' } header = { 'Content-Type': 'application/x-www-form-urlencoded' } url_co = 'https://entreprise.pole-emploi.fr/connexion/oauth2/access_token?realm=%2Fpartenaire' req_co = requests.post(url_co, params, header) wb = req_co.json() access_token = wb['access_token'] print('reponse: ', wb) return access_token That's working. After, I want on my view, get datas, but here, it's not working : def get_rome(param): access = login_explo_metier_pole_emploi() print('acces avec: ', access) params = request.META.get('HTTP_AUTHORIZATION', f"Bearer {access}") url = f"https://api.emploi-store.fr/partenaire/explorateurmetiers/v1/explorateurmetiers?libelle={param}&nombre=20&type=metier" req = requests.get(url, params) return req It's returned an 401 error (Unauthorized). Can someone help me please? -
Django how to create a tmp excel file and return it to the browser within the response
I have a process to build a tmp file and then return it to the browser in csv. Now i want to do the same but return a excel file. So what i have for the csv is a view in django that does: def export_wallet_view(request): tmp = tempfile.NamedTemporaryFile(delete=False) with open(tmp.name, 'w', encoding="utf-8-sig") as fi: csv_headers = [ 'Id', 'Name' ] fi.write(';'.join(csv_headers)) fi.write('\n') //here also i save the rows into the file response = FileResponse(open(tmp.name, 'rb')) response['Content-Disposition'] = 'attachment; filename="wallet.csv"' return response So to convert it to excel i try to do something like this using pandas: df = pd.read_csv(tmp.name) df.to_excel('pandas_to_excel.xlsx', sheet_name='new_sheet_name') The problem is that this creates the excel in the server, and i would like to do something like: df = pd.read_csv(tmp.name) df.to_excel('pandas_to_excel.xlsx', sheet_name='new_sheet_name') //this being a tmp file response = FileResponse(open(tmp.name, 'rb')) //this should be the new excel tmp file response['Content-Disposition'] = 'attachment; filename="wallet.csv"' return response Thanks -
Active class on first element of nav-tab with pagination
I have a project where I have a paginated nav-tab with bootstrap. The problem is that, anytime I refresh the page or I change the page with my pagination buttons, the active element is always the element n° 1 of the list. This is probably due to the fact that I need to activate it upon opening the page with an if condition within the forloop. Let me post some code to be more clear template <input type="hidden" id="quizProfileId" value="{{pk}}"> <div class="card"> <div class="card-body"> <h4 class="card-title ms-3">New {{quiz_attempt.question.question_subject}} Quiz</h4> <div class="row"> <div class="col-md-9"> <div class="tab-content text-muted mt-4 mt-md-0" id="v-pills-tabContent"> {% for quiz in quizzes %} <div class="tab-pane {% if forloop.counter == 1 %}active{% endif %} ms-3 questionsObj" id="question{{quiz.question_draft.question.id}}" value="{{quiz.question_draft.question.id}}"> <h5 class="text-dark">{{quiz.question_draft.question.text}}</h5> <div class="list-group mt-3"> {% for image in quiz.question_draft.question.questionimage_set.all %} <a href="{{image.image.url}}" target="_blank"> <img src="{{image.image.url}}" alt="" width="100px" height="100px"> </a> <br> {% endfor %} {% for answer in quiz.question_draft.answerdraft_set.all|shuffle %} <a href="#" data-pk="{{quiz.question_draft.question.id}}" id="answer{{answer.answer.id}}" data-answerId="{{answer.answer.id}}" class=" list-group-item answer question{{quiz.question_draft.question.id}} list-group-item-action {% if quiz.question_draft.is_answered == True and answer.answer.is_correct == True %}bg-success{% endif %} {% if answer.is_answered == True and answer.is_correct == True %}bg-success disabled {% elif answer.is_answered == True and answer.is_correct == False %}bg-danger disabled {% elif quiz.question_draft.is_answered == True %} disabled … -
Is it good database design?
I have this task to create api: City district - name, id Category - name, id Organization Network - name, id Organization Belongs to one of the organization networks id, name, description belongs to several districts, can be represented in several of them at the same time have list of products with prices Product id, name, category can be sold in one or several organizations in network price can be different depending on the organization This is my try to design this database, am I missing something? And also in Django: from django.db import models class District(models.Model): name = models.CharField(max_length=100, unique=True) class Category(models.Model): name = models.CharField(max_length=100, unique=True) class OrganizationNetwork(models.Model): name = models.CharField(max_length=100, unique=True) class Organization(models.Model): name = models.CharField(max_length=100, unique=True) description = models.TextField() organization_network = models.ForeignKey( OrganizationNetwork, on_delete=models.CASCADE, related_name="organizations", ) district = models.ManyToManyField( "District", through="DistrictOrganization", through_fields=("organization", "district"), related_name="organizations", ) products = models.ManyToManyField( "Product", through="OrganizationProduct", through_fields=("organization", "product"), related_name="organizations", ) class Product(models.Model): name = models.CharField(max_length=100, unique=True) category = models.ForeignKey( Category, on_delete=models.CASCADE, related_name="products" ) class DistrictOrganization(models.Model): district = models.ForeignKey(District, on_delete=models.CASCADE) organization = models.ForeignKey(Organization, on_delete=models.CASCADE) class OrganizationProduct(models.Model): price = models.DecimalField(max_digits=10, decimal_places=2) organization = models.ForeignKey(Organization, on_delete=models.CASCADE) product = models.ForeignKey(Product, on_delete=models.CASCADE) -
Accessing sqlite database running on server
I am new to Django and I have a problem, I developed an application using SQLite database in my project with django and I published this project on a server using docker and this project is now used by people and data is saved by entering it. I am doing an update now and when I want to publish this update, the data entered by the users is not in the SQLite file, but when I log in to the application, I see that this data exists. I don't know how I can do this update before this data is lost or how can I get this data and return my database to PostgreSQL, can you help with this? -
Django `TimeField` and timezone
Django documents says "Timezone withoud date no make sence" but I think it does. For example: DB store time of day start. User from Chicago saved start time as 05:00 am. System need to generate report for every day and use this start time with date (datetime field). So for summer days it will 10:00 by UTC, but for winter it will 11:00 by UTC. The question is: how to generate datetime from frimefield and current date? Store timezone in special separate field? https://docs.djangoproject.com/en/3.2/topics/i18n/timezones/#concepts -
queryset get data of the foreign key
I have 2 models ( Users and Posts ) class Users(models.Model): email = models.CharField(max_length=225) class Posts(models.Model): user = models.ForeignKey(Users, on_delete=models.CASCADE, default=1) type = models.TextField() I want to include the user email when getting all posts. I have done the following but am only getting the user id. class PostsViewSet(viewsets.ModelViewSet): serializer_class = PostsSerializer def get_queryset(self): queryset = Posts.objects.all() return queryset How can I achieve to get the user email within the queryset ? -
Django views query output mixup when multiple users are using, how to prevent that
Hi am trying to select an option in html page and after running and doing some operations am trying to print those data For single user it is working fine but when multiple users are running at almost same time The data s getting mixed up and showing duplicate data and some time it is showing half data Could any one tell me how to saparate each user session and prevent output mixup Any leads would be very helpful, thanks -
pipenv - ERROR: Couldn't install package: psycopg2
I am running pipenv on macOS Monterey 12.4. I have the following Pipfile: url = "https://pypi.python.org/simple" verify_ssl = true name = "pypi" [packages] django = "<4.1" pyparsing = "*" pydot = "*" django-extensions = "*" coverage = "*" djangorestframework = "~=3.13" Markdown = "*" django-nested-admin = "*" drf-nested-routers = "*" drf-access-policy = "~=1.0" drf-yasg = "*" drf-social-oauth2 = "*" django-cors-headers = "*" black = "*" django-stubs = "*" djangorestframework-stubs = "*" dj-database-url = "*" gunicorn = "*" whitenoise = "*" psycopg2-binary = "*" psycopg2 = "*" sentry-sdk = "*" django-filter = "*" django-hashid-field = "*" channels = "~=3.0" djangochannelsrestframework = "*" celery = "==5.2.2" django-celery-results = "==2.2.0" channels-redis = "*" requests = "*" django-silk = "*" django-auto-prefetching = "*" drf-viewset-profiler = "*" django-lifecycle = "*" django-notifications-rest = "*" django-notifications-hq = "*" django-postgrespool2 = "*" [dev-packages] mypy = "*" [requires] python_version = "3.8" This has always worked fine up to now. I tried running the command: pipenv update sentry-sdk, and this is the output: (backend) samuelebonini@Samueles-MacBook-Pro-2 backend % pipenv update sentry-sdk Locking [dev-packages] dependencies... Building requirements... Resolving dependencies... ✔ Success! Locking [packages] dependencies... Building requirements... Resolving dependencies... ✔ Success! Updated Pipfile.lock (a39560)! Installing dependencies from Pipfile.lock (a39560)... An error occurred while … -
GitHub for Django on Azure: Could not open requirements file: [Errno 2] No such file or directory: 'requirements.txt'
I am trying to deploy my Django code to Azure with GitHub actions. On GitHub actions I keep receiving the following error message: However, I have included a requirements.txt file in my project. I have tried moving the file to different folders, since GitHub actions does not seem to be able to find the file, but that does not resolve my issue. How can I see where Github actions is looking for my requirements, or does anyone have a suggestion on how to solve this?