Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Redirect User to external site and get response
I‘ve wrote this Python package to make requests to the non-publicly Audible API. To use this package, the user has to authorize himself to Amazon. Now I’m writing a Django App to give the user a new experience when using the API. My problem is to implement the authorization part. Best solution where to redirect the user to a special Amazon login site and set some cookies on client-side which then be send to Amazon. During authorization, the user have to solve a Captcha (when init cookies doesn’t set correctly) and a 2FA prompt! After a successfully authorization the user are redirected by Amazon to a Url, which contains the needed access token. How I can get this Url with Django? Questions over Questions but no answers yet. My intention is not to write multiple views to condition race the result (successfull/Captcha/2FA/...)! -
WooCommerce Python API Returning 404 on Valid Routes
My API is instantiated like this: wc_api = API( url = 'https://www.example.com', consumer_key='xxxxxxxxxxx', consumer_secret='xxxxxxxxxxx', wp_api=True, version='wc/v3', timeout=10 ) This is the line I'm making a request: wc_response = wc_api.get(f'{endpoint}?page={page}') And the function it's a part of: def get_all(endpoint:str): records = [] page = 1 while True: wc_response = wc_api.get(f'{endpoint}?page={page}') if wc_response is None or wc_response.status_code >= 400: if wc_response is not None and wc_response.status_code == 401: raise PermissionDenied('WooCommerce API denied access to tags') else: raise Exception('Unexpected error from WooCommerce API', wc_response) data = wc_response.json() if len(data) == 0: break records.extend(data) page += 1 return records The specific endpoint I'm having a problem with is products/attributes, although I suspect other valid routes are also going to return 404. I've tested that route using Postman and it returns the data just fine, it's only 404'ing through the Python API. I have previously made a successful call to products using the Python API and I don't think I've changed my configuration at all. This is the URL I used for that test: https://www.example.com/wp-json/wc/v3/products/attributes?page=1 Any ideas on why this request is 404'ing through the Python API and not Postman? -
Python function that takes a json object returns mongodb query object
I need to take a JSON (conditions to filter data), example data: query = { "and": [ { "field": "model", "operator": ">", "value": "2005" }, { "or": [ { "field": "brand", "operator": "=", "value": "Mercedes" }, { "field": "brand", "operator": "=", "value": "BMW" } ] } ] and write a function that transforms this JSON as a MongoDB query so I can directly filter the data while reading from MongoDb (I will use this data inside of apache beam ReadFromMongoDB function, as filter object) so the output should be like: {'$and': [{'field': 'model', 'operator': '>', 'value': '2005'}, { '$or': [ {'field': 'brand', 'operator': '=', 'value': 'Mercedes'}, {'field': 'brand', 'operator': '=', 'value': 'BMW'} ] }]} ( I have a function that transforms each single object into a MongoDB query so don't worry about that) WHAT I TRIED As I don't know how nested data will come, I used recursion in my function. But it becomes complicated and output is not as I expected. def splitToItems(query, items={}): if 'and' in query: for item in query['and']: if 'and' in item or 'or' in item: splitToItems(item) else: if '$and' not in items: items['$and'] = [item] # print(items) else: items['$and'].append(item) # print(items) if 'or' in … -
Django import-export duplicating rows when importing same file multiple times
I am building a tool that allows users to upload CSV files to update the database using the django-import-export tool. I uploaded my test CSV file with one data row, then uploaded it again and got a duplicated row (with a new primary key but all the other values are the same). The row.import_type value is "updated" but the only thing that is updated is the id. Then I upload the same file a third time and get an error: app.models.Role.MultipleObjectsReturned: get() returned more than one Role -- it returned 2! (I really appreciate the exclamation point in that error message, by the way.) Ideally I would get a skipped row on the second import and third import of the file. I suppose I'd be okay with an error. The file's contents are: Sales Role,System Role,System Plan,id Sales Rep,Account Executive,951M-NA, This is the format users get when they export the csv dataset. Ideally they would export a file, change a few columns (aside from the name which is the import_id_field), and re-upload the data. In app/resources.py: class RoleResourec(resources.ModelResource): name = Field(attribute='name', column_name="Sales Role") default_role = Field(attribute='default_role', column_name="System Role") default_plan = Field(attribute='default_plan', column_name="System Plan") class Meta: models=Role fields= ('id', 'name', 'default_role', … -
I have JSONField use a library to call this object for sql postgres How to use sqlite the same library
I have JSONField use a library to call this object for sql postgres How to use sqlite the same library models.py: from django.contrib.postgres.fields import JSONField class DeclareResult(models.Model): marks = JSONField(blank=True) -
(React and Django) Displaying products on the homepage is working fine, but when I click on any particular product, then the rendering is wrong
this post is a third part of this series - (You can skip these first two parts, however - it can serve as a reference) A large number of problems with React, Django, Django REST and Axios Products on the homepage are not being displayed properly (Django, Django Rest and React) My homepage looks like this - But when I click on any particular product, I get this result - I was consulting this issue with my friend, who has told me, that my React code should be fine and the problem should be somewhere inside the Django. HomeScreen.js - import React, { useState, useEffect } from "react"; import { Row, Col } from "react-bootstrap"; import Product from "../components/Product"; import axios from "axios" function HomeScreen() { const [products, setProducts] = useState([]) useEffect(() => { async function fetchProducts() { const { data } = await axios.get('/api/products/') setProducts(data) } fetchProducts() },[] ) return ( <div> <h1>Latest Products</h1> <Row> {products.map((product) => ( <Col key={product._id} sm={12} md={6} lg={4} xl={3}> <Product product={product} /> </Col> ))} </Row> </div> ); } export default HomeScreen; ProductScreen.js - import React, { useState, useEffect } from "react"; import { Link } from "react-router-dom"; import { Row, Col, Image, ListGroup, Button, … -
Django Rest Framework: How to bulk_create nested objects during de-serialization?
I have three models: class Customer(models.Model): name = models.CharField(max_length=30) class Order(models.Model): customer = models.ForeignKey(Customer,on_delete=models.CASCADE) class LineItem(models.Model): order = models.ForeignKey(Order,on_delete=models.CASCADE) name = models.CharField(max_length=30) Here is my test: class CreateOrdersTest(APITestCase): def setUp(self): self.TEST_SIZE = 10 self.factory = APIRequestFactory() self._setup_source() self.data = self._build_data() def _setup_source(self): Customer.objects.create(name='test-customer') def _build_data(self): return [{'customer':'test-customer','lineitems':[{'name':'apples'},{'name':'oranges'}]} for x in range(self.TEST_SIZE)] def test_post_orders(self): request = self.factory.post('/create_orders',self.data) response = create_orders(request) response.render() self.assertEqual(response.status_code,status.HTTP_201_CREATED) so the post object looks like [{'customer': 'test-customer', 'lineitems': [{'name': 'apples'}, {'name': 'oranges'}]}, .... ] here are the serializers: class BulkLineItemSerializer(serializers.ModelSerializer): def create(self,validated_data): lineitems = [LineItem(**validated_data) for item in validated_data] return LineItem.objects.bulk_create(**validated_data) class LineItemSerializer(serializers.ModelSerializer): order = ModelObjectidField() def create(self,validated_data): return LineItem.objects.create(**validated_data) class Meta: model = LineItem list_serializer_class = BulkLineItemSerializer fields = ['name','order'] class BulkOrderSerializer(serializers.ListSerializer): def create(self,validated_data): orders = [Order(**item) for item in validated_data] return Order.objects.bulk_create(orders) class OrderSerializer(serializers.ModelSerializer): customer = serializers.SlugRelatedField(slug_field='name',queryset=Customer.objects.all()) def create(self,validated_data): return Order.objects.create(**validated_data) class Meta: model = Order fields = ['customer'] list_serializer_class = BulkOrderSerializer then here is that ModelObjectidField I use for the order object. class ModelObjectidField(serializers.Field): def to_representation(self, value): return value.id def to_internal_value(self, data): return data Because I am passing the actual object into the field like <Order(1)>, I just return it directly for the internal_value. And finally here is my view. This is how I match … -
Django - AWS S3 - Moving Files
I am using AWS S3 as my default file storage system. I have a model with a file field like so: class Segmentation(models.Model): file = models.FileField(...) I am running image processing jobs on a second server that dump processsed-images to a different AWS S3 bucket. I want to save the processed-image in my Segmentation table. Currently I am using boto3 to manually download the file to my "local" server (where my django-app lives) and then upload it to the local S3 bucket like so: from django.core.files import File import boto3 def save_file(segmentation, foreign_s3_key): # set foreign bucket foreign_bucket = 'foreign-bucket' # create a temp file: temp_local_file = 'tmp/temp.file' # use boto3 to download foreign file locally: s3_client = boto3.client('s3') s3_client.download_file(foreign_bucket , foreign_s3_key, temp_local_file) # save file to segmentation: segmentation.file = File(open(temp_local_file, 'rb')) segmentation.save() # delete temp file: os.remove(temp_local_file) This works fine but it is resource intensive. I have some jobs that need to process hundreds of images. Is there a way to copy a file from the foreign bucket to my local bucket and set the segmentation.file field to the copied file? -
Django: 'utf-8' codec can't decode byte 0xd1 in position 2: invalid continuation byte
I'm new in Django so I got stuck. I'm having troubles with Django and MySQL connection. Instead of Cyrillic characters I got a bunch of strange symbols, but I'm using utf8_unicode_ci as character and collation in MySQL for database and tables. How can I solve this problem? This is the error I'm getting: This is what I got in MySQL: -
Problem with 'specify' field in jQuery, not returning to the default value
I'm having a problem with the following code. $(document).ready(function(){ $('#list').change(function(){ $('#specify')[$(this).val()=='Other' ? 'show' : 'hide'](); }); }); /// id 'list' is the id of the choice field form and id 'specify' is the id of the field that appears when 'Other' is selected, that is '0' by default. I'm creating an app with Django that has a choice field with some of the popular selections. But I wanted that if the choice 'Other' is selected another field appears. I managed to do it, and it's working but with the following 'bug': if 'Other' is selected, and you type an input and then change again to one of the items in the list, the input is still there. How can I use jQuery in order to write a '0' if it goes from 'Other' to one of the other choices of the list? -
(django.db.utils.OperationalError: near ")": syntax error) while creating custon django user model
So i have been racking my beain for literal hours trying to create a simple user model. I believe Abstractbaseuser creates default id, email, and password columns, and I added a couple more fields. I also added the AUTH_USER_MODEL into settings.py so I'm pretty sure all the set up should be done to at least be able to run the migration. makemigrations works and passes, but migrate gives me that error every time. So I think, I set up the model correctly(hopefully) but there has to be something I'm missing. from django.contrib.auth.models import AbstractBaseUser from django.db import models class User(AbstractBaseUser): username = models.CharField(unique=True, max_length=64, default='') email = models.EmailField(unique=True, max_length=64) is_verified = models.BooleanField(default=True) USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['username'] def __str__(self): f"{'username'} {'email'}" return -
How to configure sqlite of django project with pythonanywhere
I have deployed my project to pythonanywhere. It is working locally. But with pythonanywhere I am getting no such table exception. I have configured sqllite as in this link Just mentioned to generate the sqlite file using runmigrations. I have changed the settings.py to use os.path.join at that Database section also but still same issue. -
i am getting error Uncaught TypeError: Cannot read property 'innerHTML' of nul in javascript [duplicate]
index:601 Uncaught TypeError: Cannot read property 'innerHTML' of null at updatePopover (index:601) at index:593 enter image description here -
how to post a model with a foreignkey using django rest framework with only one field?
I'm working on an API, django rest framework for the backend and react on the frontend. i only want to create a model and pass the foreignkey as a dropdown like in django rest framework but with reactjs i have many models the ones without foreignkeys are esly manipulated but i have some models with foreignkeys that i can get but can't POST even with postman i get: "name": [ "This field is required." ] i have seen many posts that has same issues but didn't understant gow to implement it here is a simple model with a foreign key models.py class SalleSport(models.Model): name = models.CharField( max_length=50) adresse = models.CharField( max_length=50) class Planning(models.Model): name = models.CharField(max_length=50) salle_sport = models.ForeignKey(SalleSport, verbose_name="Salle de sport", on_delete= models.CASCADE, null=True, blank=True) serializers.py class SalleSportSerialiser(serializers.ModelSerializer): class Meta: model = SalleSport fields= '__all__' as i want to create a Planning instance i tried many solution that didn't worked i'm gona put them as comments class PlanningSerialiser(serializers.ModelSerializer): # salle_sport = SalleSportSerialiser(read_only=False) class Meta: model = Planning fields= '__all__' # fields= ('id', 'name', 'salle_sport') # def create(self, validated_data): # salle_sport = validated_data.pop('salle_sport', None) # if author_data: # salle_sport = SalleSport.objects.get_or_create(**salle_sport)[0] # print('salle de sport',salle_sport) # validated_data['salle_sport'] = salle_sport # … -
Django models dependencies and transfer ownership
What I am trying to build is a app that can deal with honey production management. There are number of producers that produce honey pots and they can give their pots to a variety of stores that have their customers. One producer can give his production to many stores, and a store can have honey pots from many producers. Honey pots can be tracked by pot_id. The models.py looks like this: class Produser(models.Model): name = models.CharField(max_length=255) address = models.CharField(max_length=255) class Store(models.Model): name = models.CharField(max_length=255) address = models.CharField(max_length=255) produser= models.ManyToManyField(Produser) class Customer(models.Model): first_name = models.CharField(max_length=255) last_name = models.CharField(max_length=255) store = = models.ForeignKey(Store, on_delete=models.CASCADE) class HoneyPot(models.Model): produced_date = models.DateField(auto_now=False) pot_id = models.CharField(max_length=25, blank=False) produser= models.ForeignKey(Produser, on_delete=models.CASCADE) store= models.ForeignKey(Store, on_delete=models.CASCADE) customer = models.ForeignKey(Customer, on_delete=models.CASCADE) I'm struggling to find how to modify the models in the way that one pot to be owned or by producer or a shop or a customer. As right now when a new pot is created store and customers are required fields. -
Multiple checkbox filtering javascript and Django
I am trying to use check boxes in order to filter my results at an html page. Through a django view a pass my data in a list, and then through a loop like this: </div> {% for r in rows %} <div class="res_show"> <div class="btitle"> <a href="/show_business/{{r.b_id}}/"><p style="text-align:left"><b>{{r.name}}</b></p></a> </div> <div class="baddress"> <p>{{r.address}}</p> </div> <div class="reviewstar"> {% if user.is_authenticated %} <a href="/apply_review/{{r.b_id}}/"><p style="text-align:left"><span class="fa fa-star checked"></span> {{r.stars}} ({{r.r_count}})</p></a> {% else %} <p style="text-align:left"><span class="fa fa-star checked"></span> {{r.stars}} ({{r.r_count}})</p> {% endif %} </div> <div class="b_category"> <p style="text-align: left"> <b>{{r.category}}</b></p> </div> <div class="diraddress"> <a href="/show_directions/{{r.id}}/"><p style="text-align:left">{{r.duration}}' {{r.distance}} χλμ <b>{{r.open}}</b></p></a> </div> <hr style="border:3px solid #333333"> </div> i want to use this class for filtering: <div class="b_category"> <p style="text-align: left"> <b>{{r.category}}</b></p> </div> Category has 4 possible values: cafe, bar, nigh_club, reastaurant I have found the following code but i can't figure out what i should change in order to work in my code: http://jsfiddle.net/x1av5809/ Category may contain more tha one values seperated by comma, for example bar or cafe, bar Thanks in advance -
NextJS: Axios is returning 401 error for authenticated pages where JWT tokens are set as HttpOnly cookie after login from django
I have set the access and refresh tokens as HttpOnly cookie from Django backend. In NextJS frontend, the login view works and it shows the HttpOnly cookies are present in the XHR tab. Therefore, it must work for any other authenticated views also. But when I make the request from any other pages with Axios it returns: Error: Request failed with status code 401 As far as I understand, the Django server attaches the httponly cookies to very requests. Therefore, my request to the server must have the cookies in it but the cookies are empty! It works with Postman very well but not in the browser. My NextJS page: export default function profilePage() { return ( <div className={styles.maincontainer}> <p>Hi</p> </div> );}; export async function getServerSideProps() { const url = 'http://127.0.0.1:8000/api/auth/profile/'; const headers = {'Content-Type': 'application/json'}; const resp = await axios.get(url, Headers=headers) const data = resp.json; console.log(data) return {} }; In my login page, the cookies are present in XHR tab: But still if I send request from any authenticated page, it is showing error 401! What is wrong? -
ConnectionTimeoutError AWS elasticsearch using Django elasticsearch-dsl
Not sure what's going on: I am trying to deploy and create the ES index python manage.py search_index --rebuild -f however I am getting this error: ConnectTimeoutError((<urllib3.connection.VerifiedHTTPSConnection object at 0x10e8790a0>, 'Connection to vpc-xxxxx.us-east-2.es.amazonaws.com timed out I have custom TCP listening to port 9200 in my security group. Also in my settings file: ELASTICSEARCH_DSL = { 'default': { 'hosts': https://vpc-xxxxx.us-east-2.es.amazonaws.com, }, } Any help is greatly appreciated. Thanks -
Django overriding default ModelForm error messages not working
I am new to django and I know there are many stack overflow questions and answers related with this topic, but none of the solutions seems to work for me. I am trying to override django's default error messages. I tried these to name a few of the solutions I tried. class MyForm(forms.ModelForm): class Meta: error_messages = { 'first_name': { 'required': _("First name is required."), }, } Also tried this class MyRequest(models.Model): first_name = models.CharField( max_length=254, blank=False, error_messages={ 'blank': 'my required msg..', } ) Is there any thing I need to do on the template side? -
No CSS in Admin panel in Azure App Service hosted Django APP
I cant figure out how to make Azure App Service use css for admin panel. In settings.py I included these: import os BASE_DIR = os.path.dirname(os.path.abspath(__file__)) STATIC_URL = '/static/' STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), ) STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles') But somehow css is still not working for the admin panel. How can I fix this? As far as I know Oryx which works under Azure App Service should automatically run python manage.py collectstatic Any ideas? -
Storing and retrieving default values for fields in a related model instance
I would like to store default values for a model instance in a related object; for example, given this code: class Contract(models.Model): user = models.ForeignKey(User) product = models.ForeignKey(Product) duration = models.IntegerField(null=True, help_text='Contract validity (days)') template = models.ForeignKey(ContractTemplate) class ContractTemplate(models.Model): name = models.CharField(max_length=100) duration = models.IntegerField(help_text='Contract validity (days)') I would like to store objects representing different common durations like: yearly_contract = ContractTemplate.object.create(name='yearly', duration=365) monthly_contract = ContractTemplate.object.create(name='monthly', duration=30) and return the default value from the linked template when the object contract does not specify the value: contract1 = Contract.objects.create(user=foo_user, foo_product, template=monthly_contract) # contract1.duration should return 365 contract2 = Contract.objects.create(user=foo_user, foo_product, duration=45, template=monthly_contract) # contract2.duration should return 45 So, what is the best way to achieve something like this? -
How to show wagtail parent and child pages in graphql?
I am new to wagtail and trying to understand it. I have come across a problem and i need some help. I have setup graphene and graphql. I am able to fetch all the data aswell but I need to fetch the parent page data plus the child page data. I have tried multiple options but nothing is working. This is my models.py from __future__ import unicode_literals from django.db import models from wagtail.images.edit_handlers import ImageChooserPanel from wagtail.admin.edit_handlers import (FieldPanel, StreamFieldPanel, MultiFieldPanel, InlinePanel) from modelcluster.fields import ParentalKey # Create your models here. from wagtail.core.models import Page, Orderable from wagtail.core.fields import RichTextField, StreamField from wagtail.core import blocks as work_blocks from wagtail.search import index class WorkIndexPage(Page): pass class FullStackPage(Page): description = RichTextField(blank=True) def child_pages(self): return TechnologyPage.objects.all().child_of(self) content_panels = Page.content_panels + [ FieldPanel('description', classname="full"), ] subpage_types = ['work.TechnologyPage'] class TechnologyDetailOrderable(Orderable): page = ParentalKey("work.TechnologyPage", related_name="technologypage") software_title = models.CharField(max_length=255) description = RichTextField(blank=True) class TechnologyPage(Page): content_panels = Page.content_panels + [ MultiFieldPanel([ InlinePanel('technologypage', label="Technology Details"), ], heading="Technology or Framework details"), ] This is my schema.py from __future__ import unicode_literals import graphene from graphene_django import DjangoObjectType from about.models import AboutPage from work.models import FullStackPage, TechnologyPage from django.db import models from api import graphene_wagtail class AboutNode(DjangoObjectType): class Meta: model = … -
Hosting django/scrapyd webapp [closed]
Where can I host my django/scrapyd/channels/postgresql web app?, I have no idea which hosting service would do this easily. Thanks. -
Insecure deserialization in python
I am trying to mitigate this piece of code against insecure deserialization, but I am not sure of how to do it... Original code def get_user(self, cookieObject): decode = base64.b64decode(cookieObject.remember_me) user, sign = pickle.loads(decode) # Validate signature if sign == self.sign_user(user): return user My code def get_user(self, cookieObject): creds = [session.username, self.sign_user(session.user)] # Validate signature if cookieObject == base64.b64encode(pickle.dumps(creds)): return user -
(Django query parameter using for loop) Is there a more efficient code?
I'm a novice developer studying Jango. If you look at the last code, the room connected to the accommodation, The options associated with the room were chained to import objects. The problem is, The speed is slow when receiving a value from the request. For your information, I'm taking the option as a query parameter list. It takes more than 5 minutes from 5 values. Is there a more efficient code? Thank you. The code is below. class AccommodationListView(View): def get(self, request, category_id=0): city = request.GET.get('city', None) start_date = request.GET.get('startDate', None) end_date = request.GET.get('endDate', None) guest = request.GET.get('guest', None) ordering = request.GET.get('order', 'favored') rate = request.GET.get('rate', None) room_option = request.GET.getlist('roomOption', None) q = (Q(city__name=city) & Q(room__maximum_capacity__gte=guest) & Q(rate__gte=rate) & ~Q(room__unavailabledate__start_date__gte=start_date, room__unavailabledate__end_date__lte=end_date) ) if category_id: q &= Q(category_id=category_id) accommodations = Accommodation.objects\ .select_related( 'address', 'category', 'city', 'host',)\ .prefetch_related( 'accommodationimage_set', 'room_set', 'review_set', 'room_set', 'room_set__option')\ .filter(q)\ .annotate(count=Count('review'), price=Min('room__price')).distinct() if room_option: for r in room_option: accommodations = accommodations.filter(room__option__name=r)