Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to use multiprocessing with Django to create thousands of objects from JSON?
I have an API endpoint on Django REST that accepts a .json file and does some computing on the data to create some "profiles", then does some error and validation checks, and finally returns the response of fail if something went wrong. This json file can be very large, which slows down the process. I can't use Django's bulk_create because the model is an inherited model and bulk create doesn't work in this case. So I am trying to use Python's multiprocessing (correct me if this is not the best approach) to create the model instances and then save in batches. It goes like this: Viewset: create() calls the utils function to loop through the objects in the json file For each object another function actually_create where the model is instantiated These objects are saved in a list (batch) The batches are saved inside a transaction block Even though I have used the same approach for other use cases, I can't get it to work and I don't know why because the traceback is not helpful and I can't set breakpoints when doing multiprocessing. Traceback: Traceback (most recent call last): File "/home/everton/.virtualenvs/venv/lib/python3.8/site-packages/django/db/backends/base/base.py", line 235, in _cursor return self._prepare_cursor(self.create_cursor(name)) File "/home/everton/.virtualenvs/venv/lib/python3.8/site-packages/django/db/backends/postgresql/base.py", … -
Python Django: How to prevent form field from clearing?
I am working with Python Django and I have this really simple form, consisting of a button and an input field. The problem is that the input field is for some reason cleared after I submit the form. So How can I prevent it from clearing? This is my python file: def index(request): # print(f"its me {os.getcwd()}") txt = request.GET.get("some_txt") if (request.GET.get('mybtn')): print(f"THIS IS THE TEXT VALUE: {txt}") else: print("Has not been clicked") return render(request, "main/index.html") This is the HTML file: <form action="#" method="get"> {% csrf_token %} <input type="text" name="some_txt"> <button type="submit" class="btn btn-primary" value="mybtn" name="mybtn">Submit</button> </form> -
Django JSONField data with integer keys
I want to save a model instance with JSON dict with integer keys, like {2: 3}. But after saving my dict turns into {"2": 3}. Is there any way to save integer keys into JSON? class MyModel(models.Model): data = JSONField("data") record = MyModel.objects.create( data={2: 3}, ) record.refresh_from_db() print(record.data) # > {"2": 3} # And I want record.data == {2: 3} Behavior same for from django.contrib.postgres.fields import JSONField and for modern from django.db.models import JSONField -
Django Complex Association Annotation including Count & Sum
I have the following Django query: queryset = queryset.values( 'ticket_owner_email_address' ).order_by( 'ticket_owner_email_address' ).annotate( ticket_quantity_total=Count('ticket_associated_quantity'), ticket_price_total=Sum('ticket_price') ).values( 'ticket_quantity_total', 'ticket_price_total', 'ticket_owner_email_address' ) which essentially does a lookup on a particular model (Booking) by the 'ticket_owner_email_address'. For each "ticket_owner_email_address" I want to a lookup back on the Booking, but returning fields "some_field" and "some_other_field". Both of these fields can be the same for multiple bookings. As I am doing the Count and Sum, if I try to add these two values to the list, they're either empty or they break the counting / summation commands. I was wondering, what is the best Query expression method for inserting these associated values for the ticket_owner_email_address? I have tried a Subquery with an OuterRef, but unfortunately that doesn't seem to be working (I essentially haven't set it up correctly). Any pro Django ORM tips would be greatly appreciated! -
Testing image uploads in Django REST Framework
I'm trying to add some tests to one of my apps in a Django project. This app includes the Image model: from django.db import models from django.utils.translation import gettext_lazy as _ from cecommerce.image_mappings import ImageAgnosticMapping from cecoresizer.fields import ResizableImageField class Image(models.Model): file = ResizableImageField( _("Image"), max_length=300, image_config=ImageAgnosticMapping, ) def __str__(self): return str(self.file) Which is then serialize using the following serializer: from django.http import Http404 from rest_framework import serializers from cecotec_apps.landings.models import ProductLanding from cecotec_apps.partner.models import Home from images.models import Image class ImageSerializer(serializers.ModelSerializer): file = serializers.ImageField() def to_representation(self, instance): return {"file": str(instance)} class Meta: model = Image exclude = ("id",) I'm trying to test the creation of an Image instance (which works when requesting the API using Imsonia) with this test: import tempfile from PIL import Image as ImageFile from django.test import tag from model_bakery import baker from rest_framework.test import APITestCase from rest_framework.authtoken.models import Token from rest_framework.reverse import reverse from cecotec_apps.landings.models import ProductLanding from cecotec_apps.partner.models import Home from images.api.serializers import ImageSerializer from images.models import Image from tests.oscar.decorator.decorator_all_methods import global_test_decorator from user.models import User @tag("e2e", "image") @global_test_decorator() class ImageTestCase(APITestCase): API_VERSION = "v1" IMAGES_QUANTITY = 20 HTTP_HOST = "localhost:8000" @classmethod def _populate(cls): cls.token = baker.make(Token) baker.make_recipe("images.image_recipe", _quantity=cls.IMAGES_QUANTITY) @classmethod def _generate_image_file(cls): with tempfile.NamedTemporaryFile(suffix="jpg") as … -
Get the value of a read-only field in Serializer validation
Error ### TypeError at /api/answer/ '>' not supported between instances of 'NoneType' and 'datetime.datetime' serializer.py ### class AnswerSerializer(serializers.ModelSerializer): class Meta: fields = ('exercise', 'answer_text', 'answer_file','datesend') read_only_fields = ('datesend',) def validate(self, attrs): date = attrs.get('datesend') ex = attrs['exercise'] dateend = Exercise.objects.get(topic=ex.topic) if date > dateend.exp_answer_date: raise serializers.ValidationError('Response time is over') return attrs -
API testing Django Test Cases api from swagger
I am new to API testing and I have b bunch of API's and want to test them so suppose I am writing test cases for an API https://apiuat.igrow.ag/api/v1/farm/ how should I perform all the GET POST PATCH function I have these APIs from swagger shown bellow I have done the other testings like model testing but I am new to this Api's testing Help me or suggest me to solve and all the test cases for this -
Django - button to remove row in sqlite3
I have a question.. I got a Django Application with a mailbox that shows a list with all the mails and their importance (priority to answer). clicking the button "Manage" goes to a view of a mail message. Now I also want to add a "delete" button that will delete this email in the list aka the row from the sqlite3 database. I have tried multiple things, because I saw this on multiple forums. My problem is that I'm quite new to Django and I don't know which answers I can combine with what I already have in my code. This is what I have now (it doesn't work as I'm still trying to find working solutions) mailindex.html: <tbody> {% for mail in mails %} {% if not mail.sent %} {% if not mail.importance.baseSeverity == "LOW" and not mail.importance.baseSeverity == "MEDIUM" %} <tr {% if mail.importance.baseSeverity == "CRITICAL" %} class="bg-red" {% endif %}{% if mail.importance.baseSeverity == "HIGH" %}class="bg-yellow" {% endif %}> <td><a href="{% url 'core:mail' mail.id %}" class="text-inherit">{{ mail.importance.baseSeverity }}</a></td> <td>{{ mail.importance.level }}</td> <td>{{ mail.importance.mails.all.count }} </td> <td> {{ mail.importance.lastModifiedDate }} </td> <td class="text-right"> <a href="{% url 'core:mail' mail.id %}" class="btn btn-secondary btn-sm">Manage</a> <form method='POST' action='delete'> <input type=hidden name='delete' value='{{mail.id}}'> … -
How to I call admin view funtions from api views in Django?
I want to run a method in my Admin View (admin.py): class AbstractTestAdmin(NGModelAdmin) def display_message(self, request): messages.warning(request, "message") from my views/api.py page: class InvoiceList(NGBaseView, generics.ListCreateAPIView) def create(self, request, pk=None) AbstractTestAdmin.display_message(request) How can I make views/api.py run the methods in admin? -
Best practices to build multi room (video and chat) with permissions in django and flutter
So past couple of days I have been looking into building multi room, where users are able to either chat, spectate or make a video calls dependent on their permissions in Django(DRF) backend and flutter frontend. For Django I found that I need to use, channels and web socket. Alternatively for flutter I found about stream platform (getstream.io) that helps to do that. At the end of the day I got overwhelmed and confused. I do not know where to begin, or what is the best approach to what I am looking for. I am very comfortable with working DRF and using dryrestpermissions to permit users some actions, however I am not sure how to integrate that with the multi room, websockets and then access that through flutter... Either way please give me your ideas about this, best practices and where should I begin? Thank you! -
AttributeError at /create_image/ 'tuple' object has no attribute '_committed'
I am trying to create an object as follows, throws the above error def create_image(request): item = Category( name='Images10', slug='img-10', ) item.image = ('abc.png', File(open('/static/assets/images/abc.png', 'rb'))) item.save() return HttpResponse('success') -
How to use multiple models inside one viewset/serializer?
I have 2 items inside my system that I have to use.But I am trying to develop a system which i have to get them inside one view and order them by "timestamp". class CalendarEventSerializer(serializers.ModelSerializer): class Meta: model = CalendarEvent fields = ("id","author",...) class CalendarItemSerializer(serializers.ModelSerializer): class Meta: model = CalendarItem fields = ("id","author",...) I use ViewSet to regulate my models to paginate and filter them. class CalendarItemViewSet(viewsets.ModelViewSet): permission_classes = (IsAuthenticated,) queryset = CalendarItem.objects.all().order_by('-timestamp') serializer_class = CalendarItemSerializer filter_backends = [UserFilterBackend,DjangoFilterBackend] pagination_class = StandardResultsSetPagination filterset_fields = ['author'] class CalendarEventViewSet(viewsets.ModelViewSet): permission_classes = (IsAuthenticated,) queryset = CalendarEvent.objects.all().order_by('-timestamp') serializer_class = CalendarEventSerializer filter_backends = [UserFilterBackend,DjangoFilterBackend] pagination_class = StandardResultsSetPagination filterset_fields = ['author'] How can I use this system to merge 2 or models into one viewset or serializer like this? Note:I know there are some answers for similar kind of questions in stackoverflow but my code structure is but different, I want to get some viewpoints -
Error while excel exporting using Django xlwt
I am trying to export information to an Excel file from my web application using the xlwt library, but the following error appears in urls.py TypeError: export_excel_av_items() missing 1 required positional argument: 'request' models.py: class Stock(models.Model): id = models.AutoField(primary_key=True) name = models.CharField(max_length=30, unique=True) quantity = models.IntegerField(default=1) is_deleted = models.BooleanField(default=False) def __str__(self): return self.name views.py: def export_excel_av_items(request): response = HttpResponse(content_type='application/ms-excel') response['Content-Disposition'] = 'attachment; filename="available-items.xls"' wb = xlwt.Workbook(encoding='utf-8') ws = wb.add_sheet('Available Items') row_num = 0 font_style = xlwt.XFStyle() font_style.font.bold = True columns= ['Name', 'Qty'] for col_num in range(len(columns)): ws.write(row_num, col_num, columns[col_num], font_style) font_style = xlwt.XFStyle() rows = Stock.objects.all().values_list( 'name', 'quantity') for row in rows: row_num += 1 for col_num in range(len(row)): ws.write(row_num, col_num, str(row[col_num]), font_style) wb.save(response) return response urls.py: path('', views.StockListView.as_view(), name='inventory'), path('new', views.StockCreateView.as_view(), name='new-stock'), path('stock/<pk>/edit', views.StockUpdateView.as_view(), name='edit-stock'), path('stock/<pk>/delete', views.StockDeleteView.as_view(), name='delete-stock'), path('inventory/export-excel', views.export_excel_av_items(), name='export-excel'), -
Load new pages via Ajax without refreshing the whole nav bar Django
I'm new to Django and I'm building my own website. I have written a base.html page that contains the main body, CSS, js, fonts, and the navbar that all the pages of my site will have. The navbar is on another HTML file, navbar.html Now, as soon as the CSS/js/fonts/navbar included in the base.html will be loaded on every page and won't change, is there a way with Ajax to load only the actual content of the page that change seamless without seeing the whole page refresh? For instance, the part of the page that visually changes everytime is inside a <section class="home"></section> tag -
Django - pass data from basic HTML form into model
I have created a simple app in Django using some tutorials, but it became very usefull and also scaled a lot. What i have is a basic HTML table - prefilled with data from Model A. At the end of the table there is Submit button, which just use some javascript to prompt a print window (to save the table[page] as PDF basically) What i would like to do, is that when i press the Button to print, i would also pass some data from the table for example ModelA name and adress into a ModelB - which would serve as an statistic. However i have used for this a simple tutorial, and therefore to display the table i used "DetailView" in views.py. This is my views.py file class PrinterDetailView(DetailView): model = Printer template_name = 'printer_detail.html' Is it possible to achieve this without redoing the whole site? i Found some people searching for simillar answers, but from that it seemed like i would have to redone whole app.. Thanks for your input! -
Sum total value for all items
I have a query set that contains a number of transactions for a particular product. transactions = Transaction.objects.filter(product__product_name_id = item.id) Within this queryset contains a number of fields. product amount price transaction_date I need to calculate the totals of the values in the amount fields. The current query set is returning 2 amounts from 2 `transactions' Would it be sensible to loop through each transaction and add it to a list of something? or is there a better way? list = [] for item in transactions: amount = item.amount list.append(amount) Thanks -
how to exclude fields that are in one depth level with DRF?
how to exclude fields that are in one depth level in django rest framework? I want to remove id field in testPieces and resultTestsList. this is my API: my serializer: class Test_Serializer(serializers.ModelSerializer): class Meta: model = Test_1 exclude = ["id"] depth = 3 my models: class Test_pieces_1_question(models.Model): question = models.CharField(max_length=3000) yesScore = models.IntegerField() noScore = models.IntegerField() class Result_tests_list_1(models.Model): text = models.CharField(max_length=3000) score = models.IntegerField() unicode = models.CharField(max_length=500) class Test_1(models.Model): name = models.TextField(max_length=3000) category = models.CharField(max_length=500) time = models.CharField(max_length=500) testPieces = models.ManyToManyField(Test_pieces_1_question) resultTestsList = models.ManyToManyField(Result_tests_list_1) my view: class Whole_test(mixins.ListModelMixin, generics.GenericAPIView): queryset = Test_1.objects.all() serializer_class = Test_Serializer filter_backends = [DjangoFilterBackend] def get(self, request, *args, **kwargs): return self.list(request, *args, **kwargs) thanks! -
DJANGO API - Background API
I have a requirement which involves downloading a large csv, checking for some information, and mailing somebody. And I can't write a sync API. I was looking for ways to write a API that returns a response and starts running this function server side. Can you suggest me a way to do it in DJANGO? Thanks. -
confluent-kafka-python save to database
I have a Djnago project and I'm trying to save objects to database inside of kafka consumer. I get records from kafka like this: def sync_product_from_kafka_consumer(): topic = PRODUCT_KAFKA_TOPIC_NAME schema_str = """*some_json*""" json_deserializer = JSONDeserializer(schema_str, from_dict=dict_to_product) string_deserializer = StringDeserializer('utf_8') consumer_conf = {'bootstrap.servers': KAFKA_SERVERS, 'key.deserializer': string_deserializer, 'value.deserializer': json_deserializer, 'group.id': 'myproject.dev.group-k8s-dev', 'auto.offset.reset': "earliest"} consumer = DeserializingConsumer(consumer_conf) consumer.subscribe([topic]) while True: msg = consumer.poll(1.0) if msg is None: continue product = msg.value() if product is not None: save_product(product) # sync_to_async(save_product)(product) consumer.close() def save_product(product): defaults = { *some_dict* } Product.objects.update_or_create(id=product.id, defaults=defaults) But after execute this code I get exception: django.core.exceptions.SynchronousOnlyOperation: You cannot call this from an async context - use a thread or sync_to_async. How do I fix it? Thanks for any help! -
The view Project.views.profile didn't return an HttpResponse object. It returned None instead
def profile(request): if request.user.is_authenticated: pass else: return render(request, 'profile.html') I have this code -
How do I get only non deleted items from many to many field where both the table have soft deletion on them in django?
I have the following tables class B(mixins.BaseModel, OriginPublicIdMixin, mixins.SoftDeletionModel): # has soft deletion with soft deletion manager class C(mixins.BaseModel, mixins.SoftDeletionModel: # has soft deletion, with soft deletion manager class A(mixins.BaseModel, mixins.SoftDeletionModel): d = models.ManyToManyField("B", related_name="a", through="C") Now when I do a = A.filter(pk='some_id') result = a.d.all() The result contains entries for which B is non-deleted and C is deleted/non-deleted. I want to get only those entries, for which both B and C are non deleted. In what way can I do that without having to filter again by C is not None I have already tried using use_for_related_fields = True in the soft deletion manager, no luck -
I can connect an application to 2 databases?
I have a web application in Python django. I need to import users and display data about them from another database, from another existing application. All I need is the user to be able to login and display information about them. What solutions are? -
How to display messages for api requests not admin in Django?
I want to have messages displayed on my admin page when a POST endpoint is used in my API. I am using the official messages framework from Django and have set them up accordingly: https://docs.djangoproject.com/en/4.0/ref/contrib/messages/ It does work for when I send requests on my Admin page (when I click a button for example). But it wont show when a POST function is runed. I see that the messages are saved somewhere but they are never showed. I tried both messages.warning(request, "message") and messages.add_message(request, constants.WARNING, "message") I assume its because I have a HTML template for admin. How can I join them, so that they display messages from both Admin and API? -
Why does timezone.now() + timezone.timedelta(days=1) at 12-1 AM return the same day?
Running timezone.now() + timezone.timedelta(days=1) at 12:30 AM returns datetime.datetime(2022, 2, 7, 23, 30, 00, 000000, tzinfo=<UTC>) which is practically still the same day. -
how to pass one function context variable to another function in django , or what is the good method instead of session for that?
views.py def contest(request): context = {} all_contest = Contest.objects.all() context["contest"] = all_contest return render(request,"contest.html",context) def contest_candidates(request,id): context = {} all_candidates_ = Contest_Candidates.objects.filter(contest_id = id) high_like = 0 winner_ = "" for i in all_candidates_: if i.likes_count > high_like: high_like = high_like + i.likes_count winner_ = i.owner.name context["cadidates"] = all_candidates_ return render(request,"view_contest_candidates.html",context) urls.py path("contest",views.contest, name="contest"), path("candidates/<id>",views.view_contest_candidates, name="candidates"), models.py class Contest(models.Model): title = models.CharField(max_length=50) class Contest_Candidates(models.Model): image = models.FileField( upload_to="contest_candidates/",) owner = models.ForeignKey(CustomUser, on_delete=models.CASCADE) contest = models.ForeignKey(Contest, on_delete=models.CASCADE,related_name='candidates') @property def likes_count(self): return self.likes.all().count() class CandidateLikes(models.Model): like = models.CharField(max_length=10) user = models.ForeignKey(CustomUser,on_delete=models.CASCADE,related_name='candidate_likes') contest_candidates = models.ForeignKey(Contest_Candidates, on_delete=models.CASCADE, related_name='likes') how to pass the winner variable in to contest function ? i need to show that in contest.html if it's not possible, which is the best method . want to show every contest winner in contest.html