Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django Add Q filters to query when related object exists, Conditional query
I am triying to query my product model like this: I have Product model that is related to an user My user can have a related Store, and its store can be unactive or no-visible So, my products can be sold by an user that has a store or an user that not has a store; what I want to do is to make query adding these extra parameters when "has_store" condition beign True like this. store_query = ({"seller__user_store__is_active": True, "seller__user_store__is_visible": True} if F("seller__user_store__isnull=False") else {}) And then add that query to my filtering sentence: Product.objects.filter(Q(is_new=True) & Q(is_active=True), **store_query) My product model also has is_new, and is_active and other parameters. So, expected behaviour is something like add Q(seller__user_store__is_visible=True) and Q(seller__user_store__is_active=True) if product seller has a related store I hope have been clear, thanks you a lot -
How to extract "label" and "help_text" data from Django Rest Framework serializer?
I have defined Item model in models.py exactly like below. class Item(models.Model): transaction_id = models.CharField(max_length=25, unique=True) order = models.ForeignKey(Order, on_delete=models.CASCADE) item_type = models.ForeignKey(ItemType, on_delete=models.SET_DEFAULT, default=1) title = models.CharField(max_length=200) description = models.TextField(null=True, max_length=3000) seller_user_id = models.IntegerField(null=True) buyer_user_id = models.IntegerField(null=True) listing_id = models.IntegerField(null=True) creation_tsz = models.DateTimeField() price = models.DecimalField(max_digits=9, decimal_places=2) shipping_cost = models.DecimalField(max_digits=9, decimal_places=2) quantity = models.IntegerField() currency_code = models.CharField(null=True, max_length=5) product_data = JSONField(null=True) personalization = models.TextField(max_length=1000, null=True) I have also defined my ItemSerializer with part below. class ItemSerializer(serializers.ModelSerializer): id = serializers.IntegerField(read_only=True) transaction_id = serializers.CharField(read_only=True, max_length=25) title = serializers.CharField(max_length=200) description = serializers.CharField(allow_null=True, max_length=3000) seller_user_id = serializers.IntegerField(allow_null=True, validators=[validate_positive]) buyer_user_id = serializers.IntegerField(allow_null=True, validators=[validate_positive]) listing_id = serializers.IntegerField(allow_null=True, validators=[validate_positive]) creation_tsz = serializers.DateTimeField() price = serializers.DecimalField(label='Price', max_digits=9, decimal_places=2, validators=[validate_positive]) shipping_cost = serializers.DecimalField(max_digits=9, decimal_places=2, validators=[validate_positive]) quantity = serializers.IntegerField(read_only=True) currency_code = serializers.CharField(allow_null=True, max_length=5) product_data = serializers.CharField(allow_null=True) personalization = serializers.CharField(max_length=1000, allow_null=True) def update(self, item:Item, validated_data): #Validating whether if 'product_data' in validated_data: schema_obj = item.item_type.schema try: print(validated_data) jsonschema.validate( json.loads(validated_data['product_data']), schema=schema_obj) except Exception as e: raise ValidationError({'product_data':[f'Schema for product data is not valid. {str(e)}']}) for key in validated_data: setattr(item, key, validated_data[key]) item.save() return item def validate_product_data(self, value): """ Validate whether property is json parsable :param value: :return: """ try: cur_obj = json.loads(value) return value except Exception as e: raise ValidationError("This field should … -
Django renew field from database,calculated field in Database
Newby in django, have two question, can't find needed info. 1) I have database (SQLite) which have table scale_calibration and field weight. Other application rewrite value in field weight 1-2 times per second. Is there possibility in Django to renew this field without renew browser (F5)? models.py: from django.db import models class Calibration(models.Model): mean_weight = models.FloatField(editable=True) hours_to_export = models.PositiveIntegerField(default=4, editable=True) weight = models.FloatField(editable=True) admin.py: from django.contrib import admin from .models import Calibration # Register your models here. admin.site.register(Calibration) 2) I try follow that link to make easy calculated field (that will be write to database when save), but i have no results and no error, don't understand where i get mistake. models.py: from django.db import models class Calibration(models.Model): mean_weight = models.FloatField(editable=True) hours_to_export = models.PositiveIntegerField(default=4, editable=True) weight = models.FloatField(editable=True) calibration_factor = models.FloatField(editable=True) @property def get_calibration(self): return self.weight/self.mean_weight def save(self, *args, **kwarg): self.calibration_factor = self.get_calibration() super(Calibration, self).save(*args, **kwarg) Please help with advise. -
Flask or django to do image processing on a web application
Im working on a project of image processing using opencv in a web application but i dont know what to use flask or django ? Some one to help me ? -
Is there a standard way to create dynamic formsets in Django?
I have created formsets in Django, and am able to render them in templates. The issue is that there does not seem to be a “standard” way to Add/Delete forms from formsets in the templates. I have tried “Django-dynamic-formsets” but this seems like a hack of a way to achieve something that is built into the Django admin. So is there a Django way to have an “Add” and “Delete” button on a template for adding and deleting forms from a formset? If it matters, I’m using inline formsets Django 2.2 -
how to keep selected option after refresh the page
I trying to implement filter option with django I can do the filtering, but can't keep the selected option after the refresh because I am rendering the filtered result, any solution? views.py def filter(request): products = Product.objects.all() print(products) if request.method == 'POST': title = request.POST.get('da',None) print(title) titre = Product.objects.filter(title=title) print(titre) return render(request, 'searchapp/searchview.html', {'qs': titre,'ds':products}) return render(request,'searchapp/searchview.html',{'qs':products}) html : <div> <form action="{% url 'search:query' %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <div class="form-group"> <label>Title</label> <select name="da" class="form-control"> {% for obj in qs %} <option value="{{obj}}">{{obj}}</option> {%endfor%} <!-- <input type="text" class="form-control" name="title">--> </select> </div> <button type="submit" class="btn btn-warning btn-lg">Add Product</button> </form> </div> -
Make a Django query from raw SQL with Distinct over Django ManyToMany Relation
models.py class FlagsModel(models.Model): name = models.CharField(max_length=64, verbose_name='Flag') class ColorsModel(models.Model): name = models.CharField(max_length=64, verbose_name='Color') class RelationModel(models.Model): flag = models.ForeignKey(FlagsModel, null=True, on_delete=models.SET_NULL) color = models.ForeignKey(ColorsModel, null=True, on_delete=models.SET_NULL) I am looking now for a Query which selects each flag containing the colors "yellow" and "red" in raw SQL i can do something like this: SELECT DISTINCT a1.flag_id from relationmodel a1, relationmodel a2 where a1.color_id=2 AND a2.color_id=6 (id 2 = yellow, id 6= red) but i havent found a solution that's gonna work in django any ideas ? -
Avoid duplicates in postgresql database after scraping with beautifull soup
I'm scraping god names from the website of a game. The scraped text is stored in a postgresql database through Django models. When I run my program twice, I get everything double. How do I avoid this? import requests import urllib3 from bs4 import BeautifulSoup import psycopg2 import os import django os.environ['DJANGO_SETTINGS_MODULE'] = 'locallibrary.settings' django.setup() from scraper.models import GodList urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) session = requests.Session() session.headers = { "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.117 Safari/537.36"} url = 'https://www.smitegame.com/' content = session.get(url, verify=False).content soup = BeautifulSoup(content, "html.parser") allgods = soup.find_all('div', {'class': 'god'}) allitem = [] for god in allgods: godName = god.find('p') godFoto = god.find('img').get('src') allitem.append((godName, godFoto)) GodList.objects.create(godName=godName.text) -
manage.py startapp creates read-only files from within docker
I have a django app running in a docker container. I bashed into the container and used python manage.py startapp. However, the created directories and files appear to be read-only. From within intellij, I am unable to edit the files. I get a popup offering to clear the readonly status, but that just gives me an error unable to clear readonly status on the following files. How can I edit my files outside of the container? -
DjangoRestAPI : "detail": "Unsupported media type \"text/plain\" in request."
Postman throwing me error while i request the url as 'POST' method class ProductAddAPIView(APIView): def post(self,request,format="text/plain"): product_data = [] title = request.POST.get('title') slug = request.POST.get('slug') # price = request.POST.get('price') all_data = [title, slug] product_data.append(all_data) return Response({"product_data":product_data},status=status.HTTP_200_OK) -
flake8 internal error in regular expression engine
I'm trying to run flake8 on a docker django built like described here (tutorial page) when building the docker image I get an error from flake8 which is run in an docker-compose file with like so $ flake8 --ignore=E501,F401 . multiprocessing.pool.RemoteTraceback: Traceback (most recent call last): File "/usr/local/lib/python3.8/multiprocessing/pool.py", line 125, in worker result = (True, func(*args, ** File "/usr/local/lib/python3.8/multiprocessing/pool.py", line 48, in return list(map(* File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 666, in return checker.run_checks() File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 598, in run_checks self.run_ast_checks() File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 495, in run_ast_checks checker = self.run_check(plugin, tree=ast) File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 426, in run_check self.processor.keyword_arguments_for( File "/usr/local/lib/python3.8/site-packages/flake8/processor.py", line 241, in keyword_arguments_for arguments[param] = getattr(self, param) File "/usr/local/lib/python3.8/site-packages/flake8/processor.py", line 119, in file_tokens self._file_tokens = list( File "/usr/local/lib/python3.8/tokenize.py", line 525, in _tokenize pseudomatch = _compile(PseudoToken).match(line, pos) RuntimeError: internal error in regular expression engine The above exception was the direct cause of the following exception: Traceback (most recent call last): File "/usr/local/bin/flake8", line 8, in <module> sys.exit(main()) File "/usr/local/lib/python3.8/site-packages/flake8/main/cli.py", line 18, in main app.run(argv) File "/usr/local/lib/python3.8/site-packages/flake8/main/application.py", line 393, in run self._run(argv) File "/usr/local/lib/python3.8/site-packages/flake8/main/application.py", line 381, in _run self.run_checks() File "/usr/local/lib/python3.8/site-packages/flake8/main/application.py", line 300, in run_checks self.file_checker_manager.run() File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 329, in run self.run_parallel() File "/usr/local/lib/python3.8/site-packages/flake8/checker.py", line 293, in run_parallel for ret in pool_map: File … -
Perform the PHP operation in Python in Django
I am calling a php file from a javascript code in Django, it seems it is not able to find it. So i was thinking to call a python function that will perform the same operation as of the given in PHP. the js code where im calling php file var filename = new Date().toISOString(); //filename to send to server without extension //upload link var upload = document.createElement('a'); upload.href = "#"; upload.innerHTML = "Upload"; upload.addEventListener("click", function(event) { var xhr = new XMLHttpRequest(); xhr.onload = function(e) { if (this.readyState === 4) { console.log("Server returned: ", e.target.responseText); } }; var fd = new FormData(); fd.append("audio_data", blob, filename); xhr.open("POST", "upload.php", true); xhr.send(fd); }) li.appendChild(document.createTextNode(" ")) //add a space in between li.appendChild(upload) //add the upload link to li Upload.php print_r($_FILES); //this will print out the received name, temp name, type, size, etc. $input = $_FILES['audio_data']['tmp_name']; //get the temporary name that PHP gave to the uploaded file $output = $_FILES['audio_data']['name'].".wav"; //letting the client control the filename is a rather bad idea //move the file from temp name to local folder using $output name move_uploaded_file($input, $output) so basically this php file is setting the blob file in my server Can anyone please help me to convert … -
Serving static files while debug = false, and debug = true
I have django project running, 2.2. And python version 3.6.9 If i have debug = True, everything is running correctly. But when I have debug = False, my css gets ruined. It has to do with staticfiles i guess. I want to know, if am running apache server using pycharm on ubuntu. Where do I need to copy/paste my static and static_deploy folders for this to work? -
Django Annotated Query to Count Only Latest from Reverse Relationship
Problem Overview Given the models class Candidate(BaseModel): name = models.CharField(max_length=128) class Status(BaseModel): name = models.CharField(max_length=128) class StatusChange(BaseModel): candidate = models.ForeignKey("Candidate", related_name="status_changes") status = models.ForeignKey("Status", related_name="status_changes") create_id = models.DateTimeField(auto_now_add=True, blank=True) And SQL Tables: candidates +----+--------------+ | id | name | +----+--------------+ | 1 | Beth | | 2 | Mark | | 3 | Mike | | 4 | Ryan | +----+--------------+ status +----+--------------+ | id | name | +----+--------------+ | 1 | Review | | 2 | Accepted | | 3 | Rejected | +----+--------------+ status_change +----+--------------+-----------+------------+ | id | candidate_id | status_id | created_at | +----+--------------+-----------+------------+ | 1 | 1 | 1 | 03-01-2019 | | 2 | 1 | 2 | 05-01-2019 | | 4 | 2 | 1 | 01-01-2019 | | 5 | 3 | 1 | 01-01-2019 | | 6 | 4 | 3 | 01-01-2019 | +----+--------------+-----------+------------+ I want to get the get the total number of candidates with a given status, but only the latest status_change is counted. SQL Solution Using SQL, I was able to achieve it using Group BY and COUNT. (SQL untested) SELECT status.id as status_id , status.name as status_name , COUNT(*) as status_count FROM ( SELECT status_id, Max(created_at) AS … -
create api for url shortning in Django
My model: class UrlShort(models.Model): hash_url = models.CharField(max_length=100) original_url = models.CharField(max_length=100) creation_date = models.DateTimeField() expiry_date = models.DateTimeField() I need to create a hashed url (from original url) with 16 byte in django and store in database with expiry time of 30 seconds of creation time. What will be Query ? -
Django and Excel File
I have an unusual question. I made a website that Scrapes another website and store/organizes all the data on multiple spreadsheets (XLSX Excel file). These excel files are stored on the computer running the website. Every time the scrape button is clicked, the website is scraped and the data is stored. What I want to do, is to make that data available. So, after the website is scraped I want the excel files to be download on the computer that used my website. How can I do that, is there an easy way? -
how to get data from selection tag with django
I trying to implement filter function to get data from a selection tag using post form but the result is always None view.py div> <form action="{% url 'search:query' %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <div class="form-group"> <label>Title</label> <select title="da" class="form-control"> {% for obj in qs %} <option value="{{obj}}">{{obj}}</option> {%endfor%} <!-- <input type="text" class="form-control" name="title">--> </select> </div> <button type="submit" class="btn btn-warning btn-lg">Add Product</button> </form> </div> html : div> <form action="{% url 'search:query' %}" method="post" enctype="multipart/form-data"> {% csrf_token %} <div class="form-group"> <label>Title</label> <select title="da" class="form-control"> {% for obj in qs %} <option value="{{obj}}">{{obj}}</option> {%endfor%} </select> </div> <button type="submit" class="btn btn-warning btn-lg">Add Product</button> </form> </div> -
Different newline character lengths for Django and SNS servers, while subscribing a server Endpoint to SNS
I am currently writing an API which will recieve direct messages (POST) from AWS Simple Notification Sercive. I am currently facing the issue with the content-length of the message sent by SNS to my end point. Below is the payload : { "Type" : "Notification", "MessageId" : "da41e39f-ea4d-435a-b922-c6aae3915ebe", "TopicArn" : "arn:aws:sns:us-west-2:123456789012:MyTopic", "Subject" : "test", "Message" : "test message", "Timestamp" : "2012-04-25T21:49:25.719Z", "SignatureVersion" : "1", "Signature" : "EXAMPLElDMXvB8r9R83tGoNn0ecwd5UjllzsvSvbItzfaMpN2nk5HVSw7XnOn/49IkxDKz8YrlH2qJXj2iZB0Zo2O71c4qQk1fMUDi3LGpij7RCW7AW9vYYsSqIKRnFS94ilu7NFhUzLiieYr4BKHpdTmdD6c0esKEYBpabxDSc=", "SigningCertURL" : "https://sns.us-west-2.amazonaws.com/SimpleNotificationService-f3ecfb7224c7233fe7bb5f59f96de52f.pem", "UnsubscribeURL" : "https://sns.us-west-2.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55" } And the headers are: x-amz-sns-message-type: Notification x-amz-sns-message-id: da41e39f-ea4d-435a-b922-c6aae3915ebe x-amz-sns-topic-arn: arn:aws:sns:us-west-2:123456789012:MyTopic x-amz-sns-subscription-arn: arn:aws:sns:us-west-2:123456789012:MyTopic:2bcfbf39-05c3-41de-beaa-fcfcc21c8f55 Content-Length: 761 Content-Type: text/plain; charset=UTF-8 Host: ec2-50-17-44-49.compute-1.amazonaws.com Connection: Keep-Alive User-Agent: Amazon Simple Notification Service Agent The payload above can have two content-lengths : 761 and 773. The issue is SNS counts newline as one character('\n') while the django server counts the newline as two characters('\r\n'). SNS sends content-length to be 761, and hence django closes the stream after reading the 761's character according to it. But as it considers newline to be two characters, it leaves some information. Also, the point to notice is that SNS sends json with header of content-type set to 'text/plain' and not 'application/json'. What should I do to parse the whole message, and not leave even a single character … -
Django form not showing fields in correct order
I am not sure what i am doing wrong. The fields are mentioned in order. Still i get different order of the fields everytime i restart the django server. Here is the screenshot. views.py def user_creation(request): if request.method == 'POST': user_form = UserRegForm(request.POST) if user_form.is_valid(): user = user_form.save() return render(request, 'info/base.html') else: user_form = UserRegForm() context = { 'UserForm' : user_form, } return render(request, 'users/form.html', context ) forms.py from django import forms from django.contrib.auth.models import User from django.contrib.auth.forms import UserCreationForm class UserRegForm(UserCreationForm): class Meta: model = User fields = { 'username', 'password1','password2' } -
How or where to handle form validation errors for unique constraint in modelformset_factory formset
I'm using a modelformset_factory formset and I'm not sure where and/or how to handle unique constraint errors so that I can gracefully display them on my template html instead of just crashing the app. It seems that when you create a formset in the forms.py that you can't then create a function underneath and start trying to clean data and/or raise form validation errors. The formset works great in adding new data but at this point, I'm not sure how to handle the unique constraint errors. Model class Disciplines(models.Model): class Meta: unique_together = (('account','discipline'),) verbose_name_plural = ('Disciplines') account = models.ForeignKey(Account, on_delete=models.CASCADE) discipline = models.CharField(max_length=40,choices=Discipline_Choices) rank = models.CharField(max_length=41, choices=Rank_Choices) conferred_date = models.DateField(blank=True, null=True) expires_date = models.DateField(blank=True, null=True) def __str__(self): return self.account.first_name + ' ' + self.account.last_name + ' ' + self.discipline + ' ' + self.rank def has_module_perms(self, app_label): return True Form from django.forms.models import modelformset_factory from account.models import Disciplines DisciplineFormSet = modelformset_factory(Disciplines, fields=('id','discipline','rank'), can_delete=True) View def discipline_update_view(request): if not request.user.is_authenticated: return redirect('login') user = request.user account_id = user.id if request.POST: Disc_Update_Form = DisciplineFormSet(request.POST) # import pdb; pdb.set_trace() if Disc_Update_Form.is_valid(): for form in Disc_Update_Form: instance = form.save(commit=False) if not instance.discipline: continue instance.account_id = account_id instance.save() return redirect('account') else: Disc_Update_Form = DisciplineFormSet(queryset=Disciplines.objects.filter(account_id=account_id).order_by('rank')) … -
Set a value in Model field based on another value from other model
I have two models where one has a ForeignKey relationship with other entity. The thing is the date in data_start field from ModelB needs to be not equal and grater than the date in date_end from ModelA. How can I do this comparison(rule) inside the ModelB and save it? class ModelB(models.Model): date_start = models.DateTimeField('') date_end = models.DateTimeField('') class ModelA(models.Model): name = models.CharField(...) date_start = models.DateTimeField('') date_end = models.DateTimeField('') resource = models.ForeignKey(ModelB,...) -
django.db.utils.IntegrityError: NOT NULL constraint failed: users_customuser.user_type
I am trying to create a role-based authentication in Django and i getting this error django.db.utils.IntegrityError: NOT NULL constraint failed: users_customuser.user_type This is my models: from django.contrib.auth.models import AbstractUser from django.db import models class CustomUser(AbstractUser): USER_TYPE_CHOICES = ((3, 'secretary'),(4, 'supervisor'),(5, 'admin'),) user_type = models.PositiveSmallIntegerField(choices=USER_TYPE_CHOICES) def __str__(self): return self.name -
The model has two many-to-many relations through the intermediate model
I have a model that has 2 separate ManyToManyField relations back to itself class Company(models.Model): parent = models.ManyToManyField("self", through='CompanyParent', through_fields=('company_child', 'company_parent'), related_name='+') child = models.ManyToManyField("self", through='CompanyParent', through_fields=('company_parent', 'company_child'), related_name='+') The above works fine on my localhost Django v3.0.2/ SQLite 3.8.7.2 To actually publish it, I have had to use Django v2.1.15/ SQLite 3.7.17, but with the published version it is chucking out the following errors companies.Company.child: (fields.E332) Many-to-many fields with intermediate tables must not be symmetrical. companies.Company.parent: (fields.E332) Many-to-many fields with intermediate tables must not be symmetrical. companies.Company: (models.E003) The model has two many-to-many relations through the intermediate model 'companies.CompanyParent'. What's going on here? Solved the first 2 issues by adding symmetrical=False to each model, but no idea how to solve the final error? -
How to insert values into related models?
I have two models as follows: class Reservation(models.Model): startDate = models.DateField() endDate = models.DateField() count = models.IntegerField(blank=False, null=False) def generateReservationItems(self): for d in range((endDate - startDate).days): reservationItem= ReservationItems(reservationDate=d, count = count) reservationItem.save() class ReservationItems(models.Model): reservationDate = models.DateField(); reservation = models.ForeignKey(Reservation, on_delete=models.CASCADE) count = models.IntegerField(blank=False, null=False) class Reservation represents a package of dates (range of dates), and the class ReservationItems stores every single date exists in Reservation class. I built 'generateReservationItems()'function to be automatically invoked when a Reservation object is saved. The function inserts number of items equals to the different between startDate and endDate. for example: if startDate = Mars 6 2020, and endDate = Mars 10 2020, 5 items will be inserted in ReservationItems: Mars 6 2020 Mars 7 2020 Mars 8 2020 Mars 9 2020 Mars 10 2020 But Reservation objects are inserted successfully without inserting the items into reservationItems. I don't know how to make the 'generateReservationItems()' invoked once a reservation object is created. any ideas please to do it, or if there and other effective methods to perform such tasks. -
Calling a function from a python file into a html template in Django
I am trying to add the output of a function from a python file into an html file in django project. function name is stat() and the file name is test1.py: from django import template register = template.Library() @register.simple_tag def stat(): x = 'Succccceeesss' return x html file name is html_test1.html: {% load test1 %} <div id="id_div"> {% stat as result %} <h> {{ result }} </h> </div> I know I can build the html string in the python file and then call it in the view, but this defeats the purpose of using django, and is not a clean/efficient way to handle enormous quantity of data. Thanks.