Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
django.db.utils.ProgrammingError: column accounts_account.username does not exist
I am getting this error after I was customizing my Customer User Model. and now I get this error when I try to login into the admin panel and when I try to create a superuser. I tried to drop the postgresql database called skincareryland... but am getting an error on the password login... when I try to change the password I get this error.. ERROR: role "postgres" does not exist I also tried going through these steps from an overstack post, but not having any luck fixing the problem... Comment out 'django.contrib.admin' from INSTALLED_APPS in settings.py and comment out # path('admin/', admin.site.urls) in urls.py execute commands: Python manage.py makemigrations appname Python manage.py migrate appname add 'django.contrib.admin' to INSTALLED_APPS in settings.py file and path('admin/', admin.site.urls) in urls.py execute commands again: Python manage.py makemigrations appname Python manage.py migrate appname from django.db import models from django.contrib.auth.models import AbstractBaseUser, BaseUserManager class MyAccountManager(BaseUserManager): def create_user(self, first_name, last_name, username, email, password=None): if not email: raise ValueError('User must have an email address') if not username: raise ValueError('User must have an username') user = self.model( email=self.normalize_email(email), username=username, first_name=first_name, last_name=last_name, ) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, first_name, last_name, email, username, password): user = self.create_user( email=self.normalize_email(email), username=username, password=password, … -
DoesNotExist at / AllocationDetail matching query does not exist
I know there are a number of questions already on stack overflow related to this issue and I have read them all, but still I am having no success with this issue. I am hoping somebody can help me out with this. C:\Users\Ravokid\Desktop\E-Fund_2\users\views.py, line 73, in home 'money_per_ward' : get_money_per_ward(), C:\Users\Ravokid\Desktop\E-Fund_2\accounting\views.py, line 17, in get_money_per_ward obj = accounting_models.AllocationDetail.objects.all().get(financial_year__start_date=datetime.date.today().year, financial_year__end_date=datetime.date.today().year+1) -
How do I used the map in django
I need for your helps plz.. I need to show heatmap plot to the page by Django in the views heat = sns.heatmap(mcor, annot=True) heat = plt.show() in the page Heat Map{{heat|safe}} but the reslut show the map out the page (new tap) as like image what's problem ? -
django loginview csrftoken fobidden error
I'd like to ask you a question about the Loginview of Django. I'm using the Loginview as it is. There's a problem, but I'll open two browser tabs and leave them open as login pages (the same browser ) When I log in first from one browser tab and log in from another browser tab (both are the same browser), the csrf_token error appears, which is 403 Forbidden error! The way I want to do it is to log in without errors even if I log in from another browser tab if I'm already logged in from one browser tab. Also, I'm trying to solve it using Ajax, but I'm not familiar with it, so I'm having a hard time solving the problem. How should I solve this? -
REST framework's FileField: How can I force using TemporaryUploadedFile
I want to extract and process text from a text document uploaded by the user without actually saving the document. I use Django's REST framework and I want this to happen in the serializer. I get the document from a FileField. Because the text document is likely to be small, it will be wrapped into InMemoryUploadedFile automatically. However, text-extraction modules (e.g. docx2python) need to open files from paths, not from memory. How can I turn an InMemoryUploadedFile into a TemporaryUploadedFile, so I can get a path I use? Or how do I force a particular field (without changing the app settings) to always wrap a file in TemporaryUploadedFile? -
CSRF validation does not work on Django While entegrating with reactjs
even after adding CSRF_TRUSTED_ORIGINS there is an error while post request Origin checking failed - (website name) does not match any trusted origins. -
Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view in Django RestFramework
I'm trying to return a response after the execution of loop but I'm getting an error as AssertionError at Data/CurrentRunningActivityForAudit/10 Expected a `Response`, `HttpResponse` or `HttpStreamingResponse` to be returned from the view, but received a `<class 'NoneType'>` when I added another return response outside the loop it shows the empty array i.e.,.[ ] it's returning empty response ` views.py: def CurrentRunningActivity(UserID): cursor = connection.cursor() cursor.execute('EXEC [dbo].[sp_GetCurrentRunningActivityAudit] @UserId=%s',(UserID,)) result_set = cursor.fetchall() IsActive = 'true' for row in result_set: data = [] data.append({ 'TaskId':row[0], 'TaskName' : row[1], 'Source' : row[2], 'Requester' : row[3], 'type' : row[4], 'IsActive':IsActive, }) return Response(data[0], status=status.HTTP_200_OK) when I move the return response outside the loop it shows as local variable 'data' referenced before assignment -
Filter into Foreign Keys on Django-Rest Datatables
I have a datatable for Order model which contains some fields from User model and is implemented using serializer as following serializers.py # customer order detail serializer class CustomerOrderSerializer(serializers.ModelSerializer): first_name = serializers.ReadOnlyField(source='user.first_name') last_name = serializers.ReadOnlyField(source='user.last_name') def __init__(self, *args, **kwargs): remove_fields = kwargs.pop('remove_fields', None) super(CustomerOrderSerializer, self).__init__(*args, **kwargs) if remove_fields: for field_name in remove_fields: self.fields.pop(field_name) class Meta: model = Order fields = ['id', 'amount', 'order_status', 'created', 'first_name', 'last_name'] read_only_fields = ['issue_date', 'first_name', 'last_name'] In views.py class OrderList(generics.ListAPIView): queryset = Order.objects.order_by('-created') serializer_class = CustomerOrderSerializer I am successfully able to get first_name & last_name fields as in datatable format as shown However, my requirements is to filter/search in datatable on fields first_name & last_name which are from different model User when I try to filter on those fields I get Following error: Cannot resolve keyword 'first_name' into field. Choices are: ..., .., ..., .. Can anyone help me understand, How we can filter on foreign key fields in django-rest datatables -
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!