Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Efficient geojson queries with massive datasets
How can I efficently query massive amounts of geospatially linked data (10s of millions of rows) which are bound to some spatial region and needs a cloropleth / ranking metric applied to them. Naturally, this requires mathematical functions on the properties of teh region itself. So, we have 30k+ zip codes, demographics for each of this, need to apply metrics to items inside of this demographic data and the demographic data itself is significant. Seems there's no way around this being incredibly slow. I can layer things. Do the tiling map, then grab the zip code polygons. Then once those features are loaded I can do a callback within the bounding box to grab the demographics attached to those regions, then apply the metrics in the frontend / javascript. But, that's only localized regions. What happens when you zoom out and you're looking at an entire country. That's massive amounts of data. -
Django Rendering Forms Manually and using csfr token
I think I am slowly getting the hang of Django, but am relatively new and self taught at programming. I have been using form.as_p and form_ul in templates, but wanted to get a better understanding of how to make them look better. I looked at the Django documentation of how to manually render forms. That seems to work as advertised, and I can show the individual fields etc. on my html page. However in the documentation this is highlighted: Forms and Cross Site Request Forgery protection Django ships with an easy-to-use protection against Cross Site Request Forgeries. When submitting a form via POST with CSRF protection enabled you must use the csrf_token template tag as in the preceding example. However, since CSRF protection is not directly tied to forms in templates, this tag is omitted from the following examples in this document. I don't think I understand what is meant by the last line. I assume it means that I can render the form all I want, but unless there is a Post request being made I don't need a CSRF token. Is there an example of how to manually render forms with post requests and CSRF tokens? I am … -
How to add Google Calendar to a Django Webapp which is already using Django-allauth google social login
I am trying to add a google calendar to my webpage for a user who has logged into the website using a google account via the Django-allauth package social login. I have set up the social login, and I am able to login and authenticate users. Next step will be to add a calendar to the webpage, since I have already logged in with a google account to my website. How do I go about adding a google calendar? -
Alternate rows of a queryset from different order_by() clauses
I'm trying to implement a custom view where posts are alternated between newest and most voted: | post | vote_score | created_at | ==================================== | A | 20 | 01/01/2020 | ---> Top voted | F | 0 | 05/01/2020 | ---> Newest | I | 19 | 02/01/2020 | ---> Second to Top Voted | B | 2 | 04/01/2020 | ---> Second to Newest I know I could just run 2 queries, one sorting by vote_score and another one by created_at and then interleave the rows in Python. I was wondering if there was a more efficient way to do this in the database so that I could use LIMIT and OFFSET to support simple paging on my website. I was playing with annotate, Window() and RowNumber() but I couldn't make it work: qs.annotate(row_number=Window( expression=RowNumber()*2, order_by=F('created_at').desc()) ).annotate(row_number=Window( expression=RowNumber()*2+1, order_by=F('vote_score').desc()) ).order_by('row_number') -
Is there a better way to create model while the database already have the table
In my MySQL database I already have a table fo_dic. mysql> desc fo_dic; +----------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +----------+-------------+------+-----+---------+----------------+ | id | int(11) | NO | PRI | NULL | auto_increment | | name | varchar(30) | YES | | NULL | | | desc | text | YES | | NULL | | | author | varchar(20) | YES | | NULL | | | priority | int(5) | YES | | NULL | | +----------+-------------+------+-----+---------+----------------+ Now I want to create the FoDic model for Django Rest Framework to ORM the data. from django.db import models class FoDic(models.Model): id = models.IntegerField() name = models.CharField() desc = models.TextField() author = models.CharField() priority = models.IntegerField() def __str__(self): return self.name def __unicode__(self): return self.name I want to know whether this will generate migrations when I sync the database, then there will have conflict? is there a way to create the Model as the same structure as the MySQL table? -
django cannot view database items in template
I have data that I insert into my database but cannot view the items. I can see a index, but no data. $ python3 manage.py shell Python 3.7.3 (default, Dec 20 2019, 18:57:59) [GCC 8.3.0] on linux Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) >>> from userdash.models import AssetList >>> ls = AssetList.objects.get(id=2) >>> ls <AssetList: Mike's List> >>> ls.items_set.all() <QuerySet [<Items: Batcoin>, <Items: Drum set>, <Items: Dildo>, <Items: Koodie>]> >>> ls.items_set.create(user_asset="5th Item", sell_asset=False) in userdash/templates/userdash/list.html {% extends 'userdash/base.html' %} {% block title %} <h1>List Page</h1> {% endblock %} {% block content %} <h2>{{ls.name}}</h2> <ul> {% for item in ls.items_set.all %} <li>{{items.user_asset}}</li> {% endfor %} </ul> {% endblock %} in exchange2/userdash/models.py from django.db import models # Create your models here. class AssetList(models.Model): name = models.CharField(max_length=200) def __str__(self): return self.name class Items(models.Model): currencylist = models.ForeignKey(AssetList, on_delete=models.CASCADE) user_asset = models.CharField(max_length=300) sell_asset = models.BooleanField() def __str__(self): return self.user_asset output: html output The ul output indicates data is there, but it's not displaying it. How do I properly display my database query in a django template? -
Django: Getting a reverse error, but data still goes through the database
I'm getting a reverse error when redirecting to the details page after filling out the form (and the information actually going to my db). I'm not sure how to fix it since it was working before, so it must've somehow broke in the middle while coding. The error that I'm getting : NoReverseMatch at /finches/create/ Reverse for 'finches_detail' with keyword arguments '{'pk': 20}' not found. 1 pattern(s) tried: ['finches/(?P<finch_id>[0-9]+)/$'] Here's part of my models.py: class Finch(models.Model): name = models.CharField(max_length=100) breed = models.CharField(max_length=100) description = models.TextField(max_length=250) age = models.IntegerField(default=0) toys = models.ManyToManyField(Toy) def __str__(self): return self.name def get_absolute_url(self): return reverse('finches_detail', kwargs={'pk': self.id}) It should return /finches/ upon creation, but it doesn't. My views.py class and function: finch = Finch.objects.get(id=finch_id) toys_finch_doesnt_have = Toy.objects.exclude( id__in=finch.toys.all().values_list('id')) feeding_form = FeedingForm() return render(request, 'main_app/finch_detail.html', { 'finch': finch, 'feeding_form': feeding_form, 'toys': toys_finch_doesnt_have }) class FinchCreate(CreateView): model = Finch fields = ['name', 'breed', 'description', 'age'] and the urls.py path('finches/<int:finch_id>/', views.finch_detail, name='finches_detail'), path('finches/create/', views.FinchCreate.as_view(), name="finches_create"), I'll update more info if it's needed. Upon creating, I notice that the link is still at finches/create even pressing submit. Any ideas on what's happening or any solutions? Thanks -
Path / URL error in application created for Django/Python course I'm taking
This is the error I receive from the browser This is the code of the urls.py file from django.urls import path from blog import views urlpatterns = [ path('',views.PostListView.as_view(),name='post_list'), # sets home page to all current blogs that are published path('about/',views.AboutView.as_view(),name='about'), path('post/<int:pk>',views.PostDetailView.as_view(),name='post_detail'), # will match primary key to whatever we click on path('post/new/',views.CreatePostView.as_view(),name='post_new'), path('post/<int:pk>/edit/',views.PostUpdateView.as_view(),name='post_edit'), path('post/<int:pk>/remove/',views.PostDeleteView.as_view(),name='post_remove'), path('drafts/',views.DraftListView.as_view(),name='post_draft_list'), path('post/<int:pk>/comment/',views.add_comment_to_post,name='add_comment_to_post'), path('comment/<int:pk>/approve',views.comment_approve,name='comment_approve'), path('comment/<int:pk>/remove',views.comment_remove,name='comment_remove'), path('post/<int:pk>/publish',views.post_publish,name='post_publish'), ] Please help! Thanks! -
How to implement Many to many relation in django when the relation contain attributes?
I'm begginer at django and I knew that you can use ManyToManyField to link two models like author-post user-groups. so I have this relation in my UML How can I implement this in my code ? my shot was like this class User(models.Model): # user fields class Group(models.Model): # group fields class GroupMember(models.Model): group = models.ForeignKey(Group, on_delete=models.DO_NOTHING, db_index=True) user = models.ForeignKey(User, on_delete=models.DO_NOTHING, db_index=True) IsAdmin = models.BooleanField(default=False, verbose_name='Est un admin') class Meta: unique_together = (("group", "user"),) I want to use ManyToManyFields so I can refer between them (if I haven't IsAdmin field, I don't have to create the third class 'GroupMember') -
Python Django - ModuleNotFoundError: No module named 'picamera'
I'm making my first Django-based web page, and trying to live stream video from my raspberry pi to said web page, but for some reason, I can't import the 'picamera'-module in Django... I have written a script that outputs the camera-feed, and it works fine when i run it outside of the server, but when i import my camera-script to views.py, i get the error message ModuleNotFoundError: No module named 'picamera'. Does anyone know why? All the other modules i'm using works fine both in and outside of Django. Importing my own modules also works fine. I'm certain that i have installed the picamera-module (https://picamera.readthedocs.io/en/release-1.13/) correctly, I even tried removing and reinstalling it. I got the same error when trying to import NumPy(https://pypi.org/project/numpy/)... I am using a virtual environment. Don't know if that may be a factor, but i thought I'd mention it, just in case. Eternally grateful to anyone who can help! -
Cannot access Django Model from Celery in Docker
I've been working on this problem most of the day and I am hoping someone can point me in the right direction. I am building an application with django and I started messing around with deploying it to docker and using celery to schedule tasks. I have celery running a custom manage.py command using call_command once per minute. Everything is working! Except I cannot seem to access django models from my custom django admin command. I have tried to import the model inside my function, and also use get_model to no avail. When I do Model.objects.all() the query is always empty. When I try to grab a specific Object with Mode.objects.get(id=##) it gives me an error. The command works perfectly when I call it using docker-compose -f docker-compose.yml exec web python manage.py [custom-command] but not when run by celery. Hopefully, someone out there knows of a fix because From when I have read so far it seems like this should be working. Alright, enough talking here is some code. Here is my docker-compose file for celery redis: image: redis:alpine celery: build: ./app command: celery -A [appname] worker -l info volumes: - ./app/:/usr/src/app/ environment: - DEBUG=1 - SECRET_KEY=[secret-key] - DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 … -
I have a question about how to get data from HTML in Django
So my problem is that when I render my home page in Django I make a request to a movie API and display the top-rated 20 movies. Now I want to display each movie as it's a poster and that poster would be clickable and would take me to a page for that specific movie. What I need to do (please let me know if there is a better way to do that in Django) is get the information that I have passed to the render function back in my next movie function. def home (request): r = requests.get("https://api.themoviedb.org/3/movie/top_rated?api_key=key=en-US&page=1") movies_json = r.json() context = { 'movies' : movies_json['results'] } return render(request, 'movies/home.html', context) HTML <div class="row"> {% for movie in movies %} <div class="column"> <a href = "/movie" class = "column col-xs-6" id = "sepia"> <img src = "https://image.tmdb.org/t/p/w500{{movie.poster_path}}" style="width:50%; height:75%;" alt="..."> <span class="caption">{{movie.title}}</span> <span class="caption">{{movie.id}}</span> </a> </div> {% endfor %} </div> </div> What I want to do is get that movie.id in my next function which will make another request to the API with that specific id so it would display the right page. -
How to use custom Django Authentication with it's rest TokenAuthentication
So I am using a third party Authentication (Firebase Auth)(Facebook && Google) with my Django Application. I did set up some rest endpoints that need authentication to post some content. I will very well like to use the Django Rest Framework Token Authentication. However, this requires that I have to pass a username & password in other to get the token. Since I am using Firebase(Facebook and Google), users are not asked to set any password. Is there any workaround or ideas on this? -
Error django framework UnicodeEncodeError /xel in position 3
I have the following error when deploying to a vps of a django-framework projectenter image description here -
Django 3.0 - Multilingual models - How to make __str__ return value for current, in-use language?
I am developping a Django 3.0 application. This application has to support two languages, French and English. I don't foresee it supporting any other language in the future. Given that I only have two language, I want to support them directly in the models and not resort to apps that do funky things like adding them on the fly (for performance and simplicity reasons). Now, let's say I have a model class that looks like this: class Organization(models.Model): name_en = models.CharField(max_length=100) name_fr = models.CharField(max_length=100) def __str__(self): # how do I return the name_en or name_fr, based on active language ? I'm guessing this might have to do with lazy_get_text, but I feel like I'm missing something. Also, in templates, aside from blocktrans to display/format fields based on active language, is there something else I should be aware of ? -
Generating SQL for Django pre/post-migration hooks
I am trying to move away from running django migrations to avoid locked tables/downtime during deploys and instead use SQL via Online Schema Migrations. I want to continue using manage.py makemigrations to generate app/migrations/0001_migratation.py files in order to continue tracking model state and will continue to run these migrations on my development machine. But I wanted to use manage.py sqlmigrate <app> <migration.py> to generate SQL associated with my migration to run in my TEST and PROD environments. However, the issue that I am running into is that sqlmigrate only returns the SQL associated with the python migration file, and does not take into account any SQL that is run as part of callbacks listening to pre_migration and post_migration signals emitted during manage.py migrate. One example of such a listener is the django.contrib.auth app which ensures that the db is in a consistent state by adding any missing rows to the auth_permission and django_content_type tables; the SQL for which does not appear in the output of the manage.py sqlmigrate <app> <migration.py> command. Is there a standard way of capturing the SQL of these "side effects" of the manage.py sqlmigrate command for django apps that do not run migrations in non-development environments? -
Manager object has no attribute 'get_by_natural_key'
I am trying to set up a custom User Model in Django, and believe that the vast majority of my coding is correct, however I am greeted with the following error: 'Manager' object has no attribute 'get_by_natural_key' models.py from django.db import models from django.contrib.auth.models import AbstractBaseUser, BaseUserManager from django.core.validators import RegexValidator USERNAME_REGEX = '^[a-zA-z0-9.+-]*$' class MyAccountManager(BaseUserManager): def create_user(self, email, first_name, last_name, username, password=None): if not email: raise ValueError("Users must have an email address") if not first_name: raise ValueError("Users must have a first name") if not last_name: raise ValueError("Users must have a last name") if not username: raise ValueError("Users must hae an username") user = self.model( username=username, email=email, first_name=first_name, last_name=last_name, ) user.set_password(password) user.save(using=self._db) return user def create_superuser(self, email, username, password): user = self.create_user( username, email, first_name, last_name, password ) user.is_admin = True user.is_Staff = False user.is_superuser = True user.save(using=self._db) return user def get_by_natural_key(self, email_): return self.get(code_number=email_) def __str__(self): return self.email def get_short_name(self): # The user is identified by their email address return self.email def has_perm(self, perm, obj=None): "Does the user have a specific permission?" return True def has_module_perms(self, app_label): "Does the user have permissions to view the app 'app_label'?" return True class Account(AbstractBaseUser): objects = MyAccountManager() email = models.EmailField( verbose_name="email address", … -
DJANGO - accesing some user's profile
I'm working on web app and currently I'm developing the profiles part. I've written part to view someones profile and this basically works fine except one thing. I can open someones profile but when I click the tabs which redirects to different part of the profile (these parts are included into block to keep the profile's main information), my code instantly switches to the currently loged user. Is there any way to get the user based on the cookies ? this is the single advert: def view_advert(request, pk, title): advert = Advert.objects.get(pk=pk) return render(request, "advert.html", {'advert':advert}) html <small>from </small><a href="{% url 'profile' nick=advert.user.username %}">{{advert.user}}</a> I open the profile of person who posted the advert (it works) Here is one of the profile and tab: @login_required def profile(request, nick): user = User.objects.get(username=nick) user_profile = Profile.objects.get(user=user) return render(request, "users_templates/profile.html", {"user_profile":user_profile}) def profile_adverts(request, user): user = User.objects.get(username=user) adverts = Advert.objects.filter(user=user) context = { "objects":adverts, "no_data":"No other adverts", "user":user } return render(request, "profile_templates/general_list.html", context) html from profile.html to access tab: <a class="nav-link" href="{% url 'all_adverts' user=user %}" role="tab" aria-selected="false">All adverts</a> Appreciate for help. -
ImproperlyConfigured: Field name first_name is not valid for model CustomUser?
help, Registrations, login, logout is working perfectly but when I try the url http://127.0.0.1:8000/user/ it is showing me this error. Where am I wrong in which step of the program. ImproperlyConfigured: Field name first_name is not valid for model CustomUser? model.py from django.db import models from django.contrib.auth.models import AbstractBaseUser from django.utils.translation import ugettext_lazy as _ from .managers import CustomUserManager class CustomUser(AbstractBaseUser): username = models.CharField(_('username'), max_length=100,unique=True) email = models.EmailField(_('email address'), unique=True) is_staff = models.BooleanField(default=False) is_admin = models.BooleanField(default=False) is_superuser = models.BooleanField(default=False) is_active = models.BooleanField(default=True) last_login = models.DateTimeField(auto_now=True) date_joined = models.DateTimeField(auto_now_add=True) full_name = models.CharField(_("Full Name"), max_length=50, null=True) date_of_birth = models.DateField( auto_now=False, auto_now_add=False, null=True) is_verified = models.BooleanField(_("Verified"), default=False) USERNAME_FIELD = 'email' REQUIRED_FIELDS = ['username'] objects = CustomUserManager() def __str__(self): return self.username def has_perm(self,perm,obj=None): return self.is_admin def has_module_perms(self,app_label): return True`from rest_framework import serializers from rest_auth.registration.serializers import RegisterSerializer serializer.py class RegisterSerializer(RegisterSerializer): full_name = serializers.CharField() date_of_birth = serializers.DateField() is_verified = serializers.BooleanField(read_only=True,default=False) def get_cleaned_data(self): super(RegisterSerializer, self).get_cleaned_data() return { 'username': self.validated_data.get('username', ''), 'password1': self.validated_data.get('password1', ''), 'email': self.validated_data.get('email', ''), 'date_of_birth': self.validated_data.get('date_of_birth', ''), 'full_name': self.validated_data.get('full_name', ''), 'is_verified': self.validated_data.get('is_verified', '') } setting.py import os # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) # Quick-start development settings - unsuitable for production # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/ # SECURITY WARNING: keep … -
Why can't we use reverse side of ForeignKey relationship in a Django Form?
I'm running into a problem when using a ModelForm and I want to know if I'm crazy or not before I go off making some complicated work-around Use Case: I have the following model definitions: class Setting(models.Model): """Manufacturing settings - can be shared among materials to be manufactured""" ... class Material(models.Model): """Specific record for a material that is manufactured""" ... setting = models.ForeignKey('Setting', on_delete=models.SET_NULL, related_name='materials') class ManufacturingRun(models.Model): """An instance of a manufacturing run""" material = models.ForeignKey('Material', on_delete=models.SET_NULL, related_name='manufacturing_runs') In this application, the data in the Material model is coming from an outside source and we do not provide users the ability to edit these records. However, the setting_id field is not populated as part of the data received from this outside source. Basically I need my users to define Setting instances and be able to assign each record to multiple Material instances. To that end I define a Setting form like so: class SettingForm(forms.ModelForm): class Meta: model = Setting fields = [ ... 'materials' ] Problem: Everything seems fine until I access the View to create a new Setting, thereby triggering the instantiation of a SettingForm object. The server returns django.core.exceptions.FieldError: Unknown field(s) (materials) specified for Setting I've become so … -
Django Form to update another app's database records
I want to be able to update the user app's Profile table favorite_subjects and favorite_courses records from a form inside of my memberships app. This will then allow the {{ user.profile.favorite_subjects }} and {{ user.profile.favorite_courses }} values to change in my template after the form submission. I am not sure how to do that and whether to use an instance variable anywhere in the view. Also not sure how to populate the form with the current values of favorite_courses and favorite_subjects. User app - models.py Profile table: class Profile(models.Model): user = models.OneToOneField(User, related_name='profile', on_delete=models.CASCADE) favorite_subjects = models.CharField(max_length=20, unique=True) favorite_courses = models.CharField(max_length=20, unique=True) def __str__(self): return self.user.username --------------- Memberships app - memberships/profile.html: {{ user.profile.favorite_subjects }} {{ user.profile.favorite_courses }} <a href="{% url 'memberships:interest_view' %}">Change Interests</a> --------------- Memberships app Urls.py: from .views import ( interest_view ) path('interest_view/', interest_view, name='interest_view'), --------------- Memberships app Forms.py: class InterestsForm(forms.ModelForm): captcha = CaptchaField(widget=CustomCaptchaTextInput) class Meta: model = Profile fields = ('favorite_subjects', 'favorite_courses',) def __init__(self, *args, **kwargs): super(InterestsForm, self).__init__(*args, **kwargs) self.fields['favorite_subjects'].widget.attrs={ 'id': 'favorite_subjects_field', 'class': 'form-control', 'required': 'true', 'autocomplete':'off'} self.fields['favorite_subjects'].widget.attrs['placeholder'] = 'Favorite Subjects' self.fields['favorite_courses'].widget.attrs={ 'id': 'favorite_subjects_field', 'class': 'form-control', 'required': 'true', 'autocomplete':'off'} self.fields['favorite_courses'].widget.attrs['placeholder'] = 'Favorite Courses' self.fields['captcha'].widget.attrs['class'] = 'form-control … -
Single account for users to sign-in on my different websites
I am new to web development. I am developing different websites. I want a system in which users can sign-in on my different websites with a single account because i want to put some functionalities in there. But i have no idea how to achieve this. Please guide me how to do this. -
How to create multiple fields using one attribute in Django-filters
I'm using Django-filters to filter based on categories and price. Price is an attribute of my model, I want to create two filter fields using this same attribute, one for min price and for max price. How can I do that? Can anyone please show me the method to do that? Thanks in advance! My filters.py: class ItemFilter(django_filters.FilterSet): class Meta: model = Item fields = { 'category': ['exact'], 'price': ['lte'] } -
How do you model the following use case in Django?
Together with two friends I have been making a game similar to the drinking game piccolo where you have a list of challenges. Each challenges has variables that need to be filled in (e.g: Player X gives four sips to Player Y). Besides that, a challenge consists of rounds, with each round having an index and a description (e.g Round 0: X and Y drink 4 sips. Round 1: X and Y drink 5 sips now), with X and Y being the same names in both rounds. First we made a small console app that had the challenges hardcoded in them. The list of challenges would look like this: challenge_list = [ Challenge(["p(0)", "rand_phone_number()"],[[0, "{d[0]} moet een nummer bellen. Het volgende scherm bepaalt welk nummer"], [1, "Het nummer is {d[1]}"]]), Challenge(["p(0)", "rand(2,5)", "rand_char('a','z')", "rand(2,5)"], [[0, "{d[0]} noemt {d[1]} dieren die beginnen met de letter {d[2]} of drinkt {d[3]} slokken"]]), Challenge([], [[0, "Alle drankjes schuiven een plek naar links"]]), After requests from other friends we decided that it would be educational to migrate the project to Django, since we did not have much experience in web development and we want to learn something new. We came up with the following model … -
Django Filter on between two different ManytoMany fields
I want to model a Cake with multiple Icings. Each Icings has a flavor. An Icing can belong to multiple Cakes so this needs to be a many-to-many relationship. I achieved the requirement thourgh this model: class Cake(models.Model): layers = models.ManyToManyField(Icing, through='CakeLayer') class Icing(models.Model): flavour = models.CharField(max_length=200) class CakeLayer(models.Model): cake = models.ForeignKey(Cake, on_delete=models.CASCADE) icing = models.ForeignKey(Icing, on_delete=models.CASCADE) order = models.IntegerField() However, I want to query a cake which has a chocolate icing ontop of a vanila icing (i.e chocolate_icing.order > vanila_icing.order) I cannot achieve it through Django's ORM but its rather simple by SQL. SELECT * FROM "cake" INNER JOIN "cakelayer" i1 ON ("cake"."icing" = "reservation_triproute"."id") INNER JOIN "cakelayer" i2 ON ("reservation_triproute"."id" = "reservation_tripstop"."trip_route_id") WHERE (i1.order > i2.order AND i1.icing.flavor="chocolate" AND i2.icing.flavor="vanila" ) I would rather write python code than inject SQL. Any idea how to convert this SQL to Django ORM syntax ?