Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Wrapping django queryset update/delete in a transaction
I want to wrap all update/delete calls in a transaction, since there are some post_save signal handlers that need to be done atomically. For a specific model, I can override the save function like so: class ModelA: @transaction.atomic def save(...): super().save(...) how do I apply the same for all .update() querysets involving ModelA? -
Only messages from request.user showing, not from other users
so I'm trying to display messages between two users, however only the messages sent by request.user are currently showing up. So originally, I created an inbox view named messages that displayed current conversations the current user has with other users via the Conversation model which has a manyTOmany field and I created an InstantMessage model which has a foreign key to my conversation model. Anyways, I got the conversations to display, but I am having trouble getting the messages sent by other user's to display for my user. I am not sure if the problem is within my link that sends me to view the contents of the message or within the logic. views.py/message and messages #displays contents of conversation via messages def message(request, profile_id): messages = InstantMessage.objects.filter(Q(sender_id=request.user)).\ values('sender_id','receiver_id', 'message', 'date', ).\ order_by('date',) conversations = Conversation.objects.filter(members=request.user) context = {'messages' : messages, 'conversations':conversations} return render(request, 'dating_app/message.html', context) #displays current conversations def messages(request,profile_id): profile = get_object_or_404(Profile,id=profile_id) conversations = Conversation.objects.filter( members=request.user ).annotate( last_message=Max('instantmessage__date') ).prefetch_related('members').order_by( '-last_message' ) return render(request, 'dating_app/messages.html', {'messages': messages,'profile': profile, 'conversations':conversations,}) messages.html displays conversations and contains my link to content of message {% for conversation in conversations%} <li class="text-right list-group-item"> {% for member in conversation.members.all %}{% if member != user %} … -
bulk insert but ignore conflicts or missing related entity
I have an ingest worker that pulls some logs from datastore (5k+/- every minute) and inserts a slimmer version of them into a posgres instance. The values inside prosgres have about 5-6 relations to other objects. From datastore I get ids of related objects and I insert based on them in order to avoid thousands of queries to the database to verify the existance of each relation. At the end of it all I do a bulk create: sourceiter = iter(objects) while True: batch = list(islice(sourceiter, batch_size)) if not batch: break QueryLogs.objects.bulk_create(batch, batch_size, ignore_conflicts=True) I have ignore_conflicts in place in case of duplicates but what can I do in case of missing relations? Some of those relations may go missing, being deleted by other tasks, users or admins. Right now the whole block stops and throws an exception and I miss all logs instead of just a few. Any recommendations? -
Can't get the detail view of a post in a social network project
I am building a simple social network in django. In the "home" of my social, I have the list of all posts published by all users, with author and publishing date. The publishing date have a link to a url that should return the detail view a specific post published by a user. However, as I click on it, it shows me the list of all posts published by its author (this also works when I click on the author link of the post). So the problem is that both www.mysocial.com/posts/by/ciccio/7/ and www.mysocial.com/posts/by/ciccio/ takes me to the same page, that is ciccio's posts list. I am going to show urls.py, views.py and models.py that all are contained in my "posts" app (myproject > posts) Here is my urls.py # shows all user posts url(r'by/(?P<username>[-\w]+)', views.UserPosts.as_view(), name='for_user'), # shows specific post url(r'by/(?P<username>[-\w]+)/(?P<pk>\d+)/$', views.PostDetail.as_view(), name='single'), my views.py class PostList(SelectRelatedMixin, generic.ListView): model = Post select_related = ('user', 'group') class UserPosts(generic.ListView): model = models.Post template_name = 'posts/user_post_list.html' def get_queryset(self): try: #prende i post fatti dal'username che è lo stesso di quello che è loggato per guardarli self.post_user = User.objects.prefetch_related('posts').get(username__iexact=self.kwargs.get('username')) except User.DoesNotExist: raise Http404 else: return self.post_user.posts.all() def get_context_data(self,**kwargs): context = super().get_context_data(**kwargs) context['post_user'] = self.post_user … -
How to make instance feature in the function
I want to add course.tutor.add(self.request.user) to the function below but don't know how to do that, because i don't know how to do without slug and so on class FormWizardView(SessionWizardView): template_name = 'courses/create_course.html' file_storage = FileSystemStorage(location=os.path.join(settings.MEDIA_ROOT,'courses')) form_list = (CourseForm1,CourseForm2,CourseForm3,CourseForm4) def done(self, form_list, **kwargs): instance = Course() for form in form_list: for field, value in form.cleaned_data.items(): setattr(instance, field, value) instance.save() return redirect('courses:my_courses',username=self.request.user.username) -
How do I fix django html form sending action to link
Ok so i know the traditional way of handling forms in django but was experimenting something different this time. So I don't know what exactly is going wrong here but its just not working!!! Here is how the error looks error So what I'm tryna do is just a simple login and register system. There is one good thing and one bad thing which I partially get. Login system doesn't work on both chrome and safari, btw I only use chrome was just experimenting with safari Registration system works on safari but bad luck on chrome I have been tryna fix this from the past 3-4 hours, can any pro help me????? here's the code views.py from django.shortcuts import render from django.shortcuts import redirect from django.http import HttpResponse, HttpResponseRedirect from django.contrib.auth import authenticate from django.contrib.auth.models import auth from django.contrib import messages from django.urls import reverse from .forms import * from django.core.cache import cache def register(request): cache.clear() if request.method == 'POST': FirstName = request.POST['FirstName'] LastName = request.POST['LastName'] DP = request.POST.get('DP', False) Username = request.POST['Username'] Email = request.POST['Email'] password = request.POST['password'] if User.objects.filter(Username=Username).exists(): print('Username.taken') elif User.objects.filter(Email=Email).exists(): print('Your Email already exists') else: user = User.objects.create(password=password, Email=Email, Username=Username, DP=DP, LastName=LastName, FirstName=FirstName) user.save() print('Bangaya!!!') return … -
Python __init__.py so that package can be imported without 'from' keyword
Lets say my package looks like this: mydir/a/__init__.py mydir/a/b.py mydir/a/c.py This may be a dumb question, but I haven't worked with __init__.py before. I have a package which has a few .py files in the same directory as __init__.py. Everything involving __init__.py I have tried so far results in imports needing to look like: from a import b from a import c or import a.b import a.c or by using the __all__ variable in __init__.py: from a import * how can I setup the __init__.py so that I can import all packages within 'a' using: import a -
Problem with nested Dict in Python (KeyError)
I try to append data to a complicated Dictionary in Python. The Structure is given, i cannot change this. Example Code: class AutoVivification(dict): def __getitem__(self, item): try: return dict.__getitem__(self, item) except KeyError: value = self[item] = type(self)() return value class Command(BaseCommand): def vorgang(self, lead): payload = AutoVivification() payload['antragsteller1'] = { 'personendaten': { 'anrede': lead.salutation, 'vorname': lead.firstname, 'nachname': lead.lastname, 'email': lead.email, 'telefonPrivat': lead.phone, 'geburtsdatum': str(lead.birthdate) }, 'wohnsituation': { 'anschrift': { 'strasse': lead.address, 'plz': lead.zip, 'ort': lead.city } }, 'beschaeftigung': { 'beschaeftigungsart': lead.employment } } if lead.employment == 'ARBEITER': payload['antragsteller1']['beschaeftigung']['arbeiter']['beschaeftigungsverhaeltnis']['nettoeinkommenMonatlich'] = str(lead.income) elif lead.employment == 'ANGESTELLTER': payload['antragsteller1']['beschaeftigung']['angestellter']['beschaeftigungsverhaeltnis']['nettoeinkommenMonatlich'] = str(lead.income) elif lead.employment == 'ARBEITSLOSER': payload['antragsteller1']['beschaeftigung']['arbeitsloser']['sonstigesEinkommenMonatlich'] = str(lead.income) Error: KeyError: 'angestellter' Why it is not possible to append 'angestellter' at the second elif statement? And how would it be possible? -
Sort based on related objects field in Django
I have two models, Player and Match class Player(PlayerModel): points = models.PositiveIntegerField(default=0) games_played = models.PositiveIntegerField(default=0) wins = models.PositiveIntegerField(default=0) draws = models.PositiveIntegerField(default=0) defeats = models.PositiveIntegerField(default=0) goals_scored = models.PositiveIntegerField(default=0) goals_conceded = models.PositiveIntegerField(default=0) goal_difference = models.IntegerField(default=0) class Match(models.Model): player1 = models.ForeignKey(PlayerLeagueModel, on_delete=models.CASCADE, related_name='player1') player2 = models.ForeignKey(PlayerLeagueModel, on_delete=models.CASCADE, related_name='player2') score1 = models.PositiveIntegerField(null=True, blank=True) score2 = models.PositiveIntegerField(null=True, blank=True) winner = models.ForeignKey(Player, null=True, blank=True, on_delete=models.CASCADE,related_name='winner') I want to sort Player model objects based on their points, if points are equal then on goal_difference, and if goal_difference is equal then I must check to see who won the match between the players with equal points. def head_to_head(): table_list = [] match = Match.objects.all() for m in match: if m.player1 not in table_list: table_list.append(m.player1) if m.player2 not in table_list: table_list.append(m.player2) table_list.sort(key=lambda x: (x.points, x.goal_difference), reverse=True) But I can't figure out how to sort based on head to head matches. -
Django: convert all values of two clumn to absolute value
Lets say, I have a product model with two columns, price, sold. I want to convert all the values to absolute value for these two columns and save it. Currently I do this for obj in Product.objects.all(): obj.price = abs(obj.price) obj.sold = abs(obj.sold) Product.objects.bulk_update(all_obj, ['price', 'sold']) Although this works, but this takes too long for a table where we can have half a million records. Is there any better way to do this? -
Django login doesn't work with uwsgi/nginx
I'm not able to login to Django using uwsgi and nginx. I looked at this similar question, but I believe I have a different issue, since I'm using up-to-date versions of uwsgi (2.0.18) and nginx (1.10.3). Ubuntu is 16.04. There's no error message on the login screen. It works fine on development, and it (or something similar) worked fine in the past. The uwsgi log shows: [pid: 5692|app: 0|req: 4/4] 98.207.204.72 () {52 vars in 1105 bytes} [Wed Apr 15 11:34:57 2020] POST /userAuth/login/ => generated 0 bytes in 21 msecs (HTTP/1.1 302) 8 headers in 517 bytes (1 switches on core 0) [pid: 5692|app: 0|req: 5/5] 98.207.204.72 () {46 vars in 928 bytes} [Wed Apr 15 11:34:57 2020] GET /core/ => generated 0 bytes in 0 msecs (HTTP/1.1 302) 4 headers in 135 bytes (1 switches on core 0) [pid: 5692|app: 0|req: 6/6] 98.207.204.72 () {46 vars in 971 bytes} [Wed Apr 15 11:34:57 2020] GET /userAuth/login/?next=/core/ => generated 1047 bytes in 3 msecs (HTTP/1.1 200) 6 headers in 360 bytes (1 switches on core 0) The nginx log shows: 98.207.204.72 - - [15/Apr/2020:11:34:57 -0700] "POST /userAuth/login/ HTTP/1.1" 302 0 "http://demo.mysite.com/userAuth/login/?next=/core/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, … -
How to insert PDF table data into database
I have extracted pdf table data by using Camelot but now how can I do put my table data into my database like do I need to convert it into CSV? like is there any other way to put it into my database? and is there any other way to choose my specific tables or just put the number of the tables. cause in here I need to specify my table no. to be extracted. ''' def tables_extract(file_name): filename_with_path = 'upload/media/pos/pdfs/{}'.format(file_name) tables = camelot.read_pdf(filename_with_path, pages="1-end") table= tables[2].df ''' Below is the table data in the pdf which I want to put the values into my DB -
Receving a NoReverseMatch error in Django with Python
I am currently working on a tutorial in the "Python Crash course" Book. The tutorial is about creating a "Learning Log" Webapp with Django. The idea of the app is to allow users to: 1. create "Topics" they have learned about 2. add "Entries" to those Topics, describing details they have learned specific to those topics I am currently stuck at creating an Entry form and receive an Error, when I run http://127.0.0.1:8000/new_entry/1 NoReverseMatch at /new_entry/1 Reverse for 'topic' with arguments '('',)' not found. 1 pattern(s) tried: ['topics/(?P<topic_id>[0-9]+)/$'] Request Method: GET Request URL: http://127.0.0.1:8000/new_entry/1 Django Version: 3.0.5 Exception Type: NoReverseMatch Exception Value: Reverse for 'topic' with arguments '('',)' not found. 1 pattern(s) tried: ['topics/(?P<topic_id>[0-9]+)/$'] Exception Location: C:\Users\DR\Desktop\learning_log\ll_env\lib\site-packages\django\urls\resolvers.py in _reverse_with_prefix, line 677 Python Executable: C:\Users\DR\Desktop\learning_log\ll_env\Scripts\python.exe Python Version: 3.8.2 Python Path: ['C:\\Users\\DR\\Desktop\\learning_log', 'C:\\Program ' 'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.8_3.8.752.0_x64__qbz5n2kfra8p0\\python38.zip', 'C:\\Program ' 'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.8_3.8.752.0_x64__qbz5n2kfra8p0\\DLLs', 'C:\\Program ' 'Files\\WindowsApps\\PythonSoftwareFoundation.Python.3.8_3.8.752.0_x64__qbz5n2kfra8p0\\lib', 'C:\\Users\\DR\\AppData\\Local\\Microsoft\\WindowsApps\\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0', 'C:\\Users\\DR\\Desktop\\learning_log\\ll_env', 'C:\\Users\\DR\\Desktop\\learning_log\\ll_env\\lib\\site-packages'] Server time: Wed, 15 Apr 2020 19:46:06 +0000 The forms.py file: from django import forms from .models import Topic, Entry class TopicForm(forms.ModelForm): class Meta: model = Topic fields = ['text'] labels = {'text': ''} class EntryForm(forms.ModelForm): class Meta: model = Entry fields = ['text'] labels = {'text': ''} widgets = {'text': forms.Textarea(attrs={'cols': 80})} The urls.py file: from django.urls … -
What is the difference between Serializer and ModelSerializer
What is the difference between django rest framework's Serializer vs ModelSerializer? When do you one or the other? Can I use ModelSerializer for Auth? Thanks a lot for the help. -
What do I set as a default parameter when customizing models.py in Django?
I'm trying to ask the user for their city, state and zip when signing up. In my models.py I have: class Profile(models.Model): ... city = models.CharField(max_length=50) state = models.CharField(max_length=3) zip = models.CharField(max_length=5) I'm getting the following error when migrating: You are trying to add a non-nullable field 'city' to profile without a default; we can't do that (the database needs something t o populate existing rows). Please select a fix: 1) Provide a one-off default now (will be set on all existing rows with a null value for this column) 2) Quit, and let me add a default in models.py Select an option: What should I put as a method parameter? I tried default=None, but that gives an error too. -
Change Django modelForm Foreign Key field to Text Field
I have to generate a form for adding member to model which is Foreign Key to User model. It gives me dropdown field, how can i make it a charfield. -
redirect url with primary key DJANGO
Hei, So i have list with some recipes and i wish that user after clicking on recipe would be redirect to view with more details. With different words i wish that every recipe has his own url. I am trying this code as below, but it does not work, it redirect 404 side after i click on recipe. View: def GenerallView(request): lista1 = Recipe.objects.all() return render(request, 'drinks/GenerallView.html', {'lista1': lista1}) def DetailView(request, pk): lista = get_object_or_404(Recipe, pk=pk) return render(request, 'drinks/DetailView.html', {'lista': lista}) Url: path('generall_view', views.GenerallView, name='GenerallView'), path('detail_view/<int:pk>', views.DetailView, name='DetailView'), Templates: generall view <ul> {% for drink in lista1 %} <li><a href="{% url 'detail_view' Recipe.pk %">{{ drink.recipe_name }}</a></li> {% empty %} <li>No notes yet.</li> {% endfor %} </ul> detail view <h1>{{ recipe.name }}</h1> -
How to evaluate the object type an individual message in the django message framework
I am attempt to write a comprehensive message handling and display framework for Django. As a result I want to handle for messages that are of different object types. Some maybe a string others maybe a dictionary. e.g. consider this trivial example messages.error(request, form.errors.as_json()) messages.warning(request, "error 2") messages.info(request, 'error 3') an example of a form error could be. add_error('username',ValidationError('User does not exist', code=40)) now if you loop through the messages dictionary you get the following output to the console: {"username": [{"message": "User does not exist", "code": 40}]} error 2 error 3 Now we just need to loop through the messages dictionary and format the message for display. For this I plan to use a customer filter. The challenge is that when evaluating the object type of the message they are all coming back as strings. Even thought the first message is a dictionary object. print (type(error.message)) returns (for all the messages) <class 'str'> I suspected that the dictionary was being returned as a string. '{"username": [{"message": "User does not exist", "code": 40}]}'. Hence I tired using literal_eval. Following is the full draft of the template filter.: from django import template from ast import literal_eval register = template.Library() @register.filter() def format_err(error): … -
Not getting any data in POST request in views django admin
I'm trying to get a value from a select option in django view but not getting any value in post request: my HTML looks like <form method="post"> <select class="custom-select" title="region_list" value="{{ region }}"> <option selected>Open this select menu</option> {% for i in region_id %} <li><option value="{{ i.region }}">{{ forloop.counter }}. {{ i.region }}</option></li> {% endfor %} </select> <input type="submit" value="submit"> </form> and my view file looks like : class updateOrderType(View): template_view = 'admin/custom_orders_all.html' @csrf_exempt def dispatch(self, request, *args, **kwargs): return super().dispatch(request, *args, **kwargs) def get(self, request): region_id = list(UserRegionMapping.objects.filter(user=request.user.id).values_list('region_id', flat=True)) store_list=list(StoreRegionMapping.objects.filter(region__id__in=region_id).values_list('store_id__name', flat=True)) region_name = UserRegionMapping.objects.filter(user=request.user.id) # store_id=Order.objects.filter(id=ixd).values_list('store_id') return render(request, self.template_view, {'region_id': region_name, 'store_list':store_list}) @csrf_exempt def post(self, request): data=request.POST in this I'm not getting any data in "data=request.POST". -
delete and modify comments by users
i've added to my Blog post comments now the problem is that i'm trying to create remove function for that and i have one but now eve uset thak is logged in can remove all the comments how can i fix this that user can delete only their own comments?? the funnyiest thing is that i have this function on my posts and it wokres and if i'm trying to do the same on my comments then i've get 404 error. Ive tried a few different ways but nothing worked You are my only hope:) views.py from django.shortcuts import render, get_object_or_404, redirect from django.views.generic import ListView, DetailView, CreateView, UpdateView, DeleteView from django.contrib.auth.models import User from .models import Post, Comment from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin from .forms import CommentForm from django.contrib.auth.decorators import login_required # Create your views here. def home(request): context = { 'posts': Post.objects.all() } return render(request, 'blog/home.html', context) class PostListView(ListView): model = Post template_name = 'blog/home.html' context_object_name = 'posts' ordering = ['-date_posted'] paginate_by = 5 class UserPostListView(ListView): model = Post template_name = 'blog/user_posts.html' context_object_name = 'posts' ordering = ['-date_posted'] paginate_by = 5 def get_queryset(self): user = get_object_or_404(User, username=self.kwargs.get('username')) return Post.objects.filter(author=user).order_by('-date_posted') class PostDetailView(DetailView): model = Post class PostCreateView(LoginRequiredMixin, CreateView): model … -
Django ORM - How to order objects by sum of three highes scores?
Suppose my Django users participate in different contests, where they score points. Every User has around 100 different contests in which he participated. How can I query the users model in a view to retrieve the list of users, ordered by the sum of their three heights contest scores? As I want to pass on certain filters from the view to the contest (country, year), I am trying to avoid writing raw SQL. -
Spotipy on Django authorization without copy-paste to console
I have a Django site in which I want to use spotipy to look for statistics of the song like popularity and views. I have this code right now: import spotipy import spotipy.util as util #luxury import json import webbrowser username = 'dgrqnco2rx8hdu58kv9if9eho' scope = 'user-read-private user-read-playback-state user-modify-playback-state' token = util.prompt_for_user_token(username, scope, client_id='08bb526962574a46b359bffc56048147', client_secret='bf6d4184c8ae40aca207714e02153bad', redirect_uri='http://google.com/') sp_obj = spotipy.Spotify(auth=token) ss = 'name of song' if ss.__contains__('('): q = ss[0:ss.index('(')] elif ss.__contains__('['): q = ss[0:ss.index('[')] elif ss.__contains__('['): q = ss[0:ss.index('{')] else: q = ss query = sp_obj.search(q, 1, 0, 'track') #<<<<<<<<<<SONG>>>>>>>>>> #FIND THE SONG URI song_uri = query['tracks']['items'][0]['uri'] track = sp_obj.track(song_uri) track_data = sp_obj.audio_features(song_uri) song_popularity = track['popularity'] song_danceability = track_data[0]['danceability'] song_energy = track_data[0]['energy'] song_loudness = track_data[0]['loudness'] song_tempo = track_data[0]['tempo'] However spotipy redirects me to a page for authorization and I need to paste the url in the console. The regular user however does not have access to this console. So how can I do the authorization in an alternative way or even bypass it? I was thinking about getting a spotify account in which every user will be getting logged in so that the user won't have to do the authorization and won't have to have a spotify account. Is this possible? … -
Unloading Data from Redshift to Amazon S3 | Django ORM
I'm looking for the best approach to work with Amazon S3 and Amazon Redshift. I need to unload the data from amazon Redshift and store it into S3. According to the AWS documentation, is truly simply to save the data to S3 https://docs.aws.amazon.com/redshift/latest/dg/t_Unloading_tables.html But I want to know if there's any way to do this using Django ORM, since also has support to Psycopg2. Thanks regards. -
Python 3 Django Rest Framework - Manager object has no attribute error
My custom EnrollmentManager says object has no attribute "end_date" - except it does!? class EnrollmentManager(models.Manager): def org_students_enrolled(self, organisation): return self.filter(student__organisation__name=organisation).filter(self.end_date.date() >= datetime.date.today()) class Enrollment(TimeStampedModel): objects = EnrollmentManager() course = models.ForeignKey(to=Course, on_delete=models.CASCADE, default=None, null=False) student = models.ForeignKey(to=Student, on_delete=models.CASCADE, default=None, null=False) enrolled = models.DateTimeField() last_booking = models.DateTimeField() credits_total = models.SmallIntegerField(default=10) credits_balance = models.DecimalField(max_digits=5, decimal_places=2) @property def end_date(self): return self.enrolled + datetime.timedelta(days=182) -
Nginx Configuration in Docker
I am trying to configure nginx inside docker container. However I can't reach my server ip without defining port number (8000). It seems nginx can't listen on 80 port or something. I almost tried every solution on internet but it still same. Here is my docker-compose.yml: version: '3' services: app: build: context: . ports: - "8000:8000" env_file: - django.env volumes: - ./app:/app - /static:/static command: > sh -c "python3 manage.py migrate && python3 manage.py wait_for_db && gunicorn app.wsgi -b 0.0.0.0:8000" environment: - DB_HOST=db - DB_NAME=app - DB_USER=postgres - DB_PASS=supersecretpassword depends_on: - db db: image: postgres:10-alpine environment: - POSTGRES_DB=app - POSTGRES_USER=postgres - POSTGRES_PASSWORD=supersecretpassword ports: - "5432:5432" volumes: - pgdata:/var/lib/postgresql/data - pgconf:/etc/postgresql - pglog:/var/log/postgresql nginx: build: ./nginx ports: - "10080:80" - "10443:443" volumes: - ./app:/app - ./config/nginx:/etc/nginx/conf.d - /static:/static expose: - "8000" depends_on: - app volumes: pgdata: driver: local pgconf: driver: local pglog: driver: local and nginx configuration: upstream app { ip_hash; server my_server_ip:8000; } server { location /static { autoindex on; alias /static/ } location / { proxy_pass http://app; } listen 80; server_name my_server_ip; } Thank you for your help.