Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to store datasets and databases from quandl using quandl Python API/module in postures or mongodb?
Does anybody know how to use quandl module/API for Python to store datasets and databases into a single database using postgresql or mongodb. Any help will be much appreciated. Thanks. -
How do we reference one foreign key with multiple tables?
I am trying access two tables with one foreign key. from table1.models import tab1 from table2.models import tab2 class Case(models.Model): table = models.ForeignKey(tab1,tab2, blank=True,null=True ,on_delete=models.SET_NULL) -
Django QuerySet selecting rows according a 'valid_from' column
I have a Django model with date dependent product prices similar to this: Class ProductPrice(models.Model): product = models.ForeignKey(Product, related_name='prices') valid_from = models.DateField() price = models.DecimalField(max_digits=7, decimal_places=2) The 'valid_from' dates can be past and future. If I would need the valid price for a single product on 'some_date' it would be simple: price = product.prices.filter(valid_from__lte=some_date).latest('valid_from') However, I need a list with all prices for a given date. How can I get a QS that selects the valid price for all (or a set of) products für a certain date? -
Csrf verification failed - Django Rest and Backbone.js
I have started going through "Lightweight Django" https://github.com/lightweightdjango to learn more about Django and Client-Side JavaScript. During testing out the LoginView created using Backbone.js I get the "Forbidden(403) CSRF verification failed.Request aborted." message, as pointed out in this post: CSRF verification failing in django/backbone.js . First of all I thought of inserting "{% csrf_token %}" template tag in the form but when I do this the server is giving me a "POST / HTTP/1.1" 405 0 - Method Not Allowed (POST) : /" message. Since the AJAX "X-CSRFToken" request header is being set using $.ajaxPrefilter(), I can't figure out what the problem is. When I am using httpie to perform POST requests using the superuser details, everything works just fine as in the following example: HTTP/1.0 200 OK Allow: POST, OPTIONS Content-Type: application/json Date: Mon, 11 Sep 2017 13:49:49 GMT Server: WSGIServer/0.2 CPython/3.6.2 Vary: Cookie X-Frame-Options: SAMEORIGIN { "token" : some_value } Making use of the console from the "Inspect Element"feature I get the following messages: Response headers: Allow: GET, HEAD, OPTIONS Content-Length: 0 Content-Type: text/html; charset=utf-8 Date: Mon, 11 Sep 2017 14:03:06 GMT Server: WSGIServer/0.2 CPython/3.6.2 X-Frame-Options: SAMEORIGIN Request headers: Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: en-US,en;q=0.5 Connection: keep-alive … -
Django TemplateView and perms in context
Django 1.11.4 I use TemplateView. I tried to check permissions in the template. Like this: https://docs.djangoproject.com/en/1.11/topics/auth/default/#permissions But there is no variable {{ perms }} in the template. I checked in the context: def get_context_data(self, **kwargs): context = super(PersonalAreaView, self).get_context_data(**kwargs) No, there is no perms. Could you tell me why there is no perms in TemplateView context? Or have I done something wrongly? -
Model is not updated
I want to put data into an excel worksheet. I wrote the following code: data_dict ={} data_dict =defaultdict(dict) def try_to_int(arg): try: return int(arg) except: return arg def main(): book4 = xlrd.open_workbook('./data/excel1.xlsx') sheet4 = book4.sheet_by_index(0) tag_list = sheet4.row_values(0)[1:] for row_index in range(1, sheet4.nrows): row = sheet4.row_values(row_index)[1:] row = list(map(try_to_int, row)) value = dict(zip(tag_list, row)) data_dict[value['age']].update(value) user2 = User.objects.filter(name=data_dict['name']) print(user2) if user2: if data_dict['name'] == 'Tom': user2.update(close_rate_under_300_ny = data_dict['300'], close_rate_under_700_ny = data_dict['d700'], close_rate_upper_700_ny = data_dictt['u700']) elif data_dict['name'] == 'John': user2.update(close_rate_under_300_eu = data_dict['300'], close_rate_under_700_eu = data_dict['d700'], close_rate_upper_700_eu = data_dict['u700']) elif data_dict['name'] == 'Blear': user2.update(close_rate_under_300_uk = data_dict['300'], close_rate_under_700_uk = data_dict['d700'], close_rate_upper_700_uk = data_dict['u700']) else: user2.update(close_rate_under_300_ch=data_dict['300'], close_rate_under_700_ch = data_dict['d700'], close_rate_upper_700_ch = data_dict['u700']) main() In print(user2), many <QuerySet []> were shown. So I think maybe user2 cannot be gotten. Furthermore, in if-else statement of if data_dict['name'] == 'Tom':,user2 is not updated. What is wrong in my code? By the way, data_dict is gotten normally. -
How to do reqursion function for views?
I have Categories and Sub-categories. I want do take all products from Category(and Sub-categories which belong this Category), and show for user. models.py class Category(models.Model): title_of_category = models.CharField(max_length=200, verbose_name='Підкатегорія', null=True) parent = models.ForeignKey('self', blank=True, null=True, verbose_name='Категорія') def __str__(self): return self.title_of_category class Meta: verbose_name = "Категорія" verbose_name_plural = "Категорії" -
Django Swagger and JSON API render issues
Using django-rest-framework-json-api I am able to create API end points that work as described within the documentation. Attempted to provide API documentation using django-rest-swagger is not so easy. 1: Swagger uses media_type = 'application/json' which is not supported in the JSON API. To get around this I created a second render class (renderer_classes) that sub classes the JSON API JSONRenderer and forces the media type. Now the end point supports application/json and application/vnd.api+json and swagger is happy to render in JSON API document structure. Aware that the generated curl requests have none a standard JSON API header. 2: Swagger has the same issue with the parser. While the work out from issue 1 does work there is a secondary challenge. Swagger renders a flat dictionary of field names which is not JSON API and ultimately is requested by DRF. Is it possible to get swagger to parse in JSON API? At the moment Swagger is not working for PUT or POST. djangorestframework-jsonapi==2.2.0 djangorestframework==3.5.4 Django==1.11.2 coreapi==2.3.1 python 3.6 -
DRF: Let serializer filter related objects
I have the three models listed below. When I serialize media objects, owners are listed just like they should. The trough-table holds an is_accepted value, and what I would like is for the media serializer to only list owners that have acepter their media. Any ideas on how to achieve this? Models: class Media(models.Model): genre = models.ForeignKey(Genre, blank=True, null=True, db_index=True) owners = models.ManyToManyField(User, through='Usermedia', related_name='owners') title = models.CharField(max_length=45, blank=True) created = models.DateTimeField(auto_now_add=True) class Usermedia(models.Model): user = models.ForeignKey(User, on_delete=models.DO_NOTHING, related_name='usermedia') media = models.ForeignKey(Media, on_delete=models.CASCADE, related_name='ownership') is_active = models.BooleanField(default=0) is_accepted = models.BooleanField(default=1) class User(AbstractBaseUser, PermissionsMixin): email=models.CharField(unique=True, max_length=255) Serializer class UserSerializer(serializers.ModelSerializer): email = serializers.EmailField( validators=[validators.UniqueValidator(queryset=User.objects.all())] ) class MediaSerializer(serializers.ModelSerializer): owners = UserSerializer(many=True, required=False) created = serializers.DateTimeField(read_only=True) genre = serializers.SlugRelatedField(required=True, slug_field='name', queryset=Genre.objects.all()) Thank you for taking your time to look at this -
Edit the admin panel using Custom user authentication
Using the documentation I am trying to create a custom authentication model in order to be able to use only Email and password to authenticate a user. Despite I manage to do it, I am having trouble to edit the Admin panel and in particular the Edit form could you please help me to add the possible editable field. my code is : admin.py: from django import forms from django.contrib import admin from django.contrib.auth.models import Group from django.contrib.auth.admin import UserAdmin as BaseUserAdmin from django.contrib.auth.forms import ReadOnlyPasswordHashField from registration.models import MyUser class UserCreationForm(forms.ModelForm): """A form for creating new users. Includes all the required fields, plus a repeated password.""" password1 = forms.CharField(label='Password', widget=forms.PasswordInput) password2 = forms.CharField(label='Password confirmation', widget=forms.PasswordInput) class Meta: model = MyUser fields = ('email','is_active','is_hr','is_candidate','is_employee','company','first_name','last_name') def clean_password2(self): # Check that the two password entries match password1 = self.cleaned_data.get("password1") password2 = self.cleaned_data.get("password2") if password1 and password2 and password1 != password2: raise forms.ValidationError("Passwords don't match") return password2 def save(self, commit=True): # Save the provided password in hashed format user = super().save(commit=False) user.set_password(self.cleaned_data["password1"]) if commit: user.save() return user class UserChangeForm(forms.ModelForm): """A form for updating users. Includes all the fields on the user, but replaces the password field with admin's password hash display field. """ … -
Differentiate Between test and runserver
Continuing from my previous question-: Modifying CharacterSet Collations in Django For which I did not get a solution. I need to 3 SQL queries for my migrations to work properly. But when I run tests, I only need 2 of the sql queries. Is there a way to identify if tests are running in migrations files(since they are python files.) In the operations array, I would have only 2 arrays if tests, else 3. Need to do something like this. -
Switch view on same URL but different method in django with rest framework?
What is the most "pythonic" way to handle view routing on same url based on method? i don't like the solution if(request.method == 'GET'): ....... is there a better way? -
Django runserver not working after installing redis for django channels
I am learning the Django-channels concepts and I am stuck when after install the asgi_redis and the redis server. If I have the CHANNEL_LAYERS configured for the in-memory backend. The server runs okay, and the web page is displayed. However, if I run the server after configuring the CHANNEL_LAYERS for the redis backed, the web page is not displayed, and I get an error in the terminal: Channel layer default (asgi_redis.core.RedisChannelLayer) Quit the server with CONTROL-C. 2017-09-11 12:34:07,259 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive 2017-09-11 12:34:07,260 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive 2017-09-11 12:34:07,261 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive 2017-09-11 12:34:07,263 - INFO - server - HTTP/2 support not enabled (install the http2 and tls Twisted extras) 2017-09-11 12:34:07,263 - INFO - server - Using busy-loop synchronous mode on channel layer 2017-09-11 12:34:07,265 - INFO - server - Listening on endpoint tcp:port=8000:interface=127.0.0.1 2017-09-11 12:34:07,267 - ERROR - server - Error trying to receive messages: unknown command 'EVALSHA' 2017-09-11 12:34:07,268 - INFO - worker - Listening on channels http.request, websocket.connect, websocket.disconnect, websocket.receive 2017-09-11 12:34:12,270 - ERROR - server - Error trying to … -
How to add this logging view to function based view drf-tracking
How to add this logging view to function based view in django-rest-framework from rest_framework import generics from rest_framework.response import Response from rest_framework_tracking.mixins import LoggingMixin class LoggingView(LoggingMixin, generics.GenericAPIView): def get(self, request): return Response('with logging') -
Does it cost to use PostgreSQL?
I'm writting a web app with Python Django. Generally it doesn't matter which DB is on the backend as Django abstracts DB interaction, but there are one or two features that Django only offers if PostgreSQL is used. My newbie question - is there a cost somewhere along the line to utilise PostgreSQL? If I host with Heroku it requires me to use PostgreSQL anyway. Do I present potential future compatibility issues or a financial hurdle to others who might use my code if I move forward and begin to depend on my project being based on PostgreSQL? -
Django maximum recursion depth exceeded in setattr
class MyModel(Model): is_valid = BooleanField(default=False) def __setattr__(self, key, value): if key == 'is_valid': print(self.is_valid) super(MyModel, self).__setattr__(key, value) Why do I get maximum recursion error when accessing self.is_valid? Froms tack trace I see that self.is_valid triggers instance.refresh_from_db(fields=[self.field_name]) which triggers __setattr__. -
Browse local directory and files in a Django View
I'm developing a Django project and I have the following "problem". I have a local directory that has subdirectories, in which I got some PDF files. For example: Main Dir: |-->2000 ____|-->A ________|-->file1.pdf ________|-->file2.pdf ____|-->B ________|-->file3.pdf ____|-->C ________|-->... ____|-->D ________|-->... |-->2001 ___|--> ... |-->2002 ___|--> ... All the folders contain thousands of PDF files. I want to display this directory in a Django view and let the user browse it by clicking the subdirectories and the PDF files so he can view them in his browser and maybe even add a "Download PDF" button. I also want to format it a bit, maybe add a search function too in the future if possible. It is my first time working with local files and Django so I'm a bit lost. Thank you for your time! -
How to excute django LogoutView?
The django auth Logout view: class LogoutView(SuccessURLAllowedHostsMixin, TemplateView): """ Logs out the user and displays 'You are logged out' message. """ next_page = None redirect_field_name = REDIRECT_FIELD_NAME template_name = 'registration/logged_out.html' extra_context = None @method_decorator(never_cache) def dispatch(self, request, *args, **kwargs): auth_logout(request) next_page = self.get_next_page() if next_page: # Redirect to this page until the session has been cleared. return HttpResponseRedirect(next_page) return super(LogoutView, self).dispatch(request, *args, **kwargs) def post(self, request, *args, **kwargs): """Logout may be done via POST.""" return self.get(request, *args, **kwargs) I do not know,How does it work,when visited it.It excute post first? -
(AWS S3) No module named 'storages.backends'; 'storages' is not a package
I'm getting this import error while I'm setting AWS S3 in Django. When I do 'python3 manage.py collectstatic', File "/home/jaemin/Desktop/Stylee/workspace/Stylee-API/stylee/stylee/utils.py", line 1, in <module> from storages.backends.s3boto import S3BotoStorage ImportError: No module named 'storages.backends'; 'storages' is not a package And this is the utils.py file location. stylee/stylee/utils.py stylee/stylee/settings/locals.py 1) My setting/local.py INSTALLED_APPS = [ ... 'storages', ] 2) Commands to install 'storages' pip3 install boto django-storages-redux 3) python3 manage.py migrate Output Operations to perform: Apply all migrations: account, admin, auth, authtoken, contenttypes, oauth2_provider, profiles, sessions, sites, social_django, socialaccount Running migrations: No migrations to apply. -
missing wsdl:service definitions in the WSDL
I'm trying to use ZEEP to do SOAP requests. I got this request: def status(request, IP): URL = "http://" + IP + "/Service" session = Session() session.auth = HTTPBasicAuth('username', 'password') client = Client(URL, transport=Transport(session=session), strict=False) response = client.service.Method1(request={'getList'}) return response But I'm hitting the error missing wsdl:service definitions in the WSDL. I'm stuck and can't find any more way to fault search. Got any ideas? -
CSV upload bulk insert
I am using django framework. I need to upload a csv file containing user's data and save it in two tables. The username field is unique. The file is having atleast 1000 rows. I have to make sure that username field is unique before adding to database. I don't want to generate 1000 queries for checking 1000 rows. Currently I am inserting it blindly, without cheking uniqueness which generates issue. I am trying to insert it using transaction. But is not reverting if any issue occurs. I am giving my code below @admin_required @transaction.commit_manually def import_user_csv(request): if request.method == 'POST' and request.FILES.has_key('csv_file'): csv_file = request.FILES['csv_file'] now = datetime.datetime.now() for i, row in enumerate(csv.reader(csv_file, delimiter=',')): if i == 0: continue else: createduser, created = User.objects.get_or_create( username=row[0], name=row[1] ) _, userrole = UserRole.objects.get_or_create( user_id = createduser.id, role = 'employee', updated_time = now ) transaction.commit() I have mainly 2 doubts. How to validate username How to insert using bulk insert as the userrole table depends on the user table -
Create a separate user type
I am working on an intranet web application which needs two types of users. Normal users that can be setup from django admin and specific type of users - Employees. I have the following model for Employee type user. class Employee(models.Model): emp_name = models.CharField(max_length=500) slug = models.SlugField(unique=True, default='') location = models.CharField(max_length=200) email = models.EmailField() experience = models.TextField(blank=True) primary_skill = models.ManyToManyField(PrimarySkill) secondary_skill = models.ManyToManyField(SecondarySkill) I tried having a OneToOneField like this as per the official doc and this article: user = models.OneToOneField(User, blank=True, null=True, on_delete=models.CASCADE) @receiver(post_save, sender=User) def create_employee(sender, instance, created, **kwargs): if created: Employee.objects.create(user=instance) @receiver(post_save, sender=User) def save_employee(sender, instance, **kwargs): instance.employee.save() I realized that this is the opposite of what I want. Every time a User is created from the admin, there was an entry created in the app_employee table. What I want is this: Every time an Employee is created, I need a User created. An Employee can be created using a separate signup form, say emp_signup How do I approach this type of scenario? -
Python IndexError: list index out of range in cleaning up a file using CSV module
I have to cleanup some files before doing the import into the Django's DB. I have an error as per the title saying that the index is out of range. My files have many rows, some of them have 10.000 rws or others have more. Traceback (most recent call last): File "/Users/cohen/my-python-project/venv/ofac/ofac_project/ofac_sdn/import_save/cleanup_sdn.py", line 9, in <module> replaced7 = row[7].replace('-0-', newinteger) IndexError: list index out of range How can i fix this error? Despite the error pops up, the first file is cleaned up, but the rest of them are not. Thank you in advance! Please find below my code: import csv, os, sys newstring = "null" newinteger = str(0) with open('sdn.csv', 'r') as file1, open('new_sdn.csv', 'w', newline='') as file2: reader = csv.reader(file1, delimiter=',') writer = csv.writer(file2, delimiter=',') for row in reader: replaced7 = row[7].replace('-0-', newinteger) row[7]=replaced7 replaced8 = row[8].replace('-0-', newinteger) row[8]=replaced8 replaced2 = row[2].replace('-0-', newstring) row[2] = replaced2 replaced4 = row[4].replace('-0-', newstring) row[4] = replaced4 replaced5 = row[5].replace('-0-', newstring) row[5] = replaced5 replaced6 = row[6].replace('-0-', newstring) row[6] = replaced6 replaced9 = row[9].replace('-0-', newstring) row[9] = replaced9 replaced10 = row[10].replace('-0-', newstring) row[10] = replaced10 replaced11 = row[11].replace('-0-', newstring) row[11] = replaced11 writer.writerow(row) with open('add.csv', 'r') as file3, open('new_add.csv', 'w', newline='') as … -
Modifying CharacterSet Collations in Django
I am adding a new model to my existing application. The model has a One-to-One relationship to a field which has collation utf8_bin. While Django creates a the model using utf8_unicode_ci. The previous model was edited during the application initialization in DB Level, but now if I want to migrate and edit it , it doesn't seem to be right. More so over,because when django migrates. It does Create Table Alter Table - Add FK --> This fails because of collation mismatch. Can I somehow edit the migration, so that I can run a sql query to modify the collation between create table and alter table? or maybe add a default collation to the model? I do not want to edit the whole database's default collation. -
Log when user access to endpoint
I want to create a custom CBV so I can log everytime an user access to any endpoint which use this custom CBV. I've tried with something like this: class LogUserAccess(APIView): def dispatch(self, request, *args, **kwargs): logger.info('User {} accessing to {} ({})'.format(request.user, resolve(request.path).app_name, self.__class__.__name__)) super(LogUserAccess, self).dispatch(request, *args, **kwargs) ... but doesn't recognize the logger instance. What would do the best approach to accomplish this?