Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django ORM Annotate boolean field that defines is user member of a team
models.py from django.db import models class UserGroup(models.Model): members = models.ManyToManyField(User, related_name='members', through='UserGroupMember') class UserGroupMember(models.Model): user = models.ForeignKey(User) usergroup = models.ForeignKey(UserGroup) class Cohort(models.Model): user_groups = models.ManyToManyField(UserGroup) class Team(models.Model): cohort = models.ForeignKey(Cohort) members = models.ManyToManyField(User, related_name='team_members', through='TeamMembers', blank=True) class TeamMembers(models.Model): team = models.ForeignKey(Team) user = models.ForeignKey(User) Single user can be part of only one team within a cohort. I want to annotate the new field (boolean) which tells you is the user assigned to some team in the cohort, something like: User.objects.filter( members__cohort=cohort ).annotate( is_team_member=... ) I am using Python 2.7.13 and Django 1.9.8. Thanks. -
d3.json(url, callback) not working correctly with django
I have created a DJango/ReactJS site that is attempting to display an nv.d3.js pie chart. When i add the code to display the pie chart, passing the data array directly into the code, the pie chart displays on the site correctly. However, when i use d3.json(url, callback) to retrieve a json object from the url (served by Django views.py) and pass that as the data into the pie chart, i get an error. The nvd3 code is: createPieChart(){ const node = this.node nv.addGraph(function() { var chart = nv.models.pieChart() .x(function(d) { return d.label }) .y(function(d) { return d.value }) .showLabels(true); d3.select(node) .datum(getPieData()) .transition().duration(350) .call(chart); return chart; }) } the getPieData() function is: function getPieData() { // return (d3.json(url, function(error, rawData) { // if (error) { // console.log("error = " + error) // return; // } // else{ // return rawData; // } // } // ) // ) return [{"label": "R", "value": 822}, {"label": "H", "value": 1}]; } notice in the above snippet, i commented out the problematic code. what i return above works. now, if i go to the URL above that is served by Django, it returns the following data exactly like this in the browser (and in the … -
Dependency between fields in model using Factoryboy
I have a Django Model that I would like to test using Factoryboy. The issue here is that the fields depend on each other. class SearchPreferences(models.Model): min_age = models.PositiveSmallIntegerField(null=True) max_age = models.PositiveSmallIntegerField(null=True) In this case, max_age cannot be smaller then min_age. class SearchPreferencesFactory(DjangoModelFactory): min_age = FuzzyInteger(30, 90) max_age = FuzzyInteger(SelfAttribute('min_age'), 100) This is what I have tried to do, which should give me a value for max_age between min_age and 100, but what happens is a TypeError: TypeError: unsupported operand type(s) for +: 'SelfAttribute' and 'int' This makes sense to me, but I can't really figure out how to get this to work? Could someone explain what would be the best approach here? -
How to access request body when using Django Rest Framework and avoid getting RawPostDataException
I need to get the raw content of POST request body yet when I try to access request.body I'm getting an exception: django.http.request.RawPostDataException: You cannot access body after reading from request's data stream I am aware that it is adviced to use request.data instead of request.body when using Django Rest Framework, yet for the purpose of validating digital signature I have to have the request body in a raw and "untouched" form, since this is what is 3rd-party signed and what I need to validate. Pseudocode: 3rd_party_sign(json_data + secret_key) != validate_sign(json.dumps(request.data) + secret_key) 3rd_party_sign(json_data + secret_key) == validate_sign(request.body + secret_key) -
Django: Is meaningful to replace standard template cached loader with memcached?
I am using recommended Django template cached loader to improve performance on production server. After investigation of loader source code and notice that loader do not use standard Django cache mechanism, but only saves compiled messages inside dictionary. Am I right that this dictionary is not shared between processes and threads? For instance if I running Django on Gunicorn in setup with 3 processed, each process 2 threads, each thread compiles and saves templates on their own? This can cause slower startup after server or process restart (using max_request directive in Gunicorn). Does it make sense to create custom template loader which use memcached for saving compiled templated and sharing between processes? -
on_delete error for Django
I'm currently doing some projects from a book called Python Crash Course by Eric Matthes. Currently, I'm doing lab 19.1 where I have to use previous code to make a Django project while changing some of his own code but ran into a problem. Every time I want to run this command >>>python manage.py makemigration blogs in return, I get this code TypeError: init() missing 1 required positional argument: 'on_delete' His original models.py code: from django.db import models class Topic(models.Model): """A topic the user is learning about.""" text = models.CharField(max_length=200) date_added = models.DateTimeField(auto_now_add=True) def __str__(self): """Return a string representation of the model.""" return self.text class Entry(models.Model): """Something specific learned about a topic.""" topic = models.ForeignKey(Topic) text = models.TextField() date_added = models.DateTimeField(auto_now_add=True) class Meta: verbose_name_plural = 'entries' def __str__(self): """Return a string representation of the model.""" return self.text[:50] + "..." and my current code: from django.db import models # Create your models here. class BlogPost(models.Model): title = models.CharField(max_length=200) text = models.CharField(max_length=200) date_added = models.DateTimeField(auto_now_add=True) def __str__(self): """Return a string representation of the model.""" return self.text class Post(models.Model): """SThe post""" topic = models.ForeignKey(BlogPost) text = models.TextField() date_added = models.DateTimeField(auto_now_add=True) class Meta: verbose_name_plural = 'posts' def __str__(self): if len(self.text) >= 50: """Return a … -
How do you handle files in a django test
I have a model that has a file field and I've been more or less testing the model like so: def test_importing_file_and_processing(self): file_path = os.path.join(settings.BASE_DIR, 'myapp/tests/reports/test_input_file') report = IngestedFile( filename='test_file' ) report.document.save('test_file', File(open(file_path))) report.save() report.do_stuff() self.assertStuf.... Is there a "better" way to test the file? -
Celery auto retry not working
I'm trying to use celery to send and queue emails in a Django 1.11.4 app. In my DEV environment, using ./manage.py runserver 8000 The task runs and emails get sent. But if I try to break it, by changing the SMTP password in settings.py, things go bad. Here's the task: @app.task(name="email_foo", autoretry_for=(Exception,), default_retry_delay=30, retry_kwargs={'max_retries': 5}) def email_fooApp_task(ctx, subject, recipient_id): email_fooApp_simple(ctx, subject, recipient_id) I can see the email fail in the terminal: [2017-12-05 15:19:05,908: INFO/ForkPoolWorker-2] Task email_foo[6856f683-e86d-4b99-a77c-9edee586ba5e] retry: Retry in 30s: SMTPAuthenticationError(502, b'5.5.1 Unrecognized command. q12sm565712qtk.32 - gsmtp') And it appears to be rescheduled. But it never gets retried. It's in the schedule, as inspect scheduled shows: 'priority': 6, 'eta': '2017-12-05T20:19:35.871604-04:56', 'request': {'name': 'email_foo', 'args': "({'ical_date': '20170916T170000Z/20170916T180000Z', 'txt_body': 'From Bar, Rob\r\nfoo@bar.com', 'HOST_URL': 'http://127.0.0.1:8000', 'html_body': '<p>From Bar, Rob<br />\r\nrob@foo.bar</p>', 'recipient': 'Rob'}, 'Test Bad SMTP pwd', 1108756)", 'delivery_info': {'priority': 0, 'routing_key': 'celery', 'exchange': '', 'redelivered': None}, 'time_start': None, 'kwargs': '{}', 'acknowledged': False, 'worker_pid': None, 'type': 'email_foo', 'hostname': 'celery@foo.bar', 'id': '6856f683-e86d-4b99-a77c-9edee586ba5e'}} Any ideas?? -
Django multiple view functions for one page
I'm tring to make a page on my website where users can pick recipes of which the products are added to a newly created shoplist. However, there is a lot going on at the page and quite a lot of areas on the page need to be updated after selecting a recipe. So far the functionality of the page is: 1. Make a shoplist 2. Display relevant values to the user 3. Show a filtered list of recipes to the user on the basis of submitted values 4. Update the values on the page with values from the chosen recipes In the future I will need to implement even more functionality and I'm pretty sure that my code will become a mess if I leave it like this. I'll summarize my goals and current questions with the vies that I have made: A shoplist has to be made when the page is loaded. This shoplist is not visible to the user. Only a few values will be. I want to display the values 'budget' and 'dagen'. I have made a context for this which is now the same in both views. This is not good DRY code as I understand. … -
django test a templatetag which uses the request
I'm using a custom templatetag to calculate currency values, currency is choosen from a select element. when the user choose the currency he want work with I save the choosen currency in the session and refresh the page. a templatetag shows the value calculated for the choosen currency. in any template {% load currency %} {% set_currency request 156 %} in my_app/templatetags/currency.py from django.conf import settings from djmoney_rates.utils import convert_money register = template.Library() @register.inclusion_tag('includes/price.html') def set_currency(request, price): # currency_session could be 'USD', 'EUR' ... currency_session = request.session.get(settings.CURRENCY_SESSION_KEY, settings.DEFAULT_CURRENCY) money = convert_money(price, settings.DEFAULT_CURRENCY, currency_session) return {'amount': '%d' % money.amount, 'currency': money.currency} includes/price.html is just <span class="amount">{{amount}}</span> <span class="currency">{{currency}}</span> Now I'm wondering the way to test this templatetag, how to pass the request, and how make session to exists in that request. -
*10 upstream timed out (110: Connection timed out) while reading response header from upstream with uwsgi
I currently have a server setup with nginx and uwsgi with django This error doesn't happen until I try to change my rds instance my fully error message is *10 upstream timed out (110: Connection timed out) while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: xxx.xxx.xxx.xxx, request: "GET /load/ HTTP/1.1", upstream: "uwsgi://unix:/tmp/load.sock", host: "example.com", referrer: "https://example.com/" I was using aws rds (postgres) which works perfectly fine. The only change I made is changing from regular postgres service to aurora postgres I didn't upgrade the db, from regular to aurora. I created a new aurora postgres. I got everything setup...changed host and everything in my django db setting. runserver locally works fine. It does connect to db with read and write. Works perfectly. But when I deploy to server, open up my domain. Anything ui related looks fine but db related, NO. Took awhile then of course the 504 gateway timeout. I went to checkout the nginx error log. That's the error message I found. Googled, tried a few settings other stackoverflow posts suggested such as addingsingle-interpreter = true into uwsgi.ini file. No luck. Can someone please give me an idea where I should look into for this problem? Thanks … -
Django default site - migrations
How I set up Django default site (name + domain)? Docs and answers here suggest to use data migrations but that doesn'work in case you just run migrate on newly create database where all migrations run at once. Why? Simply because Site model is not available when I call get_model() method in my data migration. I've tried to use fixtures but Django starts auto_increment site table value from SITE_ID in your settings so you can't simply ignore ID 1 (the example.com) and just add your custom domains to fixtures and set the ID in settings - like SITE_ID = 2. The question is how I set up/override Django default "example.com" site so I can run all migration at once against production database and have my domain set up? -
make query to table (django_migrations)?
I need to write in table django_migrations for that I am using: fix_migration.py from django.db import connection from django.db.migrations import recorder recorder.MigrationRecorder(connection).record_applied("registro_movimientos", "0001_initial") I am applying that via python manage.py runscript fix_migration.py but I need check first if that migrations was applied before , some like that: query = django_migrations.filter(name=0001_initial, app="registro_movimientos") if not query: recorder.M .... thanks . -
Django 2.0 path error ?: (2_0.W001) has a route that contains '(?P<', begins with a '^', or ends with a '$'
I'm new to Django and am trying to create the back end code for a music application on my website. I have created the correct view in my views.py file (in the correct directory) as shown below: def detail(request, album_id): return HttpResponse("<h1>Details for Album ID:" + str(album_id) + "</h1>") however, when creating the url or path for this (shown below) #/music/71/ (pk) path(r'^(?P = <album_id>[0-9])/$', views.detail, name='detail'), I am experiencing a warning on my terminal stating: ?: (2_0.W001) Your URL pattern '^(?P = [0-9])/$' [name='detail'] has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path(). and whenever the /music/ (for which the path works) is followed by a number, such as /music/1 (which is what I want to be able to do) the page cannot be found and the terminal gives the above warning. It may be a simple error and just me being stupid but I'm new to Django and python regex statements, so any helps is appreciated, thanks. -
Django OneToOneField Unique fails when extending User model
I'm trying to extends Django User model by means of an Actor model but I'm getting some problems. There is my code: class Actor(models.Model): usuario = models.OneToOneField('auth.User', unique = True, null = True) def __str__(self): return self.usuario.get_full_name() + ' (' + self.usuario.get_username() + ')' The problem is that one-to-one relationship is not unique so I can create two differents actors and relate them with the same user. Use case idea: I create a User (User model by Django) then I create an actor and relate him with the user I created before. If I create a second actor, there would not must be posible related him with the same user. -
Connecting to RDS Postgres from Heroku
I'm in the process of migrating my Heroku app database from Heroku to AWS RDS Postgres. On my computer, I can connect to my RDS DB using: psql -d "postgres://user:password@XXX.rds.amazonaws.com/mydb?sslrootcert=config/amazon-rds-ca-cert.pem&sslmode=require" However, the same psql command run from within my heroku server just hangs forever. Also, config/amazon-rds-ca-cert.pem is the RDS certificate that I added to my package as mentioned in the documentation https://devcenter.heroku.com/articles/amazon-rds#authorizing-access-to-rds-instance and here https://stackoverflow.com/a/29467638/943524 (I did combine certificates as I am using a eu-central-1 instance). Would someone have an idea what is blocking the connection here ? -
How to get current url in views.py django?
First I read many answers here and mostly are about getting url in template. I need to get url in views.py then I need to assign it as veriable. I have 6 views like that and I have 6 querysets as well. SweatbandsView, HeadbandsView, BandanasView, BalloonsView ,TableCoversView, CoastersView I created new view which is called Preview: class Preview(SweatbandsView, HeadbandsView, BandanasView, BalloonsView ,TableCoversView, CoastersView): template_name = "preview.html" As you see there, this view includes all of those. Now I need to get my current url and assign it like the_url. Then Preview.py should be like this: class Preview(SweatbandsView, HeadbandsView, BandanasView, BalloonsView ,TableCoversView, CoastersView): template_name = "preview.html" the_url = whatever_func_is_it() if (the_url == '/someting/something'): queryset = Product.objects.all().filter(category_id='6') else: . . . . With this way I want to use needed queryset in if condition! By the way I have 2 models in database which are Product and Category and they have OneToMany relation. Thanks! -
django REST framework nested serializer and POST nested JSON with files
How do I send a POST request with nested data including files (images) to django REST nested serializers? Given this JS object: bookData: { title: 'Anne of Green Gables', coverImage: File(123456), pages: 123, author: { name: 'Lucy Maud Montgomery', born: 1874, profilepicture_set: [ {file: File(234567), description: 'Young L. M. Montgomery'}, {file: File(234568), description: 'Old L. M. Montgomery} ], quote_set: [ {text: "I'm so glad I live in a world where there are Octobers."}, {text: "True friends are always together in spirit."}, ] }, } I want to send a POST request to my django REST API (I use VueJS on frontend, but this does not really matter). # views.py class CreateBookView(generics.CreateAPIView): serializer_class = CreateBookSerializer queryset = Book.objects.all() # serializers.py class CreateBookSerializer(serializers.ModelSerializer): author = CreateAuthorSerializer() class Meta: model = Book fields = ('title', 'pages', 'author') @transaction.atomic def create(self, validated_data): author_data = validated_data.pop('author') uploader = self.context['request'].user book = Book.objects.create(uploader=uploader, **validated_data) Author.objects.create(book=book, **author_data) return book # models.py class AuthorManager(models.Manager): def create(self, **author_data): quotes_data = author_data.pop('quote_set') photos_data = author_data.pop('profilepicture_set') author = Author(**author_data) author.save() for quote_data in quotes_data: Quote.objects.create(author=author, **quote_data) for photo_data in photos_data : ProfilePicture.objects.create(author=author, **photo_data) return author ## Skipping the CreateAuthorSerializer, Quote and ProfilePicture Managers as they are analogous. ## Assume one Author … -
How to name sorl-thumbnail files based on a field of the source picture model ? (Django)
Our site is based on Python 2.7 / Django 1.9 and sorl-thumbnail 12.4.1 Basic picture model: class Picture(models.Model): file = ImageField(max_length=500, upload_to='pics') placename = models.CharField(max_length=200) We override the _get_thumbnail_filename function of sorl-thumbnail to have more SEO friendly thumbnail names (so that the thumbnail file takes the name of the initial picture): class SEOThumbnailBackend(ThumbnailBackend): def _get_thumbnail_filename(self, source, geometry_string, options): key = tokey(source.key, geometry_string, serialize(options)) filename, _ext = os.path.splitext(os.path.basename(source.name)) path = '%s/%s' % (key, filename) return '%s%s.%s' % (settings.THUMBNAIL_PREFIX, path, EXTENSIONS[options['format']]) We would like to add the "placename" at the end of this thumbnail file name. We can't get how to retrieve the Picture model from the function. Any clue ? That would be much helpful. Thanks a lot -
Django form clean() method not raising errors
I am trying to create a custom validator for my form by overwriting the clean() method, and for some reason validation errors are not being raised properly. Here's my code: forms.py from django import forms from django.contrib.auth import get_user_model class EmailForm(forms.Form): email_field = forms.EmailField(label='E-mail address', max_length=128) def clean(self): cleaned_data = super(EmailForm, self).clean() email = cleaned_data.get('email') try: u = get_user_model().objects.get(email=email) raise forms.ValidationError("E-mail already in database.") print('validation error raised') except: pass views.py from django.shortcuts import render from django.contrib import messages from .forms import EmailForm def email_form(request): if request.method == 'POST': form = EmailForm(request.POST) # If the form is valid... if form.is_valid(): messages.add_message(request, messages.SUCCESS, "Kaboom! Your referral e-mail was sent. Hang tight - we'll be sure to let you know when your friend makes a purchase.") else: messages.add_message(request, messages.ERROR, 'Error!') else: form = EmailForm() return render(request, 'template.html', {'form': form}) template.html <form action="{% url 'form-process' %}" method="post"> {% csrf_token %} {{ form.as_p }} <input type="submit" value="Submit" /> </form> When I submit this form with input that should raise a validation error (i.e., an e-mail address that is already associated with a user in my database), I get the 'success' message rather than an error. The strangest part about it is that 'validation error raised' … -
HTTPS equivalent of Django's HttpResponse
For some reason I am in need of a views.py that returns only some text. Normally, i'd use HttpResponse("text") for this. However, In this case I require the text to be send over https, to counter the inevitable mixed content warning. What is the simplest way of sending pure text via django(1.7.11) over https? -
How To Save an Item List in rows in session and display it on the Template in django
Kindly assist with my plight as am still new to django python. Am developing a shopping cart application, where ajax receives the cart items and send it via POST to View in django in Django View def add_product (request): if is_ajax or request.POST: productname = request.POST.get('productname','') price = request.POST.get('price') quantity = request.POST.get('quantity', '') cartsession = request.session.get('cartitems', {}) cartsession[productname] = productname cartsession[price] = price cartsession[total] = total cartsession[quantity] = quantity request.session.get('cartitem') = cartsession return render(request, 'cartset.html',{'cartsession':cartsession}) on Template {% for cart in cartsession %} { cart.productname} {% endfor %} After the code above nothing is displaying on the Template page... Someone pls help -
Saving a List to the database as multiple rows
I have a list that is derived from check box selections defined as the following: checkedlist = request.GET.getlist('report_id') This list gets POSTed to a submitted view. def submitted(request): owner = User.objects.get (formattedusername=request.user.formattedusername) requestsave = QVFormAccessRequest(ntname_id = owner.formattedusername, first_name = owner.first_name, last_name = owner.last_name, coid = owner.coid, facility = owner.facility, title = owner.title ,report_id = request.POST.getlist('report_id')) requestsave.save() The problem is that when it tries to save to the database my field is defined as an int which is correct. I want my report_id for the list to be stored on multiple lines in my database like the following: user information report_id HFA9592 1 HFA9592 2 HFA9592 3 HFA9592 4 In the current save it's trying to post the list to the report_id how would I break it into multiple rows as shown above? I get the following error: TypeError at /account/submitted/ int() argument must be a string, a bytes-like object or a number, not 'list' -
Forbidden (CSRF token missing or incorrect.) django 1.11
followed a lot of the stuff recommended on StackOverflow. Also I tried to put {% csrf_token %} in the html in various places but none seemed to work. Any suggestions? Here is my django templates input button <input id=saveWaypoints type=button value='Save your Location' disabled=disabled> Which calls this javascript $('#saveWaypoints').click(function () { var waypointStrings = []; for (id in waypointByID) { waypoint = waypointByID[id]; waypointStrings.push(id + ' ' + waypoint.lng + ' ' + waypoint.lat); }; $.post("{% url 'waypoints-save' %}", { csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val(), waypointsPayload: waypointStrings.join('\n') }, function (data) { if (data.isOk) { $('#saveWaypoints').attr('disabled', 'disabled'); } else { alert(data.message); } }); }); Which then calls view: def save(request): 'Save waypoints' for waypointString in request.POST.get('waypointsPayload', '').splitlines(): waypointID, waypointX, waypointY = waypointString.split() waypoint = Waypoint.objects.get(id=int(waypointID)) waypoint.geometry.set_x(float(waypointX)) waypoint.geometry.set_y(float(waypointY)) waypoint.save() return HttpResponse(simplejson.dumps(dict(isOk=1)), mimetype='application/json') Any help is appreciated. Please. Thanks -
Firebase nested child index query rule pyrebase
There will be multiple companies.i want to authenticate user with USER_NAME and PASSWORD. I thought about making user as main node. then it will be like what we do with SQL foreign key references. in this structure, all data related to a company will be under its node only. it gives only error : "Index not defined, add \".indexOn\": \"USER_NAME\", for path \"/company/USERS\", to the rules". i found many answers but none of them fits mine. (sorry for bad english) this is the data { "company" : { "-L-dLuMIehYmEuzaui_D" : { "ADDRESS" : "OPPOSITE SOUTH INDIAN BANK, KONDOTTY", "CITY" : "KONDOTTY", "NAME" : "PAMCO TROPHIES", "USERS" : { "-L-b5ZIxc5BqTOxpMAI4" : { "DESCRIPTION" : "", "FULL_NAME" : "Administrator", "PASSWORD" : "123.com", "USER_NAME" : "admin", "USER_TYPE" : "Admin" } } } } } this is what i written in python user = firebase.database().child("company").order_by_child("$cmpId").child("USERS").order_by_child("$userId").order_by_child("USER_NAME").equal_to(userName).get() this is the rule { "rules": { ".read": true, ".write": true, "company":{ "$cmpId":{ "USERS":{ "$userId":{ ".indexOn":["USER_NAME"] } } } } } }