Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to handle an extremely big table in a search?
I'm looking for suggestions on how to go about handling the following use case scenario with python django framework, i'm also open to using javascript libraries/ajax. I'm working with pre-existing table/model called revenue_code with over 600 million rows of data. The user will need to search three fields within one search (code, description, room) and be able to select multiple search results similar to kendo controls multi select. I first started off by combining the codes in django-filters as shown below, but my application became unresponsive, after waiting 10-15 minutes i was able to view the search results but couldn't select anything. https://simpleisbetterthancomplex.com/tutorial/2016/11/28/how-to-filter-querysets-dynamically.html I've also tried to use kendo controls, select2, and chosen because i need the user to be able to select as many rev codes as they need upward to 10-20, but all gave the same unresponsive page when it attempted to load the data into the control/multi-select. Essentially what I'm looking for is something like this below, which allows the user to select multiple selections and will handle a massive amount of data without becoming unresponsive? Ideally i'd like to be able to query my search without displaying all the data. https://petercuret.com/add-ajax-to-django-without-writing-javascript/ Is Django framework meant to … -
Django: Reusable App Code
Can anyone share the reusable app one is supposed to get after completing the official Django tutorial? 2.0 would be ideal! I tried searching on Github but wasn't able to find working projects. Alternatively, can you perhaps recommend some other starter project templates written in Django? Thank you! -
Django - Create a User instance from another model that uses User as a OneToOne
I think an example will explain it better. Lets say I am making an application for various Libraries to show what books they have available. The site will have Users that are registered through the built-in user registration system and User model. These Users (think of it like library members) can visit the Libraries and browse and checkout the available books. The site will also allow Libraries to register themselves, and part of that registration is declaring a "Librarian," a person who controls what books are available in their particular Library. I want to create a Library Registration Form that takes the information of this Library and its Librarian and not only creates an instance of the Library model, but also an instance of the User model. The Librarian automatically becomes a User. This is my current models.py: class Library(models.Model): name = models.CharField(max_length=200) website = models.URLField() street = models.CharField(max_length=200) city = models.CharField(max_length=200) state_or_province = models.CharField(max_length=200) postal_code = models.CharField(max_length=200) date_registered = models.DateField(auto_now_add=True) librarian = models.OneToOneField(User, on_delete=models.CASCADE) @receiver(post_save, sender=Library) def create_user(sender, instance, created, **kwargs): if created: User.objects.create(user=instance) instance.user.save() I am currently lost as to how to build views.py and forms.py. I am not even sure that model is built correctly, since I … -
TypeError at /en/ Object of type '__proxy__' is not JSON serializable
I'm trying to implement a breadcrumb in my django project. To archieve that, I've created a mixin which is inherited by every view I have. Here is the code: class BreadcrumbMixin(object): index = False url_name = None verbose_name = None def breadcrumbUpdate(self, breadcrumb, new_value): for i in range(len(breadcrumb)): if breadcrumb[i]['url_name'] == new_value['url_name']: breadcrumb = breadcrumb[:i + 1] return breadcrumb breadcrumb.append(new_value) return breadcrumb def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) new_value = { 'url_name' : self.url_name, 'verbose_name' : self.verbose_name, 'url' : reverse_lazy(self.url_name, **kwargs) } if self.index: self.request.session['breadcrumb'] = [new_value,] else: self.request.session['breadcrumb'] = self.breadcrumbUpdate(self.request.session['breadcrumb'], new_value) return context The attributes index, url_name and verbose_name are set in each view that inherits it. For instance: class Index(BreadcrumbMixin, TemplateView): template_name = 'crud/index.html' index = True url_name = 'index' # Name argument used in urls.py verbose_name = _('Index') It basically use these informations to make a list of dicts, which is passed to the template using the session dictionary. In the template, it is being called like this: <div style="margin-left: 10px;"> {% for element in request.session.breadcrumb %} <a href="{{ element.url }}" class="breadcrumb"> {{ element.verbose_name }} </a> {% endfor %} </div> The problem is, it's giving me the error mentioned in the title. Here is the traceback: Environment: … -
How references fields that are foreignkey in another table
Documents models.py ERD-Restaurants python3.6.4, django 2.0 Hello django pals, I need some help. Only business_id is not generated automatically (user_id and dish_id are generated automatically by django API) This is the Rating class(Rating Table) in model.py class Rating(models.Model): business = models.? r1 dish = models.? r2 user = models.ForeignKey(User, on_delete=models.CASCADE, primary_key=True) rating = models.IntegerField() comment = models.TextField() How can I references r1 with Price.restaurant, r2 with Price.dish? and set these 2 fields as primary key so that the 3 fields (business, dish and user) can form a composite key. Is valid use the argument , primary_key=True in ForeignKey fields as I am using there? Because I need to set that fields as primary key too. -
How to access the a reverse relation of a reverse relation django
I am determined to try get all the other users that replied to the same comment as a given reply. I have come up with the following python, however I am concerned this will do more than one DB call. other_repliers = [other_reply.user for other_reply in my_reply.comment.replies.filter()] I'm looking for a one-db-call fix. The Psuedo-SQL logic would be something like: SELECT u.* FROM users AS u INNER JOIN replies AS r ON r.user = user.id WHERE r.comment = {my_reply.comment} AND r.user != {my_reply.user} Does anyone know a way that I can do the above SQL in a single query? -
"Invalid HTTP_HOST header" from unknown domain
I am using Nginx and Gunicorn to host a Django project. I need to secure this site, and as a test I set up Let's encrypt to an unused domain of mine. While tailing the Django access log, I noticed the following entry from time to time: Invalid HTTP_HOST header: 'aydinfatih.com'. You may need to add u'aydinfatih.com' to ALLOWED_HOSTS. This is an unknown domain to me, and while trying to access the domain (it got 400 response), I could se more of these log entries on my server. What is this? Is it related to my SSL-setup, and an indication that it's not secure? server { server_name example.com example.com; location /static/ { root /home/user/project/django-project; } location /media/ { root /home/user/project/django-project; } location / { include proxy_params; proxy_pass http://unix:/home/user/project/project.sock; } listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot if ($scheme != "https") { return 301 https://$host$request_uri; } # managed by Certbot } server { listen 80; server_name my.server.ip.here example.com; return 301 https://example.com; } I added the following to my server block: if ($host !~* ^(example.com|www.example.com)$ ) { return … -
Django How to save just one field of form
I want to save a field from form by radio button and others from excelfile the excel file imports correct by itself but when i want to save a file from form, it doesnt work My view is : def ca_import(request): uploadform=UploadFileForm(None) if request.method == 'POST' : uploadform = UploadFileForm(request.POST, request.FILES) if uploadform.is_valid(): file = uploadform.cleaned_data['docfile'] workbook = openpyxl.load_workbook(filename=file, read_only=True) first_sheet = workbook.get_sheet_names()[0] worksheet = workbook.get_sheet_by_name(first_sheet) data = [] try: for row in worksheet.iter_rows(row_offset=1): stock = Stocks() stock.user = request.user stock.name = row[0].value stock.number = row[1].value stock.suffix = row[2].value stock.brand = row[3].value.upper() stock.comment = row[4].value stock.price = row[5].value stock.seller = uploadform.cleaned_data.get("seller") data.append(stock) print(uploadform.cleaned_data.get("seller")) #it print the correct value but not save # Bulk create data Stocks.objects.bulk_create(data) #save from form instance=uploadform.save() instance.user=request.user instance.save() messages.success(request,"Saved Successfully" ,extra_tags="saveexcel") except : messages.error(request,_('Error! '),extra_tags="excelerror") return render(request, 'BallbearingSite/excelfile.html',{'uploadform':uploadform}) -
Django formset, using a form to do two different actions
My models Agency has manytomany sell_houses and manytoamany buy_houses, (they have slightly different information in each model). a Member only belongs to one Agency. I want to be able to populate a formset on a page that shows all the Members that belong to one Agency and be able to choose on a form either to sell_house or buy_house but can not do both at the same time. forms.py class ManageHouseForm(forms.Form): buy_house = forms.ModelChoiceField( label="buy a house", queryset=None, ) sell_house = forms.ModelChoiceField( label="sell a house", queryset=None, ) def __init__(self, agency, *args, **kwargs): super(ManageHouseForm, self).__init__(*args, **kwargs) self.fields["buy_house"].queryset = agency.sell_houses self.fields["sell_house"].queryset = agency.buy_houses template.htm <form method="post"> {% csrf_token %} <table....>{{formset.management_form}} {% for form in formset.forms %} {% for member in members %} <tr> <td>{{member.name}}</td> {% for field in form.visible_fields %} <td> {{ field }} </td> {% endfor %} </tr> {% endfor %} </tbody> {% endfor %} </table> views.py def agency_member_manage(request, agency_name): template = "template.htm" agency = get_object_or_404(models.Agency, name=agency_name) members = models.Machine.objects.filter(group=machine_group) ManageHouseFormSet = formset_factory(wraps( forms.ManageHouseForm)(partial(forms.ManageHouseForm, agency=agency))) if request.method == "POST": formset = ManageHouseFormSet(data=request.POST) if formset.is_valid(): for form in formset: action_buy_house(member, form) #do something with members else: formset = ManageHouseFormset() context = {"members": members,"agency":agency,"formset": formset,} return render(request, template, context=context) My biggest problem is … -
In a Django project with different versions of settings.py, can I use a dict.update() method on a setting?
I'm working on a project with multiple settings files, similar to https://medium.com/@ayarshabeer/django-best-practice-settings-file-for-multiple-environments-6d71c6966ee2. There is a settings directory with multiple settings files: settings ├── base.py ├── development.py ├── production.py └── staging.py The base.py contains the default permissions setting for the Django REST framework: REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.IsAuthenticated', ], } In production, I would like to use the default JSON renderer instead of the browsable API. So in production.py I have something like this: from .base import * REST_FRAMEWORK.update({ 'DEFAULT_RENDERER_CLASSES': ( 'rest_framework.renderers.JSONRenderer' ) }) I find this more DRY than defining a REST_FRAMEWORK setting in every file. The question is: will this work? Can you update an imported setting dictionary in a settings.py? -
Connect to API endpoint from Django and returning data
I have a django form which at the moment checks for postcodes in areas that the app can deliver. I would like to integrate it with postcodes.io API endpoint to check that the actual postcode is valid. The endpoint is as below: api.postcodes.io/postcodes/:postcode/validate This is my form code: from django import forms class PostCodeForm (forms.Form): pcode = forms.CharField() def clean_pcode(self): permitted = {'gu15','GF34','FG34','BT25'} pcode = self.cleaned_data['pcode'].lower() if not pcode[:4] in (permitted): raise forms.ValidationError("Apologies, but does not currently deliver to you postcode.") return pcode I am stuck in terms of how to integrate with the API endpoint from Django. Any help is really appreciated. Thanks in advance. -
force Django tests to write models into database
I am setting up a very simple Django project and with a very simple test like: def test_name(self): t = Thing.objects.create(name='a') print(t.id) import time time.sleep(30) self.assertEqual('a', t.name) The tests passes of course, but the database (TEST database) doesn't get populated with my Thing model information, even though I can actually see its id as you can see my script. When I connect into the database, the Thing table is always empty (I also saw some comments about it in the documentation). Now, how can I tell Django to actually populate the data? I am using mysql, and checking the complete logs I see Django is creating a SAVEPOINT before populating data (that is not committed) and once the test passes, it goes back into that previous SAVEPOINT. I am using: Django==2.0.1 mysqlclient==1.3.12 pytest==3.3.2 pytest-django==3.1.2 I would like Django to actually populate the TEST database, with the information from my tests and at the end drop that database, that's why I am assuming Django is creating a completely new database just for tests. -
form for simulataneously editing two django models with foreign key relationship
I am trying to find a simple way to create a form that allows to edit two models with foreign key relationship simultaneously. After some research, it seems that Inline formsets come very close to what I want to do. The django documentation offers this example: from django.db import models class Author(models.Model): name = models.CharField(max_length=100) class Book(models.Model): author = models.ForeignKey(Author, on_delete=models.CASCADE) title = models.CharField(max_length=100) And then, >>> from django.forms import inlineformset_factory >>> BookFormSet = inlineformset_factory(Author, Book, fields=('title',)) >>> author = Author.objects.get(name='Mike Royko') >>> formset = BookFormSet(instance=author) Let's suppose Author has a second field, city. Can I use the fields argument to add city to the form? If inline formsets are not the way to go, is there a simple other way that generates this joint form? -
Django ORM: How to find all instances with unique names which has the lowest value?
I've got queryset with 50+ instances: products = Product.objects.filter(...).order_by['price'] Product model has: store = models.ForeignKey('Store',...) Each store has 10 products in the current queryset and there are 5 different stores in total. I'm trying to get one product from each store with a minimum price -
How do I correctly import my auth class
I created my own auth 'AuthOnPostOnly, but I am having trouble correctly importing it. note: I created an app called 'api'. also left out init.py from all directories in this example but they are all there. settings.py INSTALLED_APPS = [ 'pipeline.apps.PipelineConfig', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # for testing 'django_nose', # thirdparty app 'bootstrap3', 'crispy_forms', 'bootstrap_pagination', 'mobilereports', 'rest_framework', 'api' ] REST_FRAMEWORK = { 'DEFAULT_PERMISSION_CLASSES': ( 'api.permissions.AuthOnPostOnly', ) } directory structure /proj /api urls.py views.py serializers.py /permissions permissions.py /proj /settings.py /urls.py error: Could not import 'api.permissions.AuthOnPostOnly' for API setting 'DEFAULT_PERMISSION_CLASSES'. AttributeError: module 'api.permissions' has no attribute 'AuthOnPostOnly' -
Dynamically add image to django page via javascript?
I'm trying to add an image to a django page via Mapbox's API, which has the following syntax: new mapboxgl.Popup() .setLngLat(features[0].geometry.coordinates) .setHTML(" <img src=\"../images/location_icon.png\" />") .addTo(map); Unfortunately, that doesn't seem to work. I assume that because we're skirting around the whole idea of templates (by dynamically adding the image), I don't need to use {% %} , but I'm not entirely sure about that. I've tried this as well, but to no avail: .setHTML(" <img src='{%\"images/location_icon.png\"%}' />") Does anybody know what the proper syntax is for accomplishing this? -
Annotate many to many with initial model django
I have these two models class Word(Model): word = CharField() categories = ManyToManyField('Category') class Category(Model): name = CharField() What I want to do is to output number of words for each set of categories: General (3 words) General, IT (7 words) Medicine (1 word) Archaic Words, IT (10 words) This means, that, for example, there are 3 words, that have only one category, namely General. What is the way to accomplish it in the smallest possible number of queries? First step is to fetch all possible sets, I use PostgreSQL: all_sets = Word.objects.annotate(category_names=ArrayAgg('categories__name')).values_list('category_names', flat=True).distinct() What next? I can, of course, fetch the number of words for every set: for category_set in all_sets: queryset = Word.objects.annotate(n=Count('categories')).filter(n=len(category_set)) for cat in category_set: queryset = queryset.filter(categories__name=cat) But I will hit the database for the every set of categories. Can this be improved? -
Django CountryField COUNTRIES_OVERRIDE & COUNTRIES_FIRST -- NOT WORKING
CountryField is working, but "United States of America" is too long, I prefer just "United States" or even "USA". Also, I want USA & GB at the top of the list when you pull down the pull down. When I first implemented COUNTRIES_OVERRIDE & COUNTRIES_FIRST, they were working. Then suddenly they stopped working, and have not worked since. I have been pulling my hair out! (Not literally.) In the models.py file where I import CountryField, I also import settings from django_countries.conf. Below the import lines, and above the model defintion that uses CountryField, I have these lines: settings.COUNTRIES_FIRST = [ 'US', 'GB' ] settings.COUNTRIES_OVERRIDE = { 'US': 'USA' } Troubleshooting tips would be appreciated. -
In the Django REST framework, how to allow partial updates when using a ModeViewSet?
I'd like to create a REST API for an object which can be partially updated. On http://www.django-rest-framework.org/api-guide/serializers/#partial-updates and example is given in which partial=True is passed when instantiating the serializer: # Update `comment` with partial data serializer = CommentSerializer(comment, data={'content': u'foo bar'}, partial=True) In my case, however, the model (which is called SessionType) has the following viewset: class SessionTypeViewSet(viewsets.ModelViewSet): queryset = SessionType.objects.all() serializer_class = SessionTypeSerializer where the serializer is defined as class SessionTypeSerializer(serializers.ModelSerializer): class Meta: model = SessionType fields = ('title',) How can I adapt the serializer in this use case so that partial is always True? -
Django Forms, how to validate form with startswith
I am trying to work out how to get the following bit of code working so that everything that starts with the tuple is valid, e.g. if the postcode 'GU15 56L' is entered it permits the postcode. Currently only the GU15 part is working (for this example). Something like start with but not sure where to start. class PostCodeForm (forms.Form): pcode = forms.CharField() def clean_pcode(self): permitted = {'GU15','GF34','FG34','BT25'} pcode = self.cleaned_data['pcode'].lower() if not pcode in (permitted): raise forms.ValidationError("Apologies, but does not currently deliver to you postcode.") return pcode Thanks for your help in advance -
Best approach to using unittest with Django
This may seem a very general question, but I hope not. I need to write tests for a Django backend before writing the Django code and then testing it. One of the first problems I can see is that this might use a lot of resources and be extremely slow. What is the standard way to test Django code? Do people write code snippets and test them individually before incorporating them into the whole or is there a better way or better framework than unittest? Is there a Django specific testing framework? Before you answer, I don't have much experience with Django, but I can learn if you point me to the right tutorials. Put simply, what is the best way to test Django code? -
django HTTP 405 Method Not Allowed in html form
I am new to Django and I've been trying to develop a simple site that asks the user for their email address and height. It then saves it in the database and sends an email to the user and redirects them to a page saying that it was successful. Now the problem is whenever I press 'Submit', I get a HTTP 405 method not allowed error. # urls.py urlpatterns = [ url(r'^$', views.IndexView.as_view(), name='index'), #url(r'^success/$', views.SuccessView.as_view(), name='success'), ] # forms.py class HeightForm(forms.ModelForm): class Meta: model = Height fields = ['email', 'height'] # views.py class IndexView(generic.ListView): form_class = HeightForm template_name = 'heights/index.html' def get_queryset(self): return Height.objects.all() class HeightFormView(View): form_class = HeightForm template_name = 'heights/success.html' def get(self, request): form = form_class(None) def post(self, request): print('a' * 1000) form = form_class(request.POST) if form.is_valid: email = form.cleaned_data['email'] height = form.cleaned_data['height'] form.save() return HttpResponseRedirect(template_name) #render(request, template_name, {'form': form}) # index.html {% extends 'heights/base.html' %} {% block body %} <h1>Collecting Heights</h1> <h3>Please fill the entries to get population statistics on height</h3> <form action="" method="post"> {% csrf_token %} <input type="email" name="email" placeholder="Enter your email address" required="true"/><br /> <input type="number" min="50" max="300" name="height" placeholder="Enter your height in cm" required="true" /><br /><br /> <input type="submit" name="submit" /> </form> <a … -
Different ways of calling render() in Django
I am having some difficulty understanding how render() works. I have read posts about the functionality of the function, however still have unanswered questions. Reading about it in the Django Docs, and looking at the source, it is not a method pertaining to any class. Instead, it is a function just like many others, and one that takes several arguments (request, template_name, etc). Though, in the Django Book, it is stated as a method you can call on a template object. That is, you can instantiate an object, and immediately call the render method on that object with some context. I have previously used it without specifying any template name, merely calling it on a template object with some context, as previously described. Why do the Docs describe it as a free-standing function, not belonging to a class, when it can be called on a template object like as if it was a method of a template class? Why don't I find a template class anywhere? What am I missing? -
How do I access my virtual environment when I go into Terminal (Mac)?
I was going through a tutorial on Django for the first time and had to restart my computer. After it restarted, I lost access to my virtualenv instance in terminal. Can anyone help get me back in? I can see the virtual environment. screenshot of Mac Terminal Location of virtual server on Mac -
Packaging Django code for deployment
I'm getting ready to move my Django project from my laptop to my server. What is the recommended way to do this? E.g., is there a Django command that will package everything up (and select the correct settings file for test vs prod servers) and create a zip or tar file that can be moved over to the server? Sort of like Ant for building Java projects.