Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Posting nested data in DjangoRestFramework
I am trying to post to a many to many field on a model. The below code POSTS (gives a 200 OK response) but the data ("text") never gets posted. I've used a PDB trace, and info_data does have the right data. FYI I had to use self.initial data as the validate data was striping away the PK field. Models.py: class Info(models.Model): text = models.CharField("HTML/Text String", max_length=50000, blank=True) file = models.FileField(upload_to='uploads/', blank=True) class Movie(models.Model): info = models.ManyToManyField('Info', blank=True) Serializer: class InfoSerializer(serializers.ModelSerializer): class Meta: model = Info fields = ('text', 'pk') class EditorSerializer(serializers.ModelSerializer): info = InfoSerializer(many=True, read_only=False) class Meta: model = Movie fields = ('info',) def update(self, instance, initial_data): infos_data = self.initial_data.pop('info') for info_data in infos_data: info_qs = Info.objects.filter(pk=info_data['pk']) if info_qs.exists(): info = info_qs.first() else: info = Info.objects.create(**info_data) instance.info.add(info) # import pdb; pdb.set_trace() return instance Views.py class EditorViewSet(viewsets.ModelViewSet): queryset = Task.objects.all() serializer_class = EditorSerializer The data I'm posting looks like this: {"info":[{"text":"Test POST", "pk":2}]} I'm posting it the the PK of the MOVIE model: example.com/api/Editor/123 -
Django 1.9 - Change in model choices not working
So for a model in Django 1.9 I have different choices: NATIONAL = 0 REGIONAL = 1 LOCAL = 2 NA = 99 SCOPE_CHOICES = ( (NA, 'No Report/Not Determined'), (NATIONAL, 'National'), (REGIONAL, 'Regional'), (LOCAL, 'Local'), ) scope = models.IntegerField(blank=True, choices=SCOPE_CHOICES, default=NA) This works really well so far. However, when I changed NA to "Domestic public or private institution", this is not reflected in the views. Any idea why that might be? -
Why pip does not check python version?
I'm running django tests on a large application. They require a test database, so I need to create a test database before running tests. But the problem is - because of some dependency problems the migration must be run in the following order: migrate app2, migrate app1, migrate. I know that I'm doing it wrong but I have no will and time to fix hundreds of migrations. The question is: how can I specify custom migration order to create test databases, that I would later use for each test run via --keep-db option? -
Django inheritance and parent object related name
I'm upgrading a project from django 1.8 to 1.10 and it looks like django has improved the check of eventual name collision between foreign keys and model inheritance. This is obviously a good thing, but the projet I need to upgrade is a big one and it would be a hell to rename a model. Let me explain the problem : I have a base class called Parent and many children which are linked together, like so : class Parent(models.Model): title = models.CharField(max_length=10) class ChildA(Parent): description = models.TextField() class ChildB(Parent): description = models.TextField() childa = models.ForeignKey(ChildA) The clash here is that a childb object has 2 "childa" attributes : The "childa" ForeignKey The instance inherited by the ChildA model (because childb has also the parent attributes). The 2 obvious solutions here are : Rename the ForeignKey ChildB.childa to ChildB.somethingelse Rename the ChildA model to something else. Both solutions costs a lot and will probably introduce new bugs. So I wondered : Is it possible to rename the reverse related name of the inherited object ? For example : p = Parent.objects.get(pk=1) print p.childa_child # Hit the ChildA instance I have no idea if I'm clear enough but I'll keep this … -
Django form class never appear in template
View function to represent the form, never appear in template. This is my from class from django import forms class SearchForm(forms.Form): search_compNum = forms.CharField(label='Compound_Number', max_length=10) search_formula = forms.CharField(label='Molecule_Formula') search_format = forms.CharField(label='InChI or Smiles', widget=forms.Textarea) This is the view function. from django.shortcuts import render from .forms import SearchForm def get_query(request): """ molecule search form. """ form_class = SearchForm if request.method == 'POST': form = form_class(data=request.POST) if form.is_valid(): Query_Compound_Number = request.POST.get('search_compNum', '') else: form_class = SearchForm() return render(request, 'search.html', {'form': form_class}) This is a template. {% extends "index.html" %} {% block content %} <form action="{% url 'chemdb:results' %}" method="post"> {% csrf_token %} {{ form }} <input type="submit" value="Search"/> </form> {% endblock %} And the urls.py from django.conf.urls import url from . import views urlpatterns = [ url(r'^index/$', views.index, name='index'), url(r'^search/$', views.SettingView.as_view(), name='search'), url(r'^search/$', views.DataBaseView.as_view(), name='search'), url(r'^search/$', views.SortView.as_view(), name='search'), url(r'^search/$', views.TypeView.as_view(), name='search'), url(r'^search/$', views.FilterView.as_view(), name='search'), url(r'^search/$', views.get_query), url(r'^results/$', views.result, name='results') ] And I'm using Django 1.8.x version. So, I don't need to import 'pattern'. Help me plz!!!! -
Django Fileupload error
class BoardFiles(models.Model): index = models.CharField(max_length=50, blank=True) file = models.FileField(upload_to='static-assets/') <form method="post" action="/board/update/" enctype="multipart/form-data"> <input type="file" name="file" id="id_file" multiple/> </form> @csrf_exempt def board_update(request): for file in request.FILES.getlist('file'): fl = BoardFiles (index=request.POST['id'], file=file) fl.save() But, When I upload files, There are no datas on BoardFiles table. What's the problem? -
Django-registration-redux: get_success_url() takes 1 positional argument but 2 were given
New to Django. Stacktrace: TypeError at /accounts/register/ get_success_url() takes 1 positional argument but 2 were given Request Method: POST Request URL: http://127.0.0.1:8000/accounts/register/ Django Version: 1.10.2 Exception Type: TypeError Exception Value: get_success_url() takes 1 positional argument but 2 were given Exception Location: g:\Python\lib\site-packages\registration\views.py in form_valid, line 37 Python Executable: g:\Python\python.exe Python Version: 3.5.2 Python Path: ['g:\\Git\\Jam', 'g:\\Python\\python35.zip', 'g:\\Python\\DLLs', 'g:\\Python\\lib', 'g:\\Python', 'g:\\Python\\lib\\site-packages'] Server time: Mon, 31 Oct 2016 13:13:15 +0000 I've been following Tango with Django so I'm surprised this error is happening. I checked this answer and changed my RegistrationView so it now looks like this: class MyRegistrationView(RegistrationView): success_url = 'home' However I am still getting the same error. The user gets registered, but it shows the above stack trace. Urls.py: urlpatterns = [ url(r'^accounts/', include('registration.backends.simple.urls')), url(r'^accounts/register/$', MyRegistrationView.as_view(), name="registration_register"), url(r'^accounts/password/change/$', MyRegistrationView.as_view(), name="auth_password_change"), url(r'^accounts/password/change/done/$', MyRegistrationView.as_view(), name="auth_password_changed"), ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) Registration form: {% extends "base.html" %} {% load staticfiles %} {% block body_block %} <link href="{% static 'css/signin.css' %}" rel="stylesheet"> <div class="container"> <div class="jumbotron"> <h1 class="display-3" align="center">Sign Up Here</h1> </div> <form method="POST" action="."> {% csrf_token %} {{ form.as_p }} <input type="submit" class="btn btn-default" value="Submit"/> </form> </div> {% endblock %} Yes, I have migrated. -
Django-rest-framework-jwt won't return JWToken for nonstaff accounts (django admin error?)
So I've created a register page that allows visitors to register an account on my website. These accounts have no staff status or administrative privileges. I've also created a login page that takes the username and password and sends an ajax post request to an auth url. The url links to obtain_jwt_token (django-rest-framework-jwt's view) which checks the username and password and then returns a jwt token to the visitor's localstorage. This is all fine and dandy, and it works well. Only problem is... well it works only for administrator accounts. For some reason the accounts with no staff status aren't validated. Json Web Tokens aren't returned for these accounts. Is this an issue with django.admin.auth? or is it an issue with drt-jwt? Is drt-jwt using the django admin page to authenticate users? Because that's not what I want. I don't just want admins to be able to log in to my website. -
rest framework multiple unique querysets
I have a facets method which takes a search queryset what's the best way to get both of these outputs serialized in the django rest framework? # This only works for results class SearchResultsSerializer(serializers.BaseSerializer): def to_representation(self, obj): return { 'a': obj.a, 'b': obj.b } Goal Output: { "count": 0, "next": "http://localhost:8000/q=Cupertino", "previous": null, "results": [] "facets": [] # <--- can't figure out how to get this } -
How to import headerless csv into postgresql with django_postgres_copy?
I have a massive csv file to import into Postgres and my django model is all done, my trouble is that the csv file doesn't have any headers that i can map to, and I'm trying to use postgres_copy http://django-postgres-copy.readthedocs.io/en/latest/ to do this for me, but I can't find a way to do it without headers. '123131','data','data','d','d','123112','d' That's how my csv looks like. And I have like 5 million lines. If there is other methods I'm open to them as well. from .models import MyModel from postgres_copy import CopyMapping from django.core.management.base import BaseCommand import os class DataToPostGres(BaseCommand): BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) DATA_ROOT = os.path.join(BASE_DIR, 'data/bigcsv.csv') def handle(self, *args, **kwargs): c = CopyMapping( # Give it the model MyModel, # The path to your CSV DATA_ROOT, # And a dict mapping the model fields to CSV headers dict(name='NAME', number='NUMBER', dt='DATE') ) # Then save it. c.save() Here is what I have so far but it can obviously not work cause I can't map my model fields to any CSV headers. I looked around but I couldn't find anything that answers my question so far. Thank you in advance. -
Django - custom decorator - parameter unfilled
I'm trying to create an own decorator which checks if the user meet some condition (for example user.is_authenticated) and if this condition doesn't hold, they are going to be redirected to another page (second parameter). The problem is that the decorator doesn't work because it wants another parameter (the view function). from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse def condition_or_redirect(view_func, condition, redirect_to): def wrapped(request, *args, **kwargs): if not condition(request.user): return HttpResponseRedirect(reverse(redirect_to)) else: return view_func(request, *args, **kwargs) return wrapped Then I want to use it: @condition_or_redirect(lambda x: not x.is_authenticated,'homepage') def some_view.... What is wrong with the code? I know that it want's the first parameter which is a view_func but I thought that it is added by decorator itself. -
Python - match/compare two data/tuple lists
I have two sets of data, one is from a queryset in django, which will return data as below sr_data = ShowroomConfigData.objects.only('location').filter(is_showroom=True).exclude(loc ation='MajorSite') for i in sr_data: print i.location London Glasgow Edinbrugh ... The second set of data is from an external Mysql query that returns a list of tuples: query = """long mysql query...""" cur.execute(query) esr_data = cur.fetchall() for i in esr_data: print i[3] London Glasgow Edinburgh ... they're not necessarily in that order either, the orders of both are random i think. but the external query has the some details i want to import into django on a regular basis. So i need to loop both lists and import data into django when they match, the only problem is, looping they will likley never match. Does anyone know of a way i can make this work? Thanks -
How to populate django form from model property?
I have model which among other fields contains price property which is calculated dynamically. I want to display this property in model admin page. So I've created custom ModelForm: class ShipmentForm(forms.ModelForm): price = forms.IntegerField() class Meta: model = models.Shipment fields = [ 'title', 'price', ] However I can't get price value in that form. Here's how I change form in admin panel: class ShipmentAdmin(admin.ModelAdmin): form = ShipmentForm -
How can I delete a table record comparing datetime to current time in view
I need to delete a table record whenever tables record's timestamp or datetime matches current datetime in db. Problem is I cant get datetime values on console without using datetimes method if i use it I can't filter it . here is my code def query(request): xx = datetime.datetime.now() if dulesdb.objects.all(): zzz = dulesdb.objects.datetimes('request_time','second') yyy = dulesdb.objects.filter(zzz > datetime.datetime.now()) print yyy.delete() # some trick this way return HttpResponse('deleted') -
too many SQL variables sqlite3
I'm developing a project with django haystack and sqlite3. When the result of a query is high I get this error: OperationalError too many SQL variables I think It's because of a default sqlite3 setting SQLITE_LIMIT_VARIABLE_NUMBER = 999 Ho can I change this setting? -
Django Nginx uWSGI 502 Bad Gateway always
I have been following the http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html and went up to the http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html#running-the-django-application-with-uwsgi-and-nginx and started the uwsgi --socket mysite.sock --module mysite.wsgi --chmod-socket=664 it is started but when i load my webpage on browser getting 502 Bad Gateway Django version 1.10.2 error is error] 25444#25444: *1 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: mytest.com, request: "GET / HTTP/1.1", upstream: "uwsgi://127.0.0.1:8001", host: "127.0.0.1:8000" What could be the issue ? # configuration of the server server { # the port your site will be served on listen 8000; # the domain name it will serve for server_name mytest.com; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 175M; # adjust to taste # Django media location /media { alias /home/karesh/tutorial/uwsgi-tutorial/mysite/media; # your Django project's media files - amend as required } location /static { alias /home/karesh/tutorial/uwsgi-tutorial/mysite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass unix:/home/karesh/tutorial/uwsgi-tutorial/mysite/mysite.sock; include /home/karesh/tutorial/uwsgi-tutorial/mysite/uwsgi_params; # the uwsgi_params file you installed } } -
Angular2 Form Dynamic control mapping to input fields without ngFor
I'm trying to add Angular2 on top of Django generated formset with ability to add new rows in frontend with multiple input fields. I'm willing to stick to Django field names for easier mapping to django forms and server validation, but angular2 should manage all frontend validations and complex multi-input-field calculations. I cannot use ngFor because django could already have generated multiple rows and user can add more or remove existing rows and the input field names differ in every row. I created TS/JS that add new controls to Angular2 form (created using FormBuilder) and creates new input fields with correct names in DOM, but how can I map those fields together? Currently Angular2 doesn't know that I have manipulated DOM and doesn't update newly created Controls. -
Python: How to inherit and override a method having a queryset?
I have 2classes A and B : class A(ListView): model=Model_name def get_queryset(self): instance = self.get_user_by_credential(...) if instance: model_instance = self.model.objects.get(id=instance.id) some_objects= self.Model_name.objects.filter(content_id=5, object_id=model_instance.id) return some_objects return [] class B(A,UserObject): model=B_model param='foo' def get_queryset(self): instance = self.get_user_by_credential(...) if instance: model_instance = self.model.objects.get(id=instance.id, param=param) some_objects= Model_name.objects.filter(content_id=5, object_id=model_instance.id) return some_objects return [] the difference between the 2 classes is only the param in the queryset of B How can I override the method of the parent class A and add a parameter to the queryset of the chold class B ? -
Test image upload in Django rest framework
I am trying to test image upload in DRF. conftest.py import os import pytest DIR_PATH = os.path.dirname(os.path.realpath(__file__)) @pytest.fixture def test_image() -> bytes: with open(os.path.join(DIR_PATH, 'test_image.jpg'), 'rb') as f: yield f test_views.py import uuid import pytest from django.core.urlresolvers import reverse from django.contrib.auth.models import User from django.test import Client from rest_framework.authtoken.models import Token from userprofile.models import UserProfile ... def test_add_photo(self, client: Client, profile: UserProfile, test_image): assert not profile.profile_picture url = self.get_url(profile) response = client.patch(url, content_type='multipart/form-data', data={'profile_picture': test_image}, **auth_headers(profile.user)) assert response.status_code == 200 profile = UserProfile.objects.get(pk=profile.pk) assert profile.profile_picture view.py from rest_framework import viewsets from rest_framework import mixins from rest_framework.parsers import MultiPartParser, FormParser from userprofile.serializers import UserProfileSerializer, SocialSerializer, BiographySerializer from .models import Social, Biography, UserProfile from rest_framework.response import Response from rest_framework.permissions import BasePermission, IsAuthenticated class IsOwnerOrAdmin(BasePermission): def has_object_permission(self, request, view, obj): return request.user.is_superuser or request.user == obj.user class UserProfileViewSet(mixins.RetrieveModelMixin, mixins.CreateModelMixin, mixins.UpdateModelMixin, mixins.DestroyModelMixin, viewsets.GenericViewSet): permission_classes = (IsAuthenticated, IsOwnerOrAdmin) queryset = UserProfile.objects.all() serializer_class = UserProfileSerializer parser_classes = (MultiPartParser, FormParser) lookup_field = 'firebaseUID' def check_profile_permissions(self, request, **kwargs): firebaseUID = kwargs.get('firebaseUID', None) user_profile = UserProfile.objects.get(firebaseUID=firebaseUID) return super().check_object_permissions(request, user_profile) def update(self, request, *args, **kwargs): self.check_profile_permissions(request, **kwargs) kwargs['partial'] = True return super().update(request, *args, **kwargs) serializers.py class UserProfileSerializer(serializers.ModelSerializer): class Meta: model = UserProfile fields = ('id', 'user', 'firebaseUID', 'profile_picture') models.py class … -
Setting up a server to host multiple domains using django, virtualenv, gunicorn and nginx
I am setting up a new server machine, which will host multiple django websites. I must point out that I own (developed and are in absolute control of) all websites that will be run on the server. I am pretty certain that ALL of the websites will be using the same version of: django gunicorn nginx postgreSQL and psycopg2 (all though some websites will be using geospatial and other extensions) The only thing that I know will differ between the django applications are: python modules used (which may have implications for version of python required) I can understand using virtualenv to manage instances of where a project has specific python modules (or even python version requirements), but it seems pretty wasteful to me (in terms of resources), to have each project (via virtualenv), to have separate installations of django, nginx, gunicorn ... etc. My question then is this: Is it 'acceptable' (or considered best practice in scenarios such as that outlined above) to globally install django, gunicorn, nginx, postgreSQL and psycopg2 and simply use virtualenv to manage only the parts (e.g. python modules/versions) that differ between projects?. Note: In this scenario there'll be one nginx server handling multiple domains. Last … -
403(Forbidden Error) while sending array of JSON objects to Django using ajax POST method
I am getting 403(Forbidden Error) while sending array of JSON objects to Django View using ajax POST method. I have already included csrf token but still it is giving the same error. I am making 2 different ajax calls on different events and one of them is working fine while the other one gives 403 error. Here is my code : This POST request works fine. $(document).on('submit','#dataset', function (e) { e.preventDefault(); $.ajax({ type:'POST', url:'/savedataset/', data:{ lat:$('#lat').val(), long:$('#long').val(), csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val() }, success:function (data) { alert(data); }, error:function () { alert("Error") } }) }); Here is view.py for the above request : def savedata(request): if request.method == 'POST': lat = request.POST['lat'] long = request.POST['long'] nsrpt.objects.create( latitude=lat, longitude=long ) response_data = {"lat": lat, "long": long} return HttpResponse(json.dumps(response_data), content_type="application/json") but this one returns the 403 error : $(document).ready(function () { $('#Save').click(function (e) { $.ajax({ type:'POST', url:'/savenetwork/', dataType:'json', data:{ networkdata: JSON.stringify(grapharr), csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val() }, contentType:'application/json', success:function (data) { alert(data); } }) }) }) Here "grapharr is a dynamically created array which has json objects at each index and here is view.py for this POST request : def savenetwork(request): if request.method == 'POST': data = json.load(request.body) response_data = {"data" : "Test"} return HttpResponse(json.dumps(response_data), content_type="application/json") CSRF token is defined … -
Is it possible to duplicate a django-mptt model?
Let's say I have a django-mptt model that looks like this: class Category(MPTTModel): name = models.CharField(max_length=50) parent = TreeForeignKey('self', null=True, blank=True, related_name='children', db_index=True) These Categories (and sub-categories) will serve as a kind of template for the categories used in a project. When a user starts a new project, the user will select which Categores, Sub-Categories, etc. to use. The user should be able to also add/edit Categories. The thing is that they need to be specific the project so that when another project is created, the user will start with the original/default categories. Is there any way to duplicate the MPTTModel/database table(s) to create project specific one where categories can be edited/added without it affecting the default ones? I can think of one way to solve this issue which would be to add something like projects = models.ManyToManyField(Project) and create a default/template project. What's the best approach here? -
User has no attribute backend with custom auth backend, but I called authenticate
I create a custom authorization backend in a django project. I subclassed the AuthenticationForm and overrode the clean method. The clean method calls the authenticate method from my auth backend, and my auth backend returns the authenticated user. However, I'm still getting the following error: AttributeError: 'User' object has no attribute 'backend' All the posts I've found for this error refer to django documentation that requires authenticating before calling login, but I did! AuthBackend: class MyAuthBackend(object): supports_inactive_user = False def authenticate(self, username=None, password=None, v_code=None): print("****************\t\nin authenticate: tz:{}, phone:{}, code:{}\r\n********************".format( username, password, v_code)) if not username or not password or not v_code: return None auth_response = MyUtilityClass.authenticateUser(password, username, v_code) if auth_response['status_code'] != 200: return None try: user = User.objects.get(username=username) except User.DoesNotExist: user = User(username=username, password=phone) user.save() print ("about to return user:{}".format(user)) return user def get_user(self, user_id): try: return User.objects.get(pk=user_id) except User.DoesNotExist: return None AuthenticationForm class MyAuthForm(AuthenticationForm): username = forms.IntegerField(label=_("Teudat_Zehut")) password = forms.CharField(label=_("Mobile_Phone")) v_code = forms.CharField(label=_("Code"), required=True) def clean(self): user, created = User.objects.get_or_create( username=self.cleaned_data['username'], password=self.cleaned_data['password'] ) backend = WakeupAuthBackend() self.user_cache = backend.authenticate(username=self.cleaned_data['username'], password=self.cleaned_data['password'], v_code=self.cleaned_data['v_code']) print("in clean wakeup auth, the user returned from authenticate:{}".format(self.user_cache)) if self.user_cache is None or not self.user_cache.is_active: raise forms.ValidationError(_("Your username and password didn't match. Please try again")) return self.cleaned_data -
How can I download a temporary file in Django?
I'm learning how to serve temporary files in Django, and even after reading the docs I'm having some trouble finishing it all up. These files are dynamically generated temporarily from user input. gcode = "/home/bradman/Documents/Programming/DjangoWebProjects/3dprinceprod/fullprince/media/uploads/tmp/skull.gcode" test_file = open(gcode, 'r') response = HttpResponse(test_file, content_type='text/plain') response['Content-Disposition'] = "attachment; filename=%s.gcode" % title print (response) return response The code above should place my temporary-gcode file from my server into an HttpResponse object, and the return function should download the file. It even slows down like its downloading, but there is no file once it's finished. This question provided most of the useful info and this one was helpful as well, but I can't get it working and don't know how else to test it. I'm not sure if moving to apache or something is appropriate since I have user permission issues and these files are immediately deleted after downloaded. I've checked that "gcode" is the directory url, and gcode content_type should be text/plain. So basically, how can I make my response actually download? -
Django make variable visible
I want to make variable visible in elif without making it global, is it possible in any other ways? if query !=something: all_toys = Toys.objects.filter(user_name=request.user) elif query2 != something: all_toys = all_toys.filter(amount=20) second all_toys isn't visible, and i got this error django referenced before assignment.