Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Optimizing queries in Django, prefact_related for objects that are reversely linked with goal object
In my django project I have 3 models, simplified for this example: Contact, WorkRelation and Group objects. Contact class Contact(BaseModel): title = models.CharField(max_length=30, blank=True) initials = models.CharField(max_length=10, blank=True) first_name = models.CharField(max_length=30, blank=True) prefix = models.CharField(max_length=20, blank=True) surname = models.CharField(max_length=30) def get_workrelations(self): workrelations = apps.get_model('groups', 'WorkRelation') return workrelations.objects.filter(contact=self) def get_organisations(self): output = "" strings = [] wr = self.get_workrelations() for relation in wr: group = relation.group name = group.name strings.append(s) if len(strings) > 0: output = ", ".join(strings) return output WorkRelation class WorkRelation(BaseModel): contact = models.ForeignKey(Contact, on_delete=models.CASCADE) group = models.ForeignKey(Group, on_delete=models.CASCADE) function = models.CharField(max_length=40, blank=True) email_address = models.EmailField() phone_number = models.CharField(max_length=13, blank=True) description = models.TextField(max_length=400, blank=True) Group class Group(BaseModel): group_type = models.ForeignKey(GroupType, on_delete=models.CASCADE) name = models.CharField(max_length=60, unique=True) street_name = models.CharField(max_length=40, blank=True) house_number = models.CharField(max_length=10, blank=True) The problem with this setup is that it becomes extremely slow when I want to call get_organisations() on a large contact set. For example: when trying to list all my contacts (600 for my demo set), and call get_organisations(), about 1250 queries are needed. I found that you can prevent this by using prefetch_data(), but somehow I can't get this working in my setup. I tried to replace my query for queryset = Contact.objects.prefetch_related('workrelation_set') But this … -
How to set the default value of date field to Null ones a table entry is created and update it when a book is returned?
How can I set the date_in filed to null at the time of entry and update the value to the date book is returned. class Book_Loans(models.Model): Loan_id = models.AutoField(primary_key=True) Isbn = models.ForeignKey('Books') Card_id = models.ForeignKey('Borrower') Date_out = models.DateField(auto_now_add = True, auto_now = False) Due_date = models.DateField(default = get_deadline()) Date_in = models.DateField(null = True,auto_now_add = False, auto_now = True) def __unicode__(self): return str(self.Loan_id) -
Python Webdriver raises http.client.BadStatusLine error
I'm writing a parser and I'm using Selenium Webdriver. So, I have this https://repl.it/Dgtp code and it's working fine until one random element and raises http.client.BadStatusLine error. Don't know how to fix it at all. Help. -
Django app has a no ImportError: No module named 'django.core.context_processors'
Tried git pushing my app after tweaking it and got the following error. ImportError: No module named 'django.core.context_processors' this was not showing up in my heroku logs and my app works locally so I was confused. I had to set debug to true on the production side to finally figure this out. What can I do to clear this up? this is some of the traceback Request Method: GET Request URL: http://hispanicheights.com/ Django Version: 1.10.1 Exception Type: ImportError Exception Value: No module named 'django.core.context_processors' Exception Location: /app/.heroku/python/lib/python3.5/importlib/__init__.py in import_module, line 126 Python Executable: /app/.heroku/python/bin/python Python Version: 3.5.1 Python Path:['/app', '/app/.heroku/python/bin', '/app/.heroku/python/lib/python3.5/site-packages/setuptools-23.1.0-py3.5.egg', '/app/.heroku/python/lib/python3.5/site-packages/pip-8.1.2-py3.5.egg', '/app', '/app/.heroku/python/lib/python35.zip', '/app/.heroku/python/lib/python3.5', '/app/.heroku/python/lib/python3.5/plat-linux', '/app/.heroku/python/lib/python3.5/lib-dynload', '/app/.heroku/python/lib/python3.5/site-packages', '/app', '/app'] -
Import Django model class in Scrapy project
I have a django project and a scrapy project and I want to import django model from to scrapy project. This is my spider: import scrapy from scrapy.spiders import CrawlSpider, Rule from scrapy.linkextractors import LinkExtractor import sys sys.path.append('/home/ubuntu/venv/dict') from dmmactress.models import EnActress class JpnNameSpider(scrapy.Spider): name = jp_name allowed_domains = ['enjoyjapan.co.kr'] rx = EnActress.objects.values_list('name', flat=True) rxs = rx.reverse() start_urls = ['http://enjoyjapan.co.kr/how_to_read_japanese_name.php?keyword=%s' % jp for jp in rxs] def parse(self, response): for sel in response('//*[@id="contents"]/div/div[1]/div/div[1]'): item = JapanessItem() item['koname'] = sel.xpath('div[4]/div[1]()/text()').extract() item['jpname'] = sel.xpath('div[2]/div[1]()/text()').extract() yield item next_page = response.css('#contents > div > div:nth-child(4) > div > a::attr(href)').extract_first() if naxt_page is not None: next_page = response.urljoin(next_page) yield scrapy.Request(next_page, self.parse) and I got error when I ran spider django.core.exceptions.ImproperlyConfigured: Requested setting DEFAULT_INDEX_TABLESPACE, but settings are not configure. -- Can someone help me to see what I am doing wrong? Thanks in advance! -
Tastypie Object Update: Inharitance Error
I have a BasicUserPerson model that inherits from BasicUser. I hame criate resources for both of them. My problem is that my resource for BasicUserPerson creates an instance of BasicUserPerson as expected, but only updates the fields related to BasicUser! The resource: class BasicUserPersonResource(BasicUserResource): class Meta: queryset = BasicUserPerson.objects.all() resource_name = 'basic_user_person' authorization = Authorization() validation = BasicUserValidation() serializer = JsonOnlySerializer() filtering = { "id": 'iexact', "user": ALL_WITH_RELATIONS, } I overwrote the following methods just to test: def obj_update(self, bundle, **kwargs): print type(bundle.obj) def obj_create(self, bundle, **kwargs): print type(bundle.obj) And, guess what, the update receives a BasicUser instance. Why??!! <class 'core.models.BasicUserPerson'> [24/Sep/2016 18:42:19] "POST /api/v1/basic_user_person/ HTTP/1.1" 201 0 (time: 0.03s; sql: 2ms (7q)) <class 'core.models.BasicUser'> [24/Sep/2016 18:42:24] "PATCH /api/v1/basic_user_person/2232/ HTTP/1.1" 202 0 (time: 0.49s; sql: 10ms (23q)) -
Heroku ImportError: no module named django_markdown
I recently uninstalled django-markdown, and then installed django-markdown-app. The site still runs fine locally. But after pushing to Heroku, the site crashes with File "/app/.heroku/python/lib/python2.7/site-packages/django/apps/config.py", line 90, in create module = import_module(entry) File "/app/.heroku/python/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) ImportError: No module named django_markdown My INSTALLED_APPS setting contains django-markdown, which seems to trigger the error. The requirements.txt file contains django-markdown-app==0.8.6. When I do heroku run pip freeze, the result includes django-markdown-app==0.8.6 as well. Any idea what might be going on here? -
Cannot understand how this property on a model is being invoked
I am going over a tutorial on extending a user model and it seems to work however I had 2 questions on how a property is being invoked and about a constructor. First this is the code in question The main model is this class UserProfile(models.Model): user = models.OneToOneField(User) likes_cheese = models.BooleanField(default=True) puppy_name = models.CharField(max_length=20) User.profile = property(lambda u : UserProfile.objects.get_or_create(user=u)[0])--->Statement A Now the form which gets presented to the user is this class UserProfileForm(forms.ModelForm): class Meta: model = UserProfile fields = ('likes_cheese','puppy_name') and the view that presents this form is as follows this is where my questions are (Over here i am simply interested in presenting the form so I removed the other code out): def user_profile(request): display_page = "profile.html" csrf_token = csrf(request) args={} user = request.user profile = user.profile ---------------------->statement B - Question 1 below form = UserProfileForm(instance=profile) ------->statement C - Question 2 below args.update(csrf_token) args["form"] = form return render_to_response(display_page,args) Now here are my two questions Q1- Why isnt a parameter being passed to the property profile of User object ? from what I understand from statement A which is User.profile = property(lambda u : UserProfile.objects.get_or_create(user=u)[0]) is that profile is being defined as a property of User and … -
How to Create a Backend Server for a mobile app in python
I am aware of the already made back-end server applications like firebase and bluemix. I want to build my own back end for an android app. What is the best way to do it I would prefer python but any other language is fine I will learn how to use them. I just need to know how this process will work and how to do it. Instructions would be nice. I am planning to build the front end in android studio but I need a back-end server to collect all the data. -
Django use multiple databases
I'm trying to make a Django website as a user interface for data I've collected through a scraper. The scraper generates (and constantly updates) a database and I'd like Django to interact with it as well. I need to run the scraper program often, is there a way I can do this through Django's admin? Like manage the backend that doesn't have to do with Django directly? Should I merge the databases (scraper and Django)? Is there a proper way of doing this? Thanks in advance. -
Django sharing anotations between related Items
Consider a room booking system. You might have a Building, Floor, Room models as well as a Booking. We give the room a name based on its building and floor: class Room(models.Model): number = models.PositiveIntegerField() name = models.CharField(..) floor = models.ForeignKey('Floor') def __str__(self): return '%s, #%d Floor %d, %s' % ( self.name, self.number, self.floor.number, self.floor.building.name ) This is woefully inefficient when you're doing hundreds of them (eg admin list, big reports, etc.) so I've taken to writing managers like this: class RoomManager(models.Manager): def get_queryset(self): return super().get_queryset().annotate( roomname=Concat( 'name', V(', #'), 'number' V(' Floor '), 'floor__number' V(', '), 'floor__building__name', output_field=models.CharField() ), ) And that works. It does everything I wanted it to. It's fast and I've reworked the __str__ to do a if hasattr(self, 'roomname'): return self.roomname before it does the horrendous multi-query string builder. But now on top of this, I have Booking. Each Booking instance is linked to a single room. There are many cases where to list Bookings, I actually also list room names. What I've done is write a BookingManager: class RoomManager(models.Manager): def get_queryset(self): return super().get_queryset().annotate( roomname=Concat( 'room__name', V(', #'), 'room__number' V(' Floor '), 'room__floor__number' V(', '), 'room__floor__building__name', output_field=models.CharField() ), ) But what the hell? I'm repeating … -
Redirect error when trying to request a url with requests/urllib only in python
im trying to post data to a url in my server ... but im stuck in sending any request to that url (any url on that server ) here is one for example http://apineginpay.in/page/test the website is written in python3.4/django1.9 i can send request with curl in php without any problem but any request with python will result on some kind of redirect error at first i've tried requests lib i got this error TooManyRedirects at /api/sender Exceeded 30 redirects. Request Method: GET Request URL: http://localhost:8000/api/sender Django Version: 1.9.6 Exception Type: TooManyRedirects Exception Value: Exceeded 30 redirects. i thought maybe something wrong with requests so i tried urllib request_data = urllib.parse.urlencode({"DATA": 'aaa'}).encode() response = urllib.request.urlopen("http://apineginpay.in/page/test" , data=request_data) HTTPError at /api/sender HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop. The last 30x error message was: Found Request Method: GET Request URL: http://localhost:8000/api/sender Django Version: 1.9.6 Exception Type: HTTPError Exception Value: HTTP Error 302: The HTTP server returned a redirect error that would lead to an infinite loop. The last 30x error message was: Found Exception Location: c:\Python344\lib\urllib\request.py in http_error_302, line 675 im using mod_wsgi and apache to serve the website -
Django insistently appends /admin/login/?next= to my url
I am having quite a headache with this today. It used to work fine lol. I have a menu which calls django admin views, for example, /admin/geotechnical/alertlevel/, but when i click it, it redirects to /admin/login/?next=/admin/geotechnical/alertlevel/. There, I try to log in but it sends me back. For some reason it isn't seeing that my user is already logged in and keeps sending me to this infinite loop. In my admin.py @never_cache def login(self, request, extra_context=None): """ Displays the login form for the given HttpRequest. """ if request.method == 'GET' and self.has_permission(request): # Already logged-in, redirect to admin index index_path = reverse('admin:index', current_app=self.name) return HttpResponseRedirect(index_path) from django.contrib.auth.views import login # Since this module gets imported in the application's root package, # it cannot import models from other applications at the module level, # and django.contrib.admin.forms eventually imports User. from django.contrib.admin.forms import AdminAuthenticationForm context = dict( # self.each_context(), title=_('Log in'), app_path=request.get_full_path(), logged=self.has_permission(request), ) if (REDIRECT_FIELD_NAME not in request.GET and REDIRECT_FIELD_NAME not in request.POST): context[REDIRECT_FIELD_NAME] = request.get_full_path() context.update(extra_context or {}) defaults = { 'extra_context': context, 'current_app': self.name, 'authentication_form': self.login_form or AdminAuthenticationForm, 'template_name': self.login_template or 'admin/login.html', } return login(request, **defaults) Basically, it passes if request.method == 'GET' and self.has_permission(request): and sends me again … -
What is happening in this urlpatterns list of Python Django urls.py file?
I have three versions of urls.py file. Here are imports (shared between versions): from django.conf.urls.static import static from django.conf import settings from django.conf.urls import patterns, url from main import views Version 1. Everything works fine here. No problems running python2 manage.py runserver. urlpatterns = patterns( url(r'^bio$', 'views.bio_view'), ) Version 2. Hmm I need some more urls though. Let's add them. No problems here either. urlpatterns = patterns( '', url(r'^$', views.index, name='index'), url(r'^bio$', 'views.bio_view'), ) Version 3. Wait a sec... What is '' doing here? I don't actually need it. Let's remove it, shall we? urlpatterns = patterns( url(r'^$', views.index, name='index'), url(r'^bio$', 'views.bio_view'), ) And here is the issue after running the manage.py server: (Some of top django library calls ommitted) File "/home/konrad/workspace/mydir/myproject/urls.py", line 20, in <module> url(r'^', include('main.urls')), File "/usr/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 52, in include urlconf_module = import_module(urlconf_module) File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module __import__(name) File "/home/konrad/workspace/mydir/myproject/urls.py", line 15, in <module> url(r'^bio$', 'views.bio_view'), File "/usr/lib/python2.7/site-packages/django/conf/urls/__init__.py", line 91, in patterns t.add_prefix(prefix) File "/usr/lib/python2.7/site-packages/django/core/urlresolvers.py", line 232, in add_prefix self._callback_str = prefix + '.' + self._callback_str TypeError: unsupported operand type(s) for +: 'RegexURLPattern' and 'unicode' So... Actually the question is about the Version 2. - why is it fixing the Version 3. error? And … -
Django makemigrations not detecting project/apps/myapp
Project structure myproject/ myproject/ apps/ myapp/ __init__.py admin.py models.py urls.py views.py tests.py myapp2/ myapp3/ __init__.py static/ templates/ virtualenv/ manage.py myproject/apps/myapp/models.py from django.db import models class MyModel(models.Model): name = models.CharField(max_length=200) created = models.DateTimeField(auto_now_add=True) Settings.py INSTALLED_APPS = [ # ... 'apps.myapp', 'apps.myapp2', 'apps.myapp3', ] makemigrations cannot find "myapps" that's not in the project root. At the same time, migrate does find it. $ ./manage.py makemigrations apps.myapp App 'apps.myapp' could not be found. Is it in INSTALLED_APPS? $ ./manage.py migrate apps.myapp CommandError: App 'apps.myapp' does not have migrations. Isn't the "let's put our apps into an apps folder" practise valid any more, am I doing something wrong, or is it a bug of the makemigrations command? Note1: before running makemigrations, I removed the "migrations" folder of myapp, but I'm pretty sure it doesn't matter. It did get re-created when the app was in the project root. Note2: I did research google and stackoverflow, I only found similar questions where the solution was either "adding the app to settings.py", "running makemigrations before migrate" or "some migration issue between Django 1.6 and 1.7". None of these apply to my situation, I think. -
How can a class hold an array of classes in django
I have been having trouble using django. Right now, I have a messagebox class that is suppose to hold messages, and a message class that extends it. How do I make it so messagebox will hold messages? Something else that I cannot figure out is how classes are to interact. Like, I have a user that can send messsages. Should I call its mehod to call a method in messagebox to send a msg or can I have a method in user to make a msg diectly. My teacher tries to accentuate cohesion and coupling, but he neveer even talks about how to impelent this in django or implement django period. Any help would be appreciated. -
Aptana Eclipse - Custom tag color
I'm using Django with AngularJS. Because Angular and Django has the same brackets tags, I've changed Angulars's brackets to "[[" and "]]". How can I change the color style of new tag in Aptana Studio 3.0? -
Python Django Rest Post API without storage
I would like to create a web api with Python and the Django Rest framework. The tutorials that I have read so far incorporate models and serializers to process and store data. I was wondering if there's a simpler way to process data that is post-ed to my api and then return a JSON response without storing any data. Currently, this is my urls.py from django.conf.urls import url from rest_framework import routers from core.views import StudentViewSet, UniversityViewSet, TestViewSet router = routers.DefaultRouter() router.register(r'students', StudentViewSet) router.register(r'universities', UniversityViewSet) router.register(r'other', TestViewSet,"other") urlpatterns = router.urls and this is my views.py from rest_framework import viewsets from rest_framework.decorators import api_view from rest_framework.response import Response from .models import University, Student from .serializers import UniversitySerializer, StudentSerializer import json from django.http import HttpResponse class StudentViewSet(viewsets.ModelViewSet): queryset = Student.objects.all() serializer_class = StudentSerializer class UniversityViewSet(viewsets.ModelViewSet): queryset = University.objects.all() serializer_class = UniversitySerializer class TestViewSet(viewsets.ModelViewSet): def retrieve(self, request, *args, **kwargs): return Response({'something': 'my custom JSON'}) The first two parts regarding Students and Universities were created after following a tutorial on Django setup. I don't need the functionality that it provides for creating, editing and removing objects. I tried playing around with the TestViewSet which I created. I am currently stuck trying to receive JSON … -
Markdown install error
l have a problem with Markdown install and l m using Python3.5 version. By using pip install markdown and l got this error below: Command ""c:\users\ömer sarı\appdata\local\programs\python\python35-32\python.exe" -u -c "import setuptools, tokenize;file='C:\Windows\Temp\pip-build-zyp_5g0x\markdown\setup.py';exec(compile(getattr(tokenize, 'open', open)(file).read().replace('\r\n', '\n'), file, 'exec'))" install --record C:\Windows\Temp\pip-ckif2kvg-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Windows\Temp\pip-build-zyp_5g0x\markdown\ before that, l tried to install Django-markdown but it failed due to same problem above. How can l solve this issue? -
A form may include HTML attributes such as maxlength
Django 1.10. The documentation reads: "Just like when using a normal Form, each form in the formset may include HTML attributes such as maxlength for browser validation." (https://docs.djangoproject.com/en/1.10/topics/forms/formsets/#formset-validation) class ArticleForm(forms.Form): title = forms.CharField(max_length=5) Which results in: <table> <tr><th><label for="id_form-0-title">Title:</label></th><td><input id="id_form-0-title" maxlength="5" name="form-0-title" type="text" value="Test" /></td></tr> </table> Keywords: form may include HTML attributes such as maxlength. What astonishes me: this is not the form that may include maxlength. This is a field in Django and input tag in HTML. Well, a form can contain maxlength neither in Django nor in HTML as far as I know. Could you help me understand: is it correct to say that a form may include maxlength attribute? Is it a bad wording in the documentation? Or maybe a form really can contain such attributes. -
Deployment of Django app to AWS Lambda using zappa fails even though Zappa says your app is live at the following link
I came across the amazing serverless AWS Lambda recently and thought it would be great to have my app up there and not have to worry about auto scaling, load balancing and all for apparently a fraction of the cost. So then I found out about Zappa which takes care of deploying your python app to AWS Lambda for you. Amazing is what I thought. Its actually on paper very easy to do. just follow the instructions here.. https://github.com/Miserlou/Zappa Anyway I followed the instructions with just a very basic django app using virtualenv that just contained the django rest framework tutorial in it.. Tested it locally and works fine. Then I set up my s3 bucket and authenticated my credentials with the awscli. Then I ran the 2 thing you need to deply. Zappa init, Zappa deploy dev. Then it went through all its processes, packaging into zip, deploying etc... Then at the end it said your app is live and here is the url It gave me a url to try. I pasted the url into the browser and this is what the browser displayed for me. Oh yeah and my s3 bucket is still empty and so is … -
Ordering a ManyToMany By Relationship id
I currently have two models, that have an relationship to each other models.py class Key(models.Model): name = models.CharField(max_length=200) class Grouping(models.Model): name = models.CharField(max_length=100) keys = models.ManyToManyField(SecurityKey) and a admin page configuration, that shows the gorupings in the key page: admin.py class GroupingInline(admin.StackedInline): model = Grouping.keys.through extra = 1 class GroupingAdmin(admin.ModelAdmin): # fields to configure fieldsets = [ (None, {'fields':['name']}), ] class KeyAdmin(admin.ModelAdmin): # fields to configure and their options fieldsets = [ ('Key-Name', {'fields':['name']}), ] inlines = [GroupingInline] admin.site.register(Key, KeyAdmin) admin.site.register(Grouping, GroupingAdmin) In Admin-Page, i can directly add the relationship for key 2 grouping from the key admin page. That way, i also can decide the order of the groupings for the key. Now, is there any possibility to replicate this ordering in a template? Using {% for grouping in key.grouping_set.all %} seems to order the groupings by their id. -
Filter on boolean field in Django
On my view I only want to show the archived posts. As you can see below I have a model on a BooleanField. How do I have to define it in my view? Thanks class Post(models.Model): author = models.ForeignKey('auth.User') title = models.CharField(max_length=200) text = models.TextField() archived = models.BooleanField(default=False) created_date = models.DateTimeField(default=timezone.now) published_date = models.DateTimeField(blank=True, null=True) That is how I did it def post_list_archived(request): posts = Post.objects.filter(archived=True).order_by('published_date') return render(request, 'blog/post_list.html', {'posts': posts}) -
Parsing POST request - object() takes no parameters
I try to access post request and check if user with email that is in post request already exist. When I try to send data to endpoint, I get error TypeError: object() takes no parameters my views.py @csrf_exempt class CheckIfEmailAvailable(): @csrf_exempt def check(request): email = request.POST.get("email") if User.objects.filter(email=email).exists(): return Response({'status': 'not available'}) my url.py url(r'^api/checkmail/', CheckIfEmailAvailable, name='check'), What am I doing wrong ? -
Query Djnago manyTomany relationship
please i need help with just getting the result of querying my manyTomany fields . basically I have a the following models : class Book(models.Model): title = models.CharField(max_length=50) authors = models.ManyToManyField(Author) class Author(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=40) So how to query my book model to show all the authors of each book,based on the book_id and without the book_id, and how to query all the books that where written by one author.? i have tried to follow the Docs where pizza and Topping but im not getting , so thanks in advanced friends and have a good day .