Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Custom user view/edit permissions related to same table for different users in django admin panel
i am making an survey/polling app using django rest framework. I have a polls table, and personality's table (with a django country Field in it) related to poll table. I am using rest-auth for user model (not related to personality table). Now i need set some permissions in django admin panel that a certain admin can only view a poll for certain country only. like for example, if there is an admin for US, then he can view/add/change only the polls related to US only. is there any way to set such permissions. NOTE: these permissions are needed to be set for admin panel -
401 (Unauthorized) error while using AutoDesk APIs
I have a Django application and I'm working with .stl, .stp and .igs files in it. I have to display these files in a HTML template and I'm trying to use the AutoDesk APIs for that. I have created an account first and authenticated but I'm getting trouble with displaying the images. here is the view which it contains authentication and uploading image to the bucket: def viewer(request): req = { 'client_id' : CLIENT_ID, 'client_secret': CLIENT_SECRET, 'grant_type' : 'client_credentials','scope':'code:all bucket:create bucket:read data:read data:write'} resp = requests.post(BASE_URL+'/authentication/v1/authenticate', req) if resp.status_code == 200: TOKEN = resp.json()['access_token'] else: print('Get Token Failed! status = {0} ; message = {1}'.format(resp.status_code,resp.text)) TOKEN = "" filepath = "C:/Users/imgea/desktop/" filename = '55.stl' my_file = Path(filepath + filename) #check package file (*.zip) exists if my_file.is_file(): filesize = os.path.getsize(filepath + filename) # encoding the file name if sys.version_info[0] < 3: encodedfilename= urllib.pathname2url(filename) else: encodedfilename= urllib.parse.quote(filename) resp = requests.put(BASE_URL + '/oss/v2/buckets/'+'bucket'+'/objects/'+ encodedfilename, headers={'Authorization': TOKEN,'Content-Type' : 'application/octet-stream','Content-Length' : str(filesize)},data= open(filepath+filename, 'rb')) if resp.status_code == 200: urn = resp.json()['objectId'] else: print('Upload File to Bucket of Data Management Failed! status ={0} ; message = {1}'.format(resp.status_code,resp.text )) urn = "none" else: print(' ' + filename + ' does not exist') urn = "none" context = … -
TemplateDoesNotExist error in django with celery
I am learning to use celery with django through this tutorial. In this tutorial, the person is developing a webscraping tool with django and celery. I am trying to follow the tutorial but I am facing the following error message TemplateDoesNotExist at / home.html, scraping/products_list.html this is how my files are arranged . ├── celerybeat-schedule.db ├── celerydjangotutorial │ ├── __init__.py │ ├── asgi.py │ ├── celery.py │ ├── settings.py │ ├── templates │ │ ├── base.html │ │ └── home.html │ ├── urls.py │ ├── views.py │ └── wsgi.py ├── db.sqlite3 ├── manage.py └── scraping ├── __init__.py ├── admin.py ├── apps.py ├── migrations │ ├── 0001_initial.py │ ├── 0002_auto_20200827_0735.py │ ├── __init__.py │ └── __pycache__ │ ├── 0001_initial.cpython-38.pyc │ ├── 0002_auto_20200827_0735.cpython-38.pyc │ └── __init__.cpython-38.pyc ├── models.py ├── tasks.py ├── tests.py └── views.py where celerydjangotutorial is the django project and scraping is a django application. In the tutorial, the person places the templates into the project directory. he also creates a views file in the project directory. I followed him as he does in the tutorial. here is my code. celerydjangotutorial/urls.py from django.contrib import admin from django.urls import path, include from .views import HomePageView urlpatterns = [ path('', HomePageView.as_view(), name='home'), path('admin/', … -
Django models in multithreaded scrapers
I am using Django models to store the data scraped. The scrapers are multithreaded due to slow response from website and interval of scraping is like every 2 or 3 or 10 minutes. The problem is that the database connections are left IDLE and after sometime I am getting OperationalError. After googling I used connections.close() from django.db.connections at the end of each function wherever database interaction is done. It worked but for a few days and then in one of the scraper again I got OperationalError saying connection was closed by admin. I scheduled the scrapers using a management command and used schedule package. I am using PostgreSQL as my database. Please tell me a solution to this. -
Django ORM sync_to_async error FATAL: remaining connection slots are reserved for non-replication superuser connections
I am trying to implement Asynchronous support(https://docs.djangoproject.com/en/3.1/topics/async/) for Django ORM to write many records in Database(Postgres). I am getting the ERROR:root:FATAL: remaining connection slots are reserved for non-replication superuser connections I am creating coroutines add adding them to running the asyincio loop # Helper Class class ModelsHelper: @staticmethod @sync_to_async def __handle_resource_data(data): // Some calculation to create kwargs dict return Resource.objects.get_or_create(**kwargs) async def store_data(metric): // some calculation to get data return await ModelsHelper.__handle_resource_data(data) # Main File def get_event_loop(): loop = None try: loop = asyncio.get_event_loop() except Exception as e: print(" New Event Loop ".center(50, '*')) loop = asyncio.new_event_loop() asyncio.set_event_loop(loop) return loop loop = get_event_loop() future = asyncio.ensure_future( asyncio.gather(*[ModelsHelper.store_data(metric) for metric in metrics]), loop=loop ) loop.run_until_complete(future) -
Fetch all record from model and it's related model
Say for example I have two table, Author and Books. class Author (models.Model ): name = models.CharField(...) class Book (models.Model ): author_id = models.ForeignKey('Author') book_name = models.CharField(...) Author table will contain the entry of authors irrespective of if they have written any book. And also one author can have multiples books. So I want to query all the authors name with their respective written books ( containing book_name ) even if they haven't written any book. -
Using a variable to for loop using django
I'm very new to python and django framework. I'm facing a minor issue of not being able to assign a variable to my for loop. In my html file there are buttons and a list (rendered using a loop). for ex: Buttons <li><a class="layer-item" href="#" onclick="selectLayer('base')" title="Base layer">Base layer</a></li> <li><a class="layer-item" href="#" onclick="selectLayer('soil')" title="Soil layers">Soil layers</a></li> Inside script tags i'm updating the variable value as <script> var layerType = ""; function selectLayer(layer){ layerType = layer; } </script> Also i have a loop like following in the same html file {% for layer in base %} <div class="col-4"> <span class="base-layer-title d-block">{{layer.title}}</span> </div> {% endfor %} Here i want to replace base according the button clicked. For ex: <a class="layer-item" href="#" onclick="selectLayer('soil')" title="Soil layers">Soil layers</a> Clicking on the above button should make the for loop as {% for layer in soil %} <div class="col-4"> <span class="base-layer-title d-block">{{layer.title}}</span> </div> {% endfor %} From i read you can do something like this to assign a value to a variable. {% with name="World" %} But not sure how to implement it in my issue. Any help is appreciated -
Handle HTTP Status Codes using @json_response decorator
Im looking for a solution to handle HTTP status codes using @json_response decorator from Django. Actually I use the view in this form: @json_response def any_view(request): return {'this will be': 'JSON'} But this always returns the 200 OK status code. It's possible to handle this status code? Thanks in advance. PD: I know how to use the return JsonResponse({'this will be': 'JSON'}, status=200) solution. But this not uses the decorator. -
Django - How to call a view function from an OnChange event in html template?
Django Newbie here. So basically when a user fills the first input field (eg. his name), I want to populate other fields automatically from data stored in database(eg. his age, height, weight etc.). For this, my logic is to use onchange event listener on the name input field and when it is triggered, the name value should be passed to a view function. The database is queried for the required results. Finally the data from query is displayed in html template fields. Hence, I would like to know how to call view functions based on onChange event. -
TypeError: Direct assignment to the reverse side of a related set is prohibited. Use thumbnails.set() instead
I'm trying to get my serializer to work but I've run into this error: TypeError: Direct assignment to the reverse side of a related set is prohibited. Use thumbnails.set() instead. And I'm not sure how to fix it. I've tried googling the problem but only found this docs which I have no idea of how to implement in my code: class YoutubeSnippetSerializer(serializers.ModelSerializer): thumbnails = YoutubeThumbnailSerializer(many=True) class Meta: model = YoutubeSnippet fields = ['publishedAt', 'channelId', 'title', 'thumbnails', 'channelTitle', 'liveBroadcastContent', 'publishTime'] def create(self, validated_data): thumb_data = validated_data.pop("thumbnails") snippet = YoutubeSnippet.objects.create(**validated_data) YoutubeThumbnails.objects.create(snippet=snippet, size="default", **thumb_data.pop("default")) YoutubeThumbnails.objects.create(snippet=snippet, size="medium", **thumb_data.pop("medium")) YoutubeThumbnails.objects.create(snippet=snippet, size="high", **thumb_data.pop("high")) return snippet The problem arises when the thumbnails variable is added. (thumbnails = YoutubeThumbnailSerializer(many=True) & 'thumbnails') Code: class YoutubeVideoSerializer(serializers.ModelSerializer): youtubeId = YoutubeIdSerializer(many=True) snippet = YoutubeSnippetSerializer(many=True) class Meta: model = YoutubeVideo fields = ['kind', 'etag', 'youtubeId', 'snippet'] def create(self, validated_data): id_data = validated_data.pop("youtubeId") snippet_data = validated_data.pop("snippet") video = YoutubeVideo.objects.create(**validated_data) for data in id_data: YoutubeId.objects.create(youtubeVideo=video, **data) for data in snippet_data: YoutubeSnippet.objects.create(youtubeVideo=video, **data) return video class YoutubeThumbnailSerializer(serializers.ModelSerializer): class Meta: model = YoutubeThumbnails fields = ['url', 'width', 'height'] Models: class YoutubeVideo(models.Model): kind = models.CharField(max_length=255, null=True) etag = models.CharField(max_length=255, null=True) class YoutubeSnippet(models.Model): publishedAt = models.DateTimeField(null=True) channelId = models.CharField(max_length=255, null=True) title = models.CharField(max_length=1084, null=True) channelTitle = models.CharField(max_length=255, null=True) liveBroadcastContent … -
I done a todo list using Django
I done a todo list using Django. My problem is after deploy my to app to heroku. When I change my "Debug=true" to "Debug=false" my app wont working. Tasks that are written in my mobile are also visible in my friend mobile as well. Any help. -
Django Rest Framework endpoint for total current visitor online
I am trying to implement total current visitor online with django rest framework. I found many tutorial and those for django and html server side rendering. But i am using django rest framework and reactjs in frontend. I am trying to show total current visitor online. What is the best possible way to show this? -
I have a problem in testing the django model values attached to signals
Scanario: #tests.py class TestModelValues(TestClass): @classmethod def setUpTestData(cls): cls.a = modelA.objects.create(...) #also creating its respective foreign key instances cls.b = modelB.objects.create( fk_attribute = cls.a, attr_x = 10, #involved in the problem ) #this is optional info abt my problem, included just in case :D def test_modelC_instance_is_created_on_creation_of_modelB_instances(self): #post_save signal in signals.py to create modelC instances modelB_count = modelB.objects.count() modelC_count = modelC.objects.count() self.assertEquals(modelB_count,modelC_count) #and a for loop to check if the instances of modelB in model C exists and saved properly #this workes fine #the problem def test_modelC_is_updated_on_updation_of_modelB_instances(self): #pre_save signal in signals.py to update it self.b.attr_x = 50 self.b.save() print(self.b.attr_x) #prints 50 correctly but #when i print the updated field in modelC below print(modelC.objects.get(OneToOneFieldToModelB__pk = self.b.pk).updated_attr) #This still prints 10 (the old value )while the print function in signals.py clearly changed it to 50 #THIS IS THE PROBLEM #assert line #signals.py #i have just shown the pre_save method here @receiver(pre_save, sender=modelB) def update_modelC_field(sender, instance, **kwargs): if instance.pk!=None: obj = modelC.objects.get(OneToOneFieldToModelB__pk=instance.pk) #code for updation obj.save() print(obj.updated_attr) #This prints 50 currently I tried using the refresh_from_db method, but that also showed no change. I am really new to testing in Django. I did not know how to create mock things and all. That's why I … -
Getting one or zero elements of QuerySet in Django?
In Django 3.1, suppose I have some model M, and I have a QuerySet over M that I expect has either one or zero elements. How do I branch on whether it is one or zero elements (throwing an exception if it has two or more), and in the branch with one element, how do I get the one M object: try: my_query_set = M.objects.filter(some_filter_expr) if ???: m = ??? # the one M object else: on_zero_objects() except ???: more_than_one_object() -
Django rest framework CORS( Cross Origin Resource Sharing) is not working
I have done token authentication for the url 'localhost:8000/api/posts' and according to django-cors-headers library I have also changed the settings.py file. Here is my settings.py file, INSTALLED_APPS = [ 'corsheaders', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', 'rest_framework', 'rest_framework.authtoken' ] Here is my middleware settings, MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', ] Here are my other settings, CORS_ALLOW_ALL_ORIGINS = False CORS_ALLOW_CREDENTIALS = True CORS_ALLOWED_ORIGINS = ["http://127.0.0.1:4000"] Here I have given access only for "http://127.0.0.1:4000" This is my client django project views file which is hosted on "http://127.0.0.1:3000" import requests from django.http import HttpResponse def get_token(): url = "http://127.0.0.1:8000/api/authentication/" response = requests.post(url, data={'username':'thomas','password':'thomas1234567890'}) token=response.json() return token token=get_token() def get_details(): url = "http://127.0.0.1:8000/api/posts/" header = {"Authorization": "Token {}".format(token['token'])} response = requests.get(url, headers = header) return response.text def homepage(request): x= get_details() return HttpResponse(x) Now even though I am requesting for the data from other domain which is not mentioned on django cors origin whitelist, I am able to fetch the data without any error, I am not able to restrict other domains for accessing the data. Can anyone please help me in solving this issue. -
Need help on Matching Engine
I am trying to simulate stock trading app as part of an assignment in django. I found one python algorithm for order matching but I am not sure how can I use this in django model. from django.db import models from threading import Thread from collections import deque class Order(models.Model): # order_type = models.CharField(max_length=255, blank=True, null=True) # order_side = models.CharField(max_length=255, blank=True, null=True) # price = models.DecimalField(max_digits=10, decimal_places=2,default=0.00) # quantity = models.IntegerField(default=0) def __str__(self): return self.order_type def __init__(self, order_type, side, price, quantity): self.type = order_type self.side = side.lower() self.price = price self.quantity = quantity class Trade(models.Model): pass def __init__(self, price, quantity): self.price = price self.quantity = quantity class OrderBook: def __init__(self, bids=[], asks=[]): self.bids = sortedcontainers.SortedList(bids, key = lambda order: -order.price) self.asks = sortedcontainers.SortedList(asks, key = lambda order: order.price) def __len__(self): return len(self.bids) + len(self.asks) def add(self, order): if order.direction == 'buy': self.bids.insert(self.bids.bisect_right(order), order) elif order.direction == 'sell': self.asks.insert(self.asks.bisect_right(order), order) def remove(self, order): if order.direction == 'buy': self.bids.remove(order) elif order.direction == 'sell': self.asks.remove(order) def plot(self): fig = plt.figure(figsize=(10,5)) ax = fig.add_subplot(111) ax.set_title("Limit Order Book") ax.set_xlabel('Price') ax.set_ylabel('Quantity') # Cumulative bid volume bidvalues = [0] for i in range(len(self.bids)): bidvalues.append(sum([self.bids[x].quantity for x in range(i+1)])) bidvalues.append(sum([bid.quantity for bid in self.bids])) bidvalues.sort() # Cumulative ask … -
how can i get total price from ManyToManyField in django?
how can i calculate each vital price with ManyToManyField. if you have any other option please tell me. if i add some vitals in patient report then how can i get the total price of these vitals? Patient from django.db import models from vital.models import Vital class Patient(models.Model): name = models.CharField(max_length=50) father = models.CharField(max_length=50) age = models.CharField(max_length=2) sex = models.CharField(choices=GENDER_CHOICES, max_length=6) phone = models.CharField(max_length=11, blank=True) vitals = models.ManyToManyField(Vital) def __str__(self): return f'{self.name} {self.father}' Vital from django.db import models class Vital(models.Model): name = models.CharField(max_length=100, help_text='Test name') price = models.IntegerField(default=0) def __str__(self): return self.name -
When does Django commit transactions in management commands/outside views?
I have a Django app managing some ETL jobs. The ETL jobs live in a subdirectory of my main app called /scripts. Each ETL job has its own script with a single run() function that contains the ETL logic. I use the django-extensions run_script command to execute these scripts manually. Otherwise, they are run on a schedule using APScheduler. The jobs collect data from a source database, does some transformations, then are created as models in the Django database. The source database is normalized and relational, so I am often creating a model instance that will be referenced in the same script later as a foreign key. For example, suppose I have a City model and a Street model that are related via foreign-key: class City(Model): name = models.CharField(max_length=100) class Street(Model): name = models.CharField(max_length=100) city = models.ForeignKey(City, on_delete=CASCADE) In the source database, it's frequent that a new City and its related Streets are created on the same day, and thus, are also both created for the first time in the Django database. So effectively, this can happen in the same script executed by APScheduler or run_script: new_city = City(name='Boston') new_city.save() ... ... existing_city = City.objects.get(name='Boston') # DoesNotExist new_street = Street(name='Mass … -
Can someone clarify these tasks for Django app?
I've been learning django since 6 days and I got a task with this TODO list which I cannot clearly understand. So, can anybody help me with understanding and maybe expanding these? I understand the generic views tasks etc. but I don't know what expenses.Category for example is. Is it a file which I have to create? Is it a model? I'am a total newbie who's still learning whole structure of projects in Django and else, that's why I would absolutely appreciate your help, It would help me a lot. my project structure | app screen Allow searching by date range in expenses.ExpenseList. Allow searching by multiple categories in expenses.ExpenseList. In expenses.ExpenseList add sorting by category and date (ascending and descending) In expenses.ExpenseList add grouping by category name (ascending and descending) In expenses.ExpenseList add total amount spent. Add list view for expenses.Category. Add number of expenses per category per row in expenses.CategoryList. Add create view for expenses.Category. Add update view for expenses.Category. Add message for create/update in expenses.Category if name already exists Add delete view for expenses.Category. In expenses.CategoryDelete add total count of expenses that will be deleted. Add detail view for expenses.Category with total summary per year-month. Add option … -
filtering using a OneToOneFields returning empty query set
I have a User model with phone field as pk. The a Transaction model with recharge_number as a OneToOne field. I can not filter data from Transaction model using the phone number even though i do a print and the number is received from the request successfully. Below are my code: Models.py - User class User(AbstractBaseUser): phone = models.CharField(max_length = 15, unique = True) first_name = models.CharField(max_length = 50, blank = False, null = False) last_name = models.CharField(max_length = 50, blank = False, null = False) Models.py - Transaction class Transaction(models.Model, Main): recharge_number = models.OneToOneField(User, on_delete=models.CASCADE, related_name='recharge_number') network = models.CharField(choices=Main.NETWORK, max_length=10) recharge_amount = models.DecimalField(decimal_places=2, max_digits=10000) serializers.py - TransactionDetailSerializer class TransactionDetailSerializer(serializers.ModelSerializer): model = Transaction fields = ( 'recharge_number', 'network', 'recharge_amount' ) views.py - Transactions class Transactions(RetrieveAPIView): def get(self, request, *args, **kwargs): phone = request.data.get('recharge_number', False) print(phone) transaction = Transaction.objects.filter(recharge_number=phone).order_by("-timestamp") serializer = TransactionDetailSerializer(transaction, many=True) return Response(serializer.data, status=status.HTTP_200_OK) Results gets an empty Query - <QuerySet []>. Should not be as they are lots on transaction records with the phone number Thanks -
Hosted django app not showing images but it wasshown on local server...i hosted on heroku
It shows card image cap instead of displaying image enter image description here -
url has more than expected django mediaFile in django
I have a model Photo class Photo(models.Model): uuid = models.UUIDField(primary_key=True, default=uuid.uuid4) created_at = models.DateTimeField(auto_now_add=True) photo = models.ImageField(upload_to='dice') title = models.CharField(max_length=30) kind = models.CharField(max_length=30) size = models.CharField(max_length=30) strength = models.CharField(max_length=30) combinations = models.CharField(max_length=30) favors = models.CharField(max_length=30) availability = models.BooleanField(default=True) iscarousel = models.BooleanField(default=False) def __str__(self): return "%s %s %s %s %s %s %s %s %s %s" % (self.created_at, self.photo, self.title, self.kind, self.size, self.strength, self.combinations, self.favors, self.availability, self.iscarousel) in a template or view when I get a photo and do photo.photo.url I get the url that the photo is associated with but with a ? and right after the question mark more characters appended. I don't understand why this is but basically because of this my image does not render How do I get my url to just be the url of where my image is saved in aws -
I have learned a part of front-end but i prefer back-end so which the best for back-end php or python [closed]
Which the best for back-end php or python ? -
Django forms filter greater than doesn't work
I'm trying to filter a chained dropdown list, so it only shows hora greater than 0. But I don't know why .filter(cupos__gt=0) in forms.py doesn't work but if I try to print it in the terminal it works forms.py class ReservaForm(forms.ModelForm): class Meta: model = Reserva fields = ('actividad', 'dia', 'hora') def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['dia'].queryset = Dia.objects.none() if 'actividad' in self.data: try: actividad_id = int(self.data.get('actividad')) self.fields['dia'].queryset = Dia.objects.filter(actividad_id=actividad_id).order_by('name') except (ValueError, TypeError): pass # invalid input from the client; ignore and fallback to empty Dia queryset elif self.instance.pk: self.fields['dia'].queryset = self.instance.actividad.dia_set.order_by('name') self.fields['hora'].queryset = Hora.objects.none() if 'dia' in self.data: try: dia_id = int(self.data.get('dia')) self.fields['hora'].queryset = Hora.objects.filter(dia_id=dia_id,cupos__gt=0).order_by('name') except (ValueError, TypeError): pass # invalid input from the client; ignore and fallback to empty Dia queryset elif self.instance.pk: self.fields['hora'].queryset = self.instance.dia.hora_set.order_by('name') views.py def reserva_add(request): form = ReservaForm() hora = Hora.objects.filter(cupos__gt=0) print(hora) context = { 'form': form, } return render(request, "agenda/reserva_form.html", context) Terminal <QuerySet [<Hora: 09:00:00 52 >]> 8 am shouldn't appear -
Django pagination based on a field not a number of records per page
Using Django 3.1 I want to allow users to be able to paginate based on a table column value - same value - one page. I use ListView CBV to show a table where one column is a date and one date should be shown on one page. So instead of: class RecipeListView(SingleTableMixin, LoginRequiredMixin, FilterView): model = Recipe table_class = RecipeTable template_name = 'kitchen/recipe/list.html' filterset_class = RecipeFilter form_class = RecipeSearchForm paginate_by = 15 I am trying to achieve something like this: class RecipeListView(SingleTableMixin, LoginRequiredMixin, FilterView): model = Recipe table_class = RecipeTable template_name = 'kitchen/recipe/list.html' filterset_class = RecipeFilter form_class = RecipeSearchForm paginate_by_attribute = 'date' Full example code: https://github.com/valasek/kicoma/blob/master/kicoma/kitchen/views.py Docs Pagination, Paginator did not helped. Any hint appreciated.