Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django Filters Library
Hi everybody I'm trying to use django_filters library to filter through a list of items. Anyway when the user enter the word he wants to filter by, it doesn't show anything. What am I doing wrong? Hope someone could help! Thanks a lot this is the filters.py import django_filters from .models import Info from django_filters import CharFilter class InfoFilter(django_filters.FilterSet): disco = CharFilter(field_name='disco', lookup_expr='icontains') class Meta: model = Info fields = ['band', 'disco'] this is the view.py: class searchesView(TemplateView): template_name = "search/searches.html" def post(self, request, *args, **kwargs): print('FORM POSTED WITH {}'.format(request.POST['srh'])) srch = request.POST.get('srh') if srch: sp = Info.objects.filter(Q(band__icontains=srch) | Q(disco__icontains=srch)) paginator = Paginator(sp, 10) page_number = request.GET.get('page') page_obj = paginator.get_page(page_number) myFilter = InfoFilter(request.GET, queryset=sp) sp = myFilter.qs return render(self.request, 'search/searches.html', {'sp':sp, 'page_obj': page_obj, 'myFilter':myFilter }) else: return render(self.request, 'search/searches.html') and last this is the template: {% if sp %} <form method="get"> {{myFilter.form}} <input type="submit" value="FILTER"/> </form> {% for k in sp %} <div class="container_band" id="display"> <div class=album_band> <!-- insert an image --> {%if k.cover%} <img src= "{{k.cover.url}}" width="100%"> {%else%} <img src="{{no}}" width="100%"> {%endif%} </div> <div class="info_band"> <!-- insert table info --> <table> <tr><th colspan=2><h3>{{k.band}}</h3></th></tr> <tr><td> Disco: </td><td> {{k.disco}} </td></tr> <tr><td> Anno: </td><td> {{k.anno}} </td></tr> <tr><td> Etichetta: </td><td> {{k.etichetta_d}} </td></tr> <tr><td> … -
Django admin set value of read only field from external service
I have a model whereby an id to store is generated by an external service, a 'job_id'. On creation, a post request is sent to this external service, and the job_id is sent back. The model is created only if this job_id is received. Within the django admin, this job_id is set to read only, as this will be created by an external service. I have placed the request to the external service in the clean function: def clean(self): if not self.cleaned_data.get('job_id'): response = requests.post(external_service, params=params, data=data) if response.status_code != 200: raise ValidationError(_("external_service unavailable. Please try again")) self.cleaned_data.update({'job_id': self.__extract_job(response)}) However I cannot seem to add the job_id to the cleaned data, I keep getting: NOT NULL constraint failed: external_service.job_id I have also tried self.cleaned_data.setdefault('job_id', self.__extract_job(response)) but I cannot seem to attach the job_id data to the field. -
Cannot filter out logs from the console
I am trying to filter out certain endpoint logs from the console. My settings.py looks like this: def skip_rss_requests(record): if record.args and record.args[0].startswith('GET /api/feed/rss/'): print("HEEERRRRREEEE") return False return True LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'skip_rss_requests': { '()': 'django.utils.log.CallbackFilter', 'callback': skip_rss_requests } }, 'formatters': { 'simple': { 'format': '[%(asctime)s] %(levelname)s|%(name)s|%(message)s', 'datefmt': '%Y-%m-%d %H:%M:%S', }, }, 'handlers': { 'console': { 'level': 'INFO', 'class': 'logging.StreamHandler', 'filters': ['skip_rss_requests'], # 'stream': sys.stdout, 'formatter': 'simple' }, However, I am still seeing these logs in my console output despite the condition being met: [2020-04-28 13:31:11] INFO|django.request|GET /api/feed/rss/ [2020-04-28 13:31:13] INFO|django.request|GET /api/feed/rss/ - 200 HEEERRRRREEEE Any ideas as to why these records are still being logged? -
Django deployment to Postgres with AWS
I am having an issue with making migrations to my newly configurated database on amazon server. When running python manage.py makemigrations I get the error: conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: FATAL: password authentication failed for user "ubuntu" FATAL: password authentication failed for user "ubuntu" I am stucked and being frankly a beginner and I am confused because although I run a virtual ubuntu machine to host my django app, I don't have a "ubuntu" user. Here is my .env file: SECRET_KEY = secretkey.. DEBUG = False NAME = dbexostock USER = pierre PASSWORD = mypassword HOST = hostnameprovidedbyamazonrds settings.py 'default': { 'ENGINE': 'django_tenants.postgresql_backend', 'NAME': config('NAME'), 'USER': config('USER'), 'PASSWORD' : config('PASSWORD'), 'HOST': config('HOST'), 'PORT': '5432', } } and one of my form needs to write in the database such as: engine = create_engine('postgresql://pierre:mypassword@:hostnameprovidedbyamazonrds:5432/dbexostock', connect_args={'options': '-csearch_path={}'.format(dbschema)}) metricsdb = metrics.to_sql('dashboard_metrics', engine, if_exists='replace') I am out of things to verify, I would appreciate any help on the issue -
Creating a new related object from a predefined parent object in a one to many relation in django
Hi I am making a car dealership project in django and I would like to be able to have a functionality of creating a review on a car just like how one can create a comment on a post in a blog app or social site. In my case the parent object is the car and the related object the review. The ideal outcome would be for one to click on review on the car and the web app to know which car i am reviewing. This is what I had tried so far car Models.py from django.db import models from django.urls import reverse from categories.models import Category # Create your models here. class Car(models.Model): image = models.ImageField(upload_to='images/cars/') make = models.CharField(null=False, blank=False, max_length=30) model = models.CharField(null=False, blank=False, max_length=30) year = models.IntegerField(null=False, blank=False) transmission = models.CharField(null=False, blank=False, max_length=30) category = models.ForeignKey(Category, on_delete=models.CASCADE) def __str__(self): return self.model def get_absolute_url(self): return reverse('car_detail', args=[str(self.id)]) car review models.py from django.db import models from django.urls import reverse from users.models import CustomUser from cars.models import Car # Create your models here. class Review(models.Model): title = models.CharField(max_length=20) description = models.TextField(max_length=160) date_added = models.DateTimeField(auto_now_add=True) user = models.ForeignKey(CustomUser, on_delete=models.CASCADE) car = models.ForeignKey(Car, on_delete=models.CASCADE) def __str__(self): return self.title def get_absolute_url(self): return … -
Unsupported lookup 'lower' for CharField or join on the field not permitted
I'm attempting to perform a slightly more advanced lookup on a queryset, as outlined here: https://docs.djangoproject.com/en/3.0/topics/db/search/ My queryset is as follows: queryset = queryset.filter( Q(user__username__unaccent__lower__trigram_similar=search) | Q(user__first_name__unaccent__lower__trigram_similar=search) | Q(user__last_name__unaccent__lower__trigram_similar=search) ) I've included the django.contrib.postgres app in INSTALLED_APPS in my settings.py. However, I receive the following error: django.core.exceptions.FieldError: Unsupported lookup 'lower' for CharField or join on the field not permitted. Does anyone know the correct procedure for search a search? I am using Django >3 and postgres engine version 11. -
Python timedelta to Postgres interval
I have a Django DurationField: timerep = models.DurationField(null=True, blank=True) The value for it comes from input <input type="number" name="timereph" value="{{ timereph|floatformat }}" min="0" max="99.9" step="0.1" class="numberinput form-control" id="timereph"> and gets recorder def form_valid(self, form): check_closed = self.request.POST.get('check_closed', None) timereph = self.request.POST.get('timereph', None) self.object = form.save(commit=False) if check_closed is not None: if timereph == '' or timereph is None: self.object.timerep = timezone.now() - self.object.date_added if timereph != '' and timereph is not None: self.object.timerep = timedelta(hours=float(timereph)) self.object.save() Everything worked fine on SQLite. But when i switched to Postgres i started getting an error (doesn't matter which if condition passes: ProgrammingError at /mwo/order/1/edit column "timerep" is of type double precision but expression is of type interval LINE 1: ..."timerep" = '0 days 32... ^ HINT: You will need to rewrite or cast the expression. It seems that in Postgres DurationField is stored as interval. Is there a function i could use to convert Python timedelta to this interval type. -
Django - how to 'remember' some SQL queries
There's this line in the Django documentation: "When running with DEBUG turned on, Django will remember every SQL query it executes. This is useful when you’re debugging, but it’ll rapidly consume memory on a production server." So if I want to remember some queries when DEBUG=False on my server, how do I do it? Use Redis/memcached I guess? But they are key-value stores, so I will need to go over several files and change the code, right? What if I just want to cache a SQL query that's repeated often. The documentation around caching is mostly about template caching or storing key-value pairs. I couldn't find anything about caching full, often used SQL query. -
How django REST Framework works regarding cache and SQL requests
Working currently with DRF for my project, I decided to use a cache system (REDIS in that case) in order to limit the number of SQL requests directly to the DB. So I saw/read many pages on that topic, but I didn't find out my answer. on the DRF web site, they talk about to use the cache_page with the help of the decorator @method_decorator. Ok it works correctly but in the HTTP header, an expiration_date field appears, indicating to the browser to put in its cache the whole page and do not request anything until the end of the expiration date. So if the expiration is around current time + 5 minutes, during 5 minutes we "live" with the web browser cache, and if something change during that 5 minutes, we could not know it. Are you agree with that behavior of that decorator? Since I would like to be informed about any changes during that time, I update my code to cache all needed SQL requests. Like this if something change, I flush the cache and the next time, I will get the up-to-date datas. Here is an light example of a ModelViewSet: queryset = MyModel.objects.all().select_related(...).prefeth_related(...) cache.set('MyKey', queryset) … -
An unexpected effect appears when using django's Q
In [25]: Project.objects.filter(Q(change__date__date__range=('2020-04-28','2020-04-28'))&~Q(change__se__isnull=True)).count() Out[25]: 8 In [26]: Project.objects.filter(Q(change__date__date__range=('2020-04-28','2020-04-28'))&Q(change__se__isnull=False)).count() Out[26]: 9 enter image description here I don't know what the difference is -
GET Response with several models
I have two ViewSets. They have unique serializer and model. For example, CitiesViewSet and TypesViewSet: CitiesViewSet - [{"name":"Moscow"},{"name": "Kazan"} etc...] TypesViewSet - [{"id": 1, "name": "sample"}, {"id": 2, "name": "sample"} etc... ] I want to combine this ViewSets to one GET Response. For example, I can do GET request and I will get looks like - { "cities": [ {"name": "Moscow"}, {"name": "Kazan"} etc... ], "types": [ {"id": 1, "name": "sample"}, {"id": 2, "name": "sample"} etc. ] } How to create it? -
Django Model Foreign Key to text
There is a problem, there is a Book model and in it title is the name of the book, and author is the ForeignKey of the Author model where there is a name field. I need to get a Python list and for this I do books = list(Book.objects.values('title', 'author'), but the result is a list [{'title': 'Harry Potter', 'author': 1}]. How do I make sure that the author is instead of the unit? -
Display a checkbox choice as "disabled" in django 3.0. ModelForm
I've been looking around for similar issues and I've yet to come across a solution to my specific problem. What I aim to do is display a checkbox like this. Where only the signed in (authenticated) user has the ability to select/deselect the checkbox corresponding to his profile. In this case, mindspace is authenticated I reached this point by trying this solution: But as one of the comments suggest the create_option method doesn't get called through the CheckboxSelectMultiple class. As a result I messed a bit with the SelectMultiple class, like-so. class CheckboxCustom(forms.SelectMultiple): def __init__(self, *args, **kwargs): self.input_type = 'checkbox' self.checked_attribute = {'checked': True} self.template_name = 'django/forms/widgets/checkbox_select.html' self.option_template_name = 'django/forms/widgets/checkbox_option.html' self.allow_multiple_selected = True self.request = kwargs.pop("request") self.option_inherits_attrs = False # print(options super(CheckboxCustom, self).__init__(*args, **kwargs) def create_option(self, *args, **kwargs): options_dict = super().create_option(*args, **kwargs) selected = options_dict['selected'] print(selected) option_attrs = self.build_attrs(self.attrs, attrs) if self.option_inherits_attrs else {} # if selected: # options_dict['attrs']['multiple'] = True if options_dict['label'] != str(self.request.user.profile): options_dict['attrs']['disabled'] = True # print(val) return options_dict And used this as a widget. However, no matter if I select or deselect one of the two checkboxes (from the corresponding profile), the other will get deselected both in the template and the database. Same thing happens … -
Django one to many get all underlying records
I'am creating a django application with a database including the following tables: I created the models as following: class Group(models.Model): id = models.AutoField(primary_key=True, editable=False) text = models.CharField(db_column='Text', max_length=4000) class Meta: db_table = 'Group' class Filters(models.Model): id = models.AutoField(primary_key=True, editable=False) text = models.CharField(db_column='Text', max_length=4000) group_id = models.ForeignKey(Group, on_delete=models.CASCADE, db_column='GroupId') class Meta: db_table = 'Filters' The goal is to call an endpoint and return a list with all Groups and underlying Filters. Is it possible to achieve this? If possible, I want it in a Serializer class. I know I can get the group of a filter record, but is this also possible the otherway around? -
FileField is valid even when empty
I have a parent and child model: class Parent(models.Model): name = models.SlugField(max_length=45, null=False) tags = TaggableManager() class Child(models.Model): version = models.CharField(null=False, max_length=10, default='1.0') upload = models.FileField( upload_to=hack_upload_path, blank=False, null=False ) parent = models.ForeignKey( Parent, on_delete=models.CASCADE, related_name='versions' ) class Meta: get_latest_by = "upload_date" verbose_name = "version" verbose_name_plural = "versions" def __str__(self): return f'v{self.version}' A parent basically has multiple children which are just file uploads with some more data. So I have a form where I want to create a Parent and one Child at the same time, so I made this form and view: class ParentForm(ModelForm): class Meta: model = Parent fields = [ 'name', 'tags' ] ChildFormset = inlineformset_factory( Parent, Child, fields=( 'version', 'upload' ), extra=1, max_num=1, can_delete=False, ) class ParentCreateView(LoginRequiredMixin, CreateView): form_class = ParentForm template_name = 'new.html' def get_context_data(self, **kwargs): data = super().get_context_data(**kwargs) if self.request.POST: data['versions'] = ChildFormset( self.request.POST, self.request.FILES, instance=self.object ) else: data['versions'] = ChildFormset(instance=self.object) return data def form_valid(self, form): form.instance.seller = self.request.user context = self.get_context_data() versions = context['versions'] if versions.is_valid(): # It comes here even though the upload field is empty! self.object = form.save() versions.instance = self.object versions.save() return super().form_valid(form) else: return self.render_to_response( self.get_context_data( form=form, versions=versions ) ) And It passes the if versions.is_valid(): check even though … -
How to add for loop iteration django templates
I have a template page and I have used models required in views.py file. The template page consists of cricket statistics and it consists of tb, tr tags along with div tags. My question is where should I use the for loop template tags to replace the values as per the model data. {% extends 'base.html' %} <!DOCTYPE html> <html> {% block content %} <head> <meta charset="utf-8"> {% load staticfiles %} <link rel="stylesheet" href="{% static 'css/details.css' %}"> <style> td,tr { height: 10px; width: 10px; } th, td { padding: 10px; } </style> </head> <body> <div class="page"> <div id="page-wrapper" class="container" style="display:inline-block;"> <div id="shosh" class="ad-unit shosh-embed" style="height:0; width:980px; text-align:center;"></div> <div style="margin-top:20px;"></div> <div> <div class="cb-col cb-col-100 cb-bg-white"> <div class="cb-col cb-col-20 cb-col-rt"><img height="152" width="152" title="profile image" src="{{PlayerStructure_details.imageUri.url}}"></div> <div class="cb-col cb-col-80 cb-player-name-wrap"> <h1 itemprop="name" class="cb-font-40">{{PlayerStructure_details.firstname}} {{PlayerStructure_details.lastname}}</h1> <h3 class="cb-font-18 text-gray">{{PlayerStructure_details.country}}</h3></div> </div> <div class="cb-col cb-col-100 cb-bg-grey"> <div class="cb-col cb-col-33 text-black"> <div class="cb-hm-rght"> <div class="cb-font-16 text-bold"> Personal Information</div> <div class="cb-col cb-col-40 text-bold cb-lst-itm-sm">Born</div> <div class="cb-col cb-col-60 cb-lst-itm-sm"> {{PlayerStructure_details.BirthDate}} </div> <div class="cb-col cb-col-40 text-bold cb-lst-itm-sm">Birth Place</div> <div class="cb-col cb-col-60 cb-lst-itm-sm"> {{PlayerStructure_details.BirthPlace}} </div> <div class="cb-col cb-col-40 text-bold">Jersey No</div> <div class="cb-col cb-col-60"> {{PlayerStructure_details.JerseyNumber}} </div> <div class="cb-col cb-col-40 text-bold cb-lst-itm-sm">Role</div> <div class="cb-col cb-col-60 cb-lst-itm-sm"> {{PlayerStructure_details.Role}} </div> <div class="cb-col cb-col-40 text-bold cb-lst-itm-sm">Batting Style</div> … -
Django - Retrieve unique rows by latest date/column
I have the following table: class AccountData(models.Model): id = models.BigIntegerField(primary_key=True) date = models.DateField() last_update = models.DateTimeField() account = models.ForeignKey('Account', models.DO_NOTHING, db_column='account', related_name="values") value = models.DecimalField(max_digits=65535, decimal_places=65535, blank=True, null=True) Given a queryset/list of ID's (unknown amount how many ID's, could be None/0) Is there a way to get the latest row for each account, then sum the value? The part I'm really struggling with is only retrieving the latest row per account. I know I can get the accounts that are in the given queryset by doing: accounts = [1, 4, 65] AccountData.objects.filter(account__in=accounts) Just need to figure out how I can only get the rows where "date" is as close to "now" as possible. Thus resulting in having unique rows (only 1 row per account) -
Must i install django every time am starting a project in pycharm
i am just starting a new project on my pycharm i start a new project and run my server but am getting this error message couldn't import django, Must i install django for every project am creating? -
Please help me how can i add some option like inserting images, bold italic inserting link in body
it is a screenshot I listen about markdown but I don't know how to use please suggest some good alternative or tell me about markdown Please check it and tell me a fix. -
Django set logged in user inlineformset_factory
hello Everyone I'm trying to build an application where I can add multiple forms but I am facing a problem when trying to add current logged in user Exception Value:'list' object is not callable @login_required(login_url='login') def addIncome(request, month_id): monthname = Month.objects.get(pk=month_id) monthnameid = Month.objects.get(id=month_id) monthes = Month.objects.all() newForm = inlineformset_factory(Month, IncomeAmount, fields=('income_Name','incomeAmount','incomeCurrency'),can_delete=False, extra=5) if request.method == 'POST': form = newForm(request.POST, instance=monthname) if form.is_valid(): newForm = form.save(commit=False) print(type(newForm)) newForm.user = request.user newForm.save() return redirect(home) else: monthes = Month.objects.all() monthname = Month.objects.get(id=month_id) month = newForm(instance=monthname) context = {'form': newForm, 'month': monthname, 'monthes':monthes} return render(request, 'add.html', context) -
How to count foreign keys in django
I have a project with courses where every Course has Sections, and every Section has Videos, I want to count number of sections in each course, and count number of videos in each Section. How to implement that? views def CourseView(request,slug): course = get_object_or_404(Course,slug=slug) sections = CourseSections.objects.filter(course__title=course.title) videos = SectionVideos.objects.filter(section__course__title=course.title) return render(request,'courses/course_detail.html',{'course':course,'sections':sections,'videos':videos}) models class Course(models.Model): title = models.CharField(max_length=255) class CourseSections(models.Model): title = models.CharField(max_length=50) course = models.ForeignKey(Course,on_delete=models.CASCADE,null=True) class SectionVideos(models.Model): title = models.CharField(max_length=50,null=True) video = models.FileField(upload_to='courses/course_videos',max_length=100) section = models.ForeignKey(CourseSections,on_delete=models.CASCADE,null=True) -
Redirecting To a New Angular Application After Login and Subscription Success
Good Day Everyone, Please I am working on a personal project on Angular, I have two angular applications, the main angular application and the second angular application to redirect to after login and subscription has been validated. The Only issue now is how to make use of the home.component.html of the second application which contains different menu or navbar items but each time i redirect to the second application, the items or menu showing on the navbar are the main or default application component.html but with the second angular application contents. I also discovered that second application was not generated with any index.html file. Please I need help on how to go about this. In summary two angular applications one acting as default (thriller page), the second to display the real contents for movies. How can i implement the default application having its own menu and the second application having its own menu as well. Thanks -
Repeated query in Django View
Good Morning, I have repeated Views in Django app, I am pretty sure there is a better solution to this situation. class BaseMixin: model = Post paginate_by = 9 class PostPostgresList(BaseMixin, ListView): template_name = "blog/postgres_field.html" def get_queryset(self): return Post.published.filter(field=Category.objects.get(name='PostgreSQL')) class PostDjangoList(BaseMixin, ListView): template_name = "blog/django_field.html" def get_queryset(self): return Post.published.filter(field=Category.objects.get(name='Django')) Field in model post is ForeignKey, so I need to put in query instance of the Category. Perhaps I should create some function where I could insert for instance name='Django' and put this function in utils? Thanks in advance for any advise. -
Django Password Validators: If any three of the validations pass user can go ahead
I am looking at how can we set the number of validations that must pass to allow the user to set the password. For example, we have these 5 validtors: AUTH_PASSWORD_VALIDATORS = [ { 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', }, { 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', }, { 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', }, { 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', }, { 'NAME': 'registration.validators.CustomPasswordValidator',} ] We want if the user password passes any of the 3 requirements from them. Users can create an account. Any idea how can we do that in Django? -
how to migrate from postgres to phpmyadmin mysql django
our university has MySQL 5.6 server with phpMyAdmin, and my django project uses postgresql. and we cannot deploy project. any help?