Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django: traceback appears on form as text after Bad Request 400
I have ajax views for editing and saving objects. When I am editing an object, the appropriate forms and formsets are loaded with instantiated data. Now I am getting this error when a user tries to add more than 1000 fields: POST parameters exceeded settings.DATA_UPLOAD_MAX_NUMBER_FIELDS. The problem is that the traceback code is inserted directly on the form and becomes visible to all: By design, I am expecting a response code on frontend, which is 400 for django form errors. However, in this case, the 400 error comes from the bad request, and not from form errors. As a result, after saving the object, I have the full traceback displayed right in the form as plain text. How can I avoid this behaviour? I want not to display the traceback/bad request error text on the form to the users. My only idea is to change the default status code of django form errors to some 444 and to display only the 444 errors on forms, while ignoring the 400 errors traceback. But I don't know how to do it and don't think this is the best option either. -
The default django `User` model generating huge number of queries, when working with views in DRF?
I am trying to build an endpoint for the default User model, which we have in django, which being imported from from django.contrib.auth.models import User, This is my serializer:- class SuperUserDetailSerializer(serializers.ModelSerializer): class Meta: model = User fields = ( 'id', 'username', 'groups', 'user_permissions', 'organization_users', ) and the views goes as this:- class UserDetailView(RetrieveUpdateDestroyAPIView): queryset = User.objects.all() serializer_class = SuperUserDetailSerializer with the above codes, the number of queries which being hit are 216-219, which is due to the field user_permissions, I checked the User model, but it doesn't contains any field as user_permissions, but in the response, I get this field, and also in the html form field the permissions are populated(DRF Browsable page). I am trying to implement prefetch_related or select_related for the user_permissions field, but I am not able to reduce the the number of query, (It is hitting the database each time for getting all the permissions). This is what I am trying to do but not able to achieve it:- queryset = User.objects.prefetch_related(Prefetch('user_permissions', queryset = Permission.obects.select_related('content_type')))...I am not sure where I am doing the mistake. -
Django Rest Best practice of handling complex data composition
I am a bit of a django newbiew so forgive me if I miss somehow something obvious. I have a database table representing a linguistinc term called terms. This term can be included in several string attributes of not directly related tables such as say article. Now I have a direct requirment to return those items when aksed for the details of a term. An example schema could look like the following { 'name': 'Some term definiton', 'another_attribute': 'some attribute value', 'articles': ['Articles that include text'] } Not the most efficient way but in order to find the 'associated' articles I have method called find_associated_articles which in essense performs a full text search on the articles text to find potential matches of particular term. No the question is what would be the most Django way of compositing this information in a Rest Response (Idealy in the context of a retrieve action within a viewset)? -
How to upload and process large excel files using Celery in Django?
I am trying to upload and process excel file using Django and DRF with Celery. There is an issue when I am trying to pass the file to my Celery task to be processed in the background, I get a following error: kombu.exceptions.EncodeError: Object of type InMemoryUploadedFile is not JSON serializable Here is my view post request handler: class FileUploadView(generics.CreateAPIView): """ POST: upload file to save data in the database """ parser_classes = [MultiPartParser] serializer_class = FileSerializerXLSX def post(self, request, format=None): """ Allows to upload file and lets it be handled by pandas """ serialized = FileSerializerXLSX(data=request.data) if serialized.is_valid(): file_obj = request.data['file'] # file_bytes = file_obj.read() print(file_obj) import_excel_task.delay(file_obj) print("its working") return Response(status=204) return Response(serialized._errors, status=status.HTTP_400_BAD_REQUEST) And my celery task: def import_excel_helper(file_obj): df = extract_excel_to_dataframe(file_obj) transform_df_to_clientmodel(df) transform_df_to_productmodel(df) transform_df_to_salesmodel(df) @shared_task(name="import_excel_task") def import_excel_task(file_obj): """Save excel file in the background""" logger.info("Importing excel file") import_excel_helper(file_obj) Any idea what is the way to handle importing Excel files into celery task so that it can be processed by other functions in the background? -
Mapbox add marker on click
I am building a website in django, and im using Mapbox as a way to add markers on a map. Right now my solution is a django form, with fields for 'longitude', 'latitude', 'Marker_Title'. A solution would be to either add the longitude and latitude to the form by clicking on the map, or clicking on the map and then a popup appears where you can write the marker title. I am not sure how i get the longitude and latitude information by clicking right now. Here is a picture of how it looks right now: Picture of the map and form I would love if anyone can help me with this! Thanks in advance! -
Compare data from a week(or minute) ago using a queryset in Django
I have some data as follows: class myModel(models.Model): date = models.DateTimeField user_ID = models.CharField index_a = models.CharField cnt_a = models.BigIntegerField cnt_b = models.BigIntegerField I would like to get Result data as follows: date user_ID sum(n.a) sum(-5min) sum(n.b) sum(-5min) 2021-08-01 13:00 a10 7011 6625 9239 8766 2021-08-01 13:00 a20 6968 6628 9238 8765 2021-08-01 13:00 a30 6940 6646 9238 8766 2021-08-01 13:00 a40 6974 6644 9238 8764 2021-08-01 12:55 a10 6625 6621 8766 8692 2021-08-01 12:55 a20 6628 6580 8765 8691 2021-08-01 12:55 a30 6646 6596 8766 8693 2021-08-01 12:55 a40 6644 6614 8764 8692 I tried SQL query as follows: and get the Result as above Table I am trying to use queryset in Django to perform the following query without using raw SQL. any idea how can do that? Thanks in advance! SELECT n.date, n.user_ID, sum(n.cnt_a), sum(p.C_AAA), sum(n.cnt_a), sum(p.C_BBB) FROM myModel AS n LEFT JOIN ( SELECT t1.date, t1.user_ID, t1.index_a, t1.cnt_a as C_AAA , t1.cnt_b as C_BBB FROM myModel AS t1 ) AS p ON p.date = DATE_SUB(n.date, INTERVAL 5 MINUTE) AND p.user_ID = n.user_ID AND p.index_a = n.index_b -
nginx websocket not working with django channels (url not found)
I want to implement a realtime chat app using django channels and redis. Everything works in development, but in production i can't get websockets to be configured correctly. I have gunicorn running as a server for handeling http requests and now i am trying to set up daphne for handeling websockets. Nginx proxies all http requests correctly to gunicorn and therefor all urls using only http work fine. However, when i try to connect to the websocket in my frontend, daphne says that the url was not found. The same happens, when i use uvicorn as an asgi server instead of daphne. Also, when i let daphne handle http and websocket requests, daphne has no problem with http but cannot find the websocket urls. My setup is as follows: settings.py WSGI_APPLICATION = 'config.wsgi.application' ASGI_APPLICATION = 'config.asgi.application' CHANNEL_LAYERS = { 'default': { 'BACKEND': 'channels_redis.core.RedisChannelLayer', 'CONFIG': { 'hosts': [('127.0.0.1', 62148)], }, } } asgi.py import os from channels.auth import AuthMiddlewareStack from channels.routing import ProtocolTypeRouter, URLRouter from django.core.asgi import get_asgi_application os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings.production') django_asgi_app = get_asgi_application() import chat.routing application = ProtocolTypeRouter({ 'http': django_asgi_app, 'websocket': AuthMiddlewareStack( URLRouter( chat.routing.websocket_urlpatterns ) ), }) routing.py from django.conf.urls import url from .consumers import ChatConsumer websocket_urlpatterns = [ url(r"^ws/chat/$", ChatConsumer.as_asgi()), … -
How can I pass two queryset in same function using Django Rest Framework
def getProducts(request): products = Product.objects.all() p = Product.objects.all().filter(category = 1) serializer = ProductSerializer((products,p), many=True) return Response(serializer.data)** I want to pass p and products value in the serializer object. Can I do this here? -
ImportError: cannot import name 'Translatable' from 'translations.models'
When I wrote this:python manage.py makemigrations I get this error:ImportError: cannot import name 'Translatable' from 'translations.models' How can i solve this problem? enter image description here -
Prevent django send_mail timimg attack
I have different REST-API views where I either send a mail (if an account exists) or do not send a mail. For example, the user can input the email in the forgot-password form and a mail is sent if the account exists. I am using from django.core.mail import send_mail to send the mail. The problem is, that this takes some time, and so requests for valid emails are generally longer than requests for non-exiting emails. This allows an attacker to compare the request times to find out if an account exists or not. Is there any way that I can call send_mail() without sending the mail? Or what would be the fix to make request times equally long for both cases? Note: I could check how long send_mail() needs on average and wait this time if I do not send the mail. As the app runs on different servers with different configs, this can not be generally done in my case. I would rather not store the average execution time per server in a database to solve this. -
can't show error message into error function in ajax - django
i'm trying to show error message from server to client site using ajax , but it wont work when i make a function for error messages , #other codes for saving the post,there is not error success_meesage = f'success' return JsonResponse({'success':'success','success_msg':success_meesage}) else: error_message=f'there is an error into your two dates please make sure check in smaller than check out' return JsonResponse({'success':False,'error_taken':True,'error_message':error_message}) my ajax code const form = document.getElementById('post-form') form.addEventListener("submit",submitHanler); function submitHanler(e){ e.preventDefault(); $.ajax({ type:'POST', url:"my-url", data:$("#post-form").serialize(), dataType:'json', success:successFunction, error:errorFunction, }) } function successFunction(data){ // console.log(data) if(data.success='success' && data.success_msg){ form.reset(); alertify.success(data.success_msg) } } function errorFunction(request, status, error){ console.log(request.responseText) if(error.error_taken == true){ alertify.alert('warning !','message') alertify.alert(error.error_message,function(){ }); } } } i also tried this : function errorFunction(jqXHR, exception) it shows nothing as well , i tried many ways but none of them worked ! thank you in advance .. -
How to add in instance field which not in model but created in Django form?Django
I have a model Book: class Book(core.BaseModel): name = models.ForeignKey( FishingGear, verbose_name="Name", on_delete=models.PROTECT, related_name='name', ) library = models.ForeignKey( Library, models.PROTECT, related_name='library_places', verbose_name='Library', ) tag_number = models.PositiveIntegerField('Tag Number', validators=[ MinValueValidator(1), MaxValueValidator(MAX_TAG_NUMBER), ]) In forms.py I created new field : class BookChangeStatusForm(core.ModelForm): start_tag_number = forms.IntegerField(label='Start number', min_value=1) In the same forms.py I have method which takes old data and create new book!But I want that without tag_number it will create field 'start_tag_number': def save(self, commit=True): # instance = super().save(commit=False) instance = Book.objects.create( name=self.instance.name, library=self.instance.library, tag_number=self.instance.**start_tag_number**, ) return instance class Meta: model = Book fields = () But I got Error: Book object has no attribute 'start_tag_number' My views.py class BookTagView(core.UpdateView): form_class = BookChangeStatusForm template_name = 'book/book_tag.html' success_url = reverse_lazy('book:book-list') I want to take in forms.py OLD instance in UpdateView and create new instance with old data but without 'tag_number' insert 'start_tag_number'!Please help me to undestand where is mistake! -
Django change hasher algorithm for user password
I need to transfer SHA512 passwords from another db to Dango. example password - b792e3c67205a800d16fceb2dacf5b70fada6f31e905352750e093bedc95ab970c0121f7c3f2a3bfcab32f3cb8a2c0d2273ada96b082dd0fbd012dbae379dcb1 and i need use auth with this password I try this. but it doesn't work hasher.py import hashlib from django.contrib.auth.hashers import BasePasswordHasher, mask_hash from django.utils.crypto import constant_time_compare from django.utils.translation import gettext_noop as _ class CustomPasswordHasher(BasePasswordHasher): algorithm = 'sha512' def salt(self): return '' def encode(self, password, salt): hash = hashlib.sha512(password.encode()) return hash def verify(self, password, encoded): encoded_2 = self.encode(password, '') return constant_time_compare(encoded, encoded_2) def safe_summary(self, encoded): return { _('algorithm'): self.algorithm, _('hash'): mask_hash(encoded, show=3), } settings.py PASSWORD_HASHERS = [ 'restorating.hasher.CustomPasswordHasher', 'django.contrib.auth.hashers.PBKDF2PasswordHasher', 'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher', 'django.contrib.auth.hashers.Argon2PasswordHasher', 'django.contrib.auth.hashers.BCryptSHA256PasswordHasher', ] -
Achor tag begining with `?`: Django pagination: next/previous links take one to index
Following the docs on Paginating a ListView. It works great however I see the next and previous link's href starts with ?page= For my site that navigates to www.example.com/?page=2, i.e. the index, instead of staying on the same url, e.g. www.example.com/foos/?page=2. Other example's on the web have it working like the tutorial. In the template I could force it by manually inserting the {{ request.path }}, e.g.: <a href="{{ request.path }}/?page={{ page_obj.previous_page_number }}">previous</a> But I am curious why there is something broken with my setup. I tried to find the spec of how href works if it begins with a ? (is it relative to the root or the current page), but did not find it on MDN or when going down a W3C rabbit hole. -
How to remove white space within an html page
It automatically generates this "empty space" for me and I can't figure out how to remove it. This is the code I wrote in my file: HTML CODE: <div class="btn my-btnmenu btn-danger"> <i class="bi bi-bag"></i> </div> CSS CODE: .my-btnmenu { padding: .325rem .60rem; border-radius: 1.5rem; } .btn-danger { color: #fff; background-color: #dc3545; border-color: #dc3545; } and this is the image of the element inspection generated by my browser: -
why do we need get_absolute_url() in Django?
Can Anyone explain me in simple terms why do we need get_absolute_url() method in django? what is the purpose of defining get_absolute_url() in our django models? -
Pulling data from an existing database ( DJANGO // PYTHON )
I am trying to create a Web App that handles account data and prints said data to account statements and age analysis' The data will need to be fetched from an existing Sage Evolution database and printed to a web page through Django I have had a look at the Django documentation, however, the inspectdb function it gives does not give too much information on how to fetch data through a database IP address. Does anyone have a code example/tutorial on how to fetch this data through an IP address, almost with the same procedure as the below .BAT code import pymssql user = '[User ]' password = '[Password]' server = '[IP]' #username: [Username]_dev_ro #password: [Password] #database name: DB_Name databaseName = 'DB_Name' conn = pymssql.connect(server, user, password, databaseName) cursor = conn.cursor(as_dict=True) cursor.execute('SELECT top 10 * FROM [DB_Name].[dbo].[_etblGLAccountTypes]') for row in cursor: print(row) conn.close() -
drf filter by multiple ManyToManyField ids
Here is my models class Mediums(models.Model): medium_name = models.CharField(max_length=255) class Artwork(models.Model): title = models.CharField(max_length=255, blank=True, null=True) mediums = models.ManyToManyField(Mediums, blank=True, related_name="artwork") class Meta: db_table = 'artwork' I am using django-reft-framework . How can i fetch artwork by filtering multiple medium_id. I checked drf doc could not find option to filter ManyToManyField Pse take a look -
Handle permission cache in django user model
I stumbled upon a weird behaviour: I add a permission to a user object but the permission check fails. permission = Permission.objects.get_by_natural_key(app_label='myapp', codename='my_codename', model='mymodel') user.user_permissions.add(permission) user.has_permission('myapp.my_codename') # this is False! I found some posts about user permission caching here and here and the solution seems to be to completely reload the object from the database. # Request new instance of User user = get_object_or_404(pk=user_id) # Now note how the permissions have been updated user.has_perms('myapp.my_codename') # now it's True This seems really overkill for me and very un-django-like. Is there really no way to either clear the permission cache oder reload the foreign keys like you can do for an object with refresh_from_db()? Thanks in advance! Ronny -
Django - Return a list of primary keys with another attribute of the entity it's referencing
I have this FollowUser and User model below and there's a URL that gets a list of people the user is following (followees). I'd like to return a list of the followees (A User model) that has their UUID and the followees username. How can I do this in a fast and efficient way? models.py class User(AbstractBaseModel): username = models.CharField(max_length=255, unique=True) first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) email = models.EmailField(max_length=255, unique=True, primary_key=True) class FollowUser(AbstractSimpleModel): follower_id = models.ForeignKey(User, on_delete=models.CASCADE, related_name="follower_following") followee_id = models.OneToOneField(User, on_delete=models.CASCADE, related_name="followee_followed_by") views.py def get_followees(request, follower_id): try: followees = FollowUser.objects.filter(follower_id=follower_id).values_list('followee_id', flat=True) data = dict(followee_ids=list(followees)) return JsonResponse(data, status=status.HTTP_200_OK) except FollowUser.DoesNotExist: return Response(dict(error=f'follower id: {follower_id} does not exist'), status=status.HTTP_400_BAD_REQUEST) -
How to get cookies (basically I need jwt token) in Ember.js which was set in Django?
Django Rest Framework class LoginView(APIView): password = request.data['password'] user = User.objects.filter(email=request.data['email']).first() if user is None: return Response({ 'success': False, 'message': 'Username or password is invalid', 'errors': 'Username or password is invalid', }, status=status.HTTP_401_UNAUTHORIZED) if not user.check_password(password): return Response({ 'success': False, 'message': 'Username or password is invalid', 'errors': {'username-or-password': ['Username or password is invalid']}, }, status=status.HTTP_401_UNAUTHORIZED) payload = { 'id': user.id, 'iat': datetime.datetime.utcnow() } token = jwt.encode(payload, 'secret', algorithm='HS256') response = Response() response.set_cookie(key='token', value=token, httponly=True) response.data = { 'success': True, 'data': { 'token': token } } return response Here I send token in response data and also set token in cookies. How can I get/receive/retrieve token from cookies in Ember.js? -
Django admin - add extra fields using ModelForm of another model
Is it possible to add extra fields in Django Admin using a ModelForm of another model? Here my models: class ModelOne(Model): field_1_1 = models.SmallIntegerField() field_1_2 = models.ForeignKey(AnotherMyModel) field_1_3 = models.BooleanField(default=True) field_1_4 = models.ManyToManyField(to="TestModel", through="ModelTwo") class ModelTwo(Model): field_2_1 = models.ForeignKey(MyModel, on_delete=models.CASCADE) field_2_2 = models.ForeignKey(ModelOne, on_delete=models.CASCADE) field_2_3 = models.BooleanField(default=True) And the admin looks like: class ModelOneAdmin(admin.TabularInline): model = ModelOne form = forms.ModelTwoForm fields = ('field_1_1', 'field_1_2', 'field_1_3', 'field_2_1', 'field_2_3') Here the form instead: class ModelTwoForm(ModelForm): class Meta: model = ModelTwo fields = "__all__" This code raises the following error: FieldError: Unknown field(s) (field_2_1) specified for ModelOneAdmin I know that this is the case of the inline pattern, but i cannot use it becasue ModelOneAdmin is already inside a inline model (and i don't want to include extra python package to achieve this). Is there a way to fix this problem and achieve this goal? (Working with Python2.7 and Django 1.11) -
Django to allow to upload multiple files for a single record
I am having a requirement in my django application, where user will fill a form with few fields, which also includes the attachments field, my requirement is that attachments should accept multiple files , but these files should be tagged to single record, but now it's creating multiple rows in table based on number of attachments, please help. Thanks in advance -
Deploying with docker
I am new to deploying with docker. Actually, I am running my django app in my computer inside docker container, and it is running successfully on port localhost:8080. Then I pulled the code to remote server and started docker-compose up, and app is running successfully there as well. What I want to ask is that, how can I see the app with the help of ip adress of the server? For example, if the ip adress is 123.45.67.89 I think the app should be running in 123.45.67.89:8080 but it does not run there. How can I access to the app running in container in remote server? P.S. I have not used nginx, should I use it? -
IntegrityError at /create_invoice NOT NULL constraint failed: invoiceAPI_invoice.buyer_id
I am trying to create a invoice which has two foreign keys seller, and buyer and one many to many to field for items. I have create serializer for seller, buyer, and items and nested serialzer for invoice and when I am creating the invoice I am getting the error IntegrityError at /create_invoice NOT NULL constraint failed: invoiceAPI_invoice.buyer_id. data which I have passed don't have the id in that. Because I don't want to pass id through JSON, could it is possible that Django create the primary key for all the models. This is my serializers.py file class SellerSerializer(serializers.ModelSerializer): class Meta: model = Seller fields = ('__all__') class BuyerSerializer(serializers.ModelSerializer): class Meta: model = Buyer fields = ('__all__') class ItemSerializer(serializers.ModelSerializer): class Meta: model = Item fields = ('name', 'price', 'quantity', 'tax') class InvoiceSerializer(serializers.ModelSerializer): seller = SellerSerializer(many=False, read_only=True) buyer = BuyerSerializer(many=False, read_only=True) items = ItemSerializer(many=True, read_only=True) class Meta: model = Invoice fields = ('seller', 'buyer', 'items', 'date') def create(self, validated_data): seller_data = validated_data.pop('seller') buyer_data = validated_data.pop('buyer') items_data = validated_data.pop('items') seller = Seller.objects.create(**seller_data) buyer = Buyer.objects.create(**buyer_data) items = [] for item_data in items_data: items.append(Item.objects.create(**item_data)) invoice = Invoice.objects.create(seller=seller, buyer = buyer, item=items) return invoice``` This is the models.py file. **models.py** ``` class Seller(models.Model): id …