Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How do I display the result of the beautifulsoup4 parser in django templates?
I'm working on a web application that collects jobs for programmers. It uses django 2.2 and beautifulsoup4. I try to display the results of parsing on the screen after clicking the button in the form redirects to the page of the result of parsing (None). There may be an error in parser or views.py, I can't figure it out. Logic: 1. Django displays the form on the main page 2. The user presses the button in the form 3. Parser collects data 4. Parsing result is displayed on the screen workua.py - scraper import requests from bs4 import BeautifulSoup def clean_description(s): return s.split('\n')[1] def get_html(url): r = requests.get(url) return r.text def get_data(html): bs = BeautifulSoup(html, 'lxml') job_list = bs.find('div', id='pjax-job-list').find_all('div', class_='card card-hover card-visited wordwrap job-link') for item in job_list: title = item.find('h2', class_='add-bottom-sm').text company = item.find('b').text d = item.find('p', class_='overflow').text descr = clean_description(d) url = 'https://www.work.ua' + item.find('h2', class_='add-bottom-sm').find('a').get('href') data = {'title':title, 'company':company, 'descr':descr, 'url':url} # print(data) def main(): pattern = 'https://www.work.ua/ru/jobs-kyiv-python/?page={}' for i in range(0, 3): url = pattern.format(str(i)) get_data(get_html(url)) views.py from django.shortcuts import render from .workua import * from .forms import PageForm def index_page(request): form = PageForm(request.GET) return render(request, 'page/index_page_form.html', context={'form':form}) def workua_result(request): result = main() return render(request, … -
Improving efficiency of QuerySet filter by most recent record of a certain "type" (attribute)
I'll try and keep this as condensed as possible. I have the following query: organisation_survey_results = OrganisationSurveyResult.objects.filter( user=user ).order_by('survey', 'created_date') In the above, I filter according to the user match. All is good. I have returned the 5 records for that user. Now, as mentioned, each record comes with the following attributes and attribute "(chains?)": Unique slug attribute: organisation_survey_result.organisation_survey.survey.slug Immutable (non-changing, write once on creation) created_date attribute: organisation_survey_result.created_date For the five records, if I loop over them, I have: django_1 | food django_1 | 2019-08-12 15:45:49.384071+00:00 django_1 | drink django_1 | 2019-08-12 15:45:49.390939+00:00 django_1 | politics django_1 | 2019-08-12 15:45:49.397714+00:00 django_1 | money django_1 | 2019-08-12 15:45:49.406612+00:00 django_1 | food django_1 | 2019-08-13 11:26:55.831903+00:00 As you can see, I have two records where the attribute organisation_survey.survey.slug with food appears twice. For a given user, this is fine. Records can, and will, supersede each over other time. My question: Is there a way whereby I can filter these records out on the Query? (for performance efficiencies)... I'd like to be able to perform this on the QuerySet level to perform a less stressful serialization of the data. Versions: `Django==2.2.1` `djangorestframework==3.9.3` Database Engine: `PostgreSQL` -
Django Graphene Relay order_by (OrderingFilter)
I have a Graphene interface with Relay and filters. It works pretty well but I would like to add the order_by options. My objects look like: class FooGQLType(DjangoObjectType): class Meta: model = Foo exclude_fields = ('internal_id',) interfaces = (graphene.relay.Node,) filter_fields = { "id": ["exact"], "code": ["exact", "icontains"], } connection_class = ExtendedConnection class Query(graphene.ObjectType): foo = DjangoFilterConnectionField(FooGQLType) ExtendedConnection should not be relevant but: class ExtendedConnection(graphene.Connection): class Meta: abstract = True total_count = graphene.Int() def resolve_total_count(root, info, **kwargs): return root.length This allows me to query like foo(code_Icontains:"bar"). According to the Graphene documentation I should be using the OrderingFilter in a FilterSet for that. I find it a bit annoying since the filters are supposed to be automatic but if I do: class FooGQLFilter(FilterSet): class Meta: model = Foo order_by = OrderingFilter( fields=( ('code', 'code'), ('lastName', 'last_name'), ('otherNames', 'other_names'), ) ) I get an error that I need to provide fields or exclude: AssertionError: Setting 'Meta.model' without either 'Meta.fields' or 'Meta.exclude' has been deprecated since 0.15.0 and is now disallowed. Add an explicit 'Meta.fields' or 'Meta.exclude' to the FooGQLFilter class. So if I add a fields = [] to silence it, it compiles. However, when I use it in: foo = DjangoFilterConnectionField(FooGQLType, filterset_class=FooGQLFilter) … -
Django: How can I inject a jinja2 variable into a template tag to then return the output
I'm trying to pass a jinja2 variable to a function as a parameter but I am unsure of the syntax or method to do so. I've tried including the reference in the jinja2 function call but this has not worked. I've tested the function and it works with a simple string "test" and the value is render in the page. HTML SECTION ... <tbody> {% for test in test_records %} <tr> <td class="trGrey"> {{ test.id }} </td> <td class="trGrey"> {{ test.testname }} </td> <td class="trGrey"> {{ test.created }} </td> <td class="trGrey"> {{ test.recordtype }} </td> <td class="trGrey"> {{ {{ test.recordtype }}|my_function }} </td> </tr> {% endfor %} </tbody> ... PYTHON FILE from django import template register = template.Library() def my_function(value): if value: return value return '' register.filter('my_function', my_function) I'd expect the input variable to be rendered to the page. Any suggestions will be helpful thanks! -
In AbstractUser does not work ordering in meta
I try when sort with id, first_name, ... but does not work in Meta class into AbstarctUser. ordering = ['id'] does not work but other models working this property. In this code I have set comment for ordering. In under file in User class and nested class Meta, ordering property not working. models.py from django.db import models from django.utils.translation import ugettext_lazy as _ from phonenumber_field.modelfields import PhoneNumberField from django.contrib.auth import get_user_model from django.contrib.auth.models import ( BaseUserManager, AbstractUser ) class UserManager(BaseUserManager): use_in_migrations = True def _create_user(self, password, **extra_fields): """ Create and save a user with the given phone number, and password. """ # if not username: # raise ValueError('The given username must be set') # email = self.normalize_email(email) # username = self.model.normalize_username(username) user = self.model(**extra_fields) user.set_password(password) user.save(using=self._db) return user def create_user(self, password=None, **extra_fields): extra_fields.setdefault('is_active', False) extra_fields.setdefault('is_staff', False) extra_fields.setdefault('is_superuser', False) return self._create_user(password, **extra_fields) def create_superuser(self, password, **extra_fields): extra_fields.setdefault('is_staff', True) extra_fields.setdefault('is_superuser', True) if extra_fields.get('is_staff') is not True: raise ValueError('Superuser must have is_staff=True.') if extra_fields.get('is_superuser') is not True: raise ValueError('Superuser must have is_superuser=True.') return self._create_user(password, **extra_fields) class User(AbstractUser): email = models.EmailField( verbose_name=_('email address'), max_length=125, null=True, blank=True, ) username = None phone_number = PhoneNumberField(unique=True, verbose_name=_('phone number')) token_sms = models.CharField(verbose_name=_('token_sms'), max_length=125) token_limit = models.IntegerField(verbose_name=_("token limit"), default=0) … -
How to release code for automatic update in django
I have a question. How can I implement the function and where exactly in django so that it automatically checks the user if the end of the premium account approaches. If the date of today coincides with the premium date, then let it turn off the premium account. -
How can I use two consecutive pks in Django URL?
I need to define path for two consecutive primary keys. I have some old version of code with url. Here it is: url(r'^([0-9]+)/(?P<pk>[0-9]+)$' Can someone translate this to path code? -
How to count the number of keys in a django jsonfield
I would like to make a queryset filter to identify all model instance that have a given number of keys in a jsonfield of the model. I have tried to create a custom lookup (#1) to extra the keys in the json field, and would like to aggregate those into an array and use the __len lookup of the array to make my filter. Unfortunately, I am stuck at the aggregation that doesn't seem to work (#2). 1 class JsonKeys(Transform): output_field = TextField() lookup_name = 'keys' function = 'jsonb_object_keys' 2 qs = qs.annotate(keysArray=ArrayAgg("myJsonField__keys")) The error that I get is: HINT: You might be able to move the set-returning function into a LATERAL FROM item. Anyone has already tried to perform this task ? -
How to make groups of all the Customers, Admins and Staff Members and render accordingly in same template based on URL hit?
I am trying to show all the Customers, Superusers and Staff members in a same template. How can I do this? I have a running code like this class GroupListView(UserPassesTestMixin, ListView): model = User template_name = 'admin_app/group_list.html' def get_context_data(self, *, object_list=None, **kwargs): context = super().get_context_data(**kwargs) group_name = (self.kwargs['group']) if group_name == 'admin': context['user_list']= User.objects.filter(is_superuser=True, is_staff=True) elif group_name == 'staff': context['user_list'] = User.objects.filter(is_staff=True) else: context['user_list'] = User.objects.filter(is_superuser=False, is_staff=False) return context def test_func(self): return self.request.user.is_superuser and the urls which produce hits are like this <a href="{% url 'admin:group_list' group='admin' %}"><i class="fa fa-adn"></i>Admin</a> Template is big so there is no need to post that. This is producing results but is there a better way to do this? How can I make Groups and then render the data? NOTE- How can I just make a link and view that will sort all the Users according to the link clicked. (same way in Django- Admin) -
saving hall id .... Error: NOT NULL constraint failed
I try to do hall booking app. I Have set up every things. the model and Serializer view set and urls. I have EndPoint that let user booking the halls. But when i try to booking hall it give me error. I lock at the problem 'NOT NULL constraint failed' after adding to models.py. they suggest to put null in the filed. But in my case i must save the hall id otherwise I can not know the hall the user is booking (it save null in the hall) at the beginning it show this Error: NOT NULL constraint failed: api_bookingmodel.user_id and I solve it by putting in the function perform_create ... serializer.save(user=self.request.user) This is my Code.... Model classes class HallModel(models.Model): owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='hall_owner') name = models.CharField(max_length=100) price = models.FloatField() phone = models.CharField(max_length=9, blank=True) size = models.CharField(max_length=50) region = models.CharField(max_length=100) state = models.CharField(max_length=100) image_1 = models.ImageField(upload_to='halls_image') image_2 = models.ImageField(upload_to='halls_image') image_3 = models.ImageField(upload_to='halls_image') created_at = models.DateTimeField(auto_now_add=True) class BookingModel(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_booking') hall = models.ForeignKey(HallModel, on_delete=models.CASCADE, related_name='hall_owner') time_date = models.DateTimeField(auto_now_add=True) booking_method = models.IntegerField() Serializer class HallSerializer(serializers.ModelSerializer): booking = serializers.SerializerMethodField() class Meta: model = HallModel fields = '__all__' def get_booking(self, obj): booking = BookingSerializer(obj.hall_owner.all(),many=True).data return booking def perform_create(self, serializer): … -
List of Users ordered by the rank of their Posts reviews
I want to make an API End Point so the user can get a list of the users in his city ordered by their post reviews I have defined a method in the post model to calculate the total review (up vote and down vote), all is needed to do is to groupBy user_fk in the post and orderBy sum(count_review()), but I don't know how to do it in django Post Model class Post(models.Model): title = models.TextField(max_length=255, default='Title') post_owner = models.ForeignKey(MyUser, on_delete=models.CASCADE) description = models.TextField(max_length=255) city = models.ForeignKey(City, related_name='location', on_delete=models.CASCADE) longitude = models.CharField(max_length=255) image = models.CharField(max_length=255, default='https://www.eltis.org/sites/default/files/styles/web_quality/public/default_images/photo_default_2.png') latitude = models.CharField(max_length=255) created_at = models.DateTimeField(auto_now_add=True) def count_reactions(self): likes_count = Reaction.objects.filter(post=self.id, is_like=True).count() dislikes_count = Reaction.objects.filter(post=self.id, is_like=False).count() return likes_count - dislikes_count def owner(self): return self.post_owner MyUser Model class MyUser(AbstractUser): phone_number = models.BigIntegerField(blank=False, unique=True) city = models.ForeignKey(City, related_name='city', on_delete=models.CASCADE) address = models.CharField(max_length=255) def owner(self): return self The expected result is to get the ordered list of the users by their posts reviews but only the users in the same city (city field in MyUser model) -
How to use select_related in save method of a model?
Lets say I have the following models: from django.db import models class X(models.Model): ... class Y(models.Model): ... x = models.ForeignKey(X, on_delete=models.CASCADE) class Z(models.Model): ... y = models.ForeignKey(Y, on_delete=models.CASCADE) Now, in a normal query, I could chain select_related() functions like so: z = Z.objects.select_related('y__x').get(pk=1) This would automatically get the related X and Y objects. Now, what if I wanted to use select_related() in the Z class's overridden save() method. How would I go about doing this? Thanks. -
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect when trying to run server in Django
I wanted to create a login page for users on my website, but I cannot run the server because of an error. It seems as if I have specified a filename in the wrong way, but I can't find it. I am following Python Crash Course, so I examined the code a few times and nothing seems wrong. urls.py from django.conf.urls import url from django.contrib.auth import views as auth_views from . import views urlpatterns = [ # Login page url('login/', auth_views.LoginView.as_view(template_name='users/login.html'), name='login') ] base.html <p> <a href="{% url 'learning_logs:index' %}">Learning Log</a> - <a href="{% url 'learning_logs:topics' %}">Topics</a> - {% if user.is_authenticated %} Hello, {{ user.username }}. {% else %} <a href="{% url 'users:login' %}">log in</a> {% endif %} </p> {% block content %}{% endblock content %} login.html {% extends "learning_logs/base.html" %} {% block content %} {% if form.errors %} <p>Your username and password didn't match. Please try again.</p> {% endif %} <form method="post" action="{% url 'users:login' %}"> {% csrf_token %} {{ form.as_p }} <button name="submit">log in</button> <input type="hidden" name="next" value="{% url 'learning_logs:index' %}" /> </form> {% endblock content %} This is the traceback I get in terminal: OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect … -
How to install quasar framework on django-vue project?
I try to install quasar framework on django-vue project. And always have any issues. Can someone give me a guide-link or help with it? I'm newbie on it and can't understand what I need to do -
convert 'set' object to back to its querryset object
b = Blog.objects.all() c= Blog2.objects.all() a= set(b+c) now type(a) is 'set' object i want to convert back it into class 'django.db.models.query.QuerySet'. -
How to fix "Cant Update profile in Django"
I ran into such a problem. The account has the ability to change the plan from free to premium for a certain period. When a user selects a package and synchronizes it in a date base is not saved. What's my mistake? Here is the code # model.py CHOICES = [('Free', 'free'), ('Premium', 'premium')] class MemberShip(models.Model): title = models.CharField("Title", max_length=100) period = models.IntegerField("Period", default=30) price = models.IntegerField("Price", default=2, help_text="Price in dollars") def __str__(self): return f'Plan - {self.title}' class Profile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) status = models.CharField("Status", max_length=20, choices=CHOICES, default='Free') end_date = models.DateTimeField("End Date", blank=True, null=True) membership = models.ForeignKey(MemberShip, on_delete=models.SET_NULL, null=True, default=None) def __str__(self): return self.user.username # def get_absolute_url(self): # return reverse('account:profile', args=['id', self.id]) @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: Profile.objects.create(user=instance) @receiver(post_save, sender=User) def save_user_profile(sender, instance, **kwargs): instance.profile.save() # form.py class PremiumForm(forms.ModelForm): class Meta: model = Profile fields = ['membership'] # view.py class GetPremium(LoginRequiredMixin, UpdateView): model = Profile form_class = PremiumForm template_name = 'account/premium.html' success_url = reverse_lazy('account:dashboard') def get_object(self, **kwargs): return get_object_or_404(User, pk=self.request.user.id) -
If statement Could not parse the remainder: '|' from '|"
I'm trying to use a filter inside of an if statement and whenever I do, I get the error below. I have looked through for typos and wasn't able to catch anything. Details.html (the template) {% extends "dashboard/header.html" %} {% block content %} {% load custom_filts %} <h1>{{ result }} <span class="badge badge-pill badge-info" style="background-color: {{ colour | hexify }}">{{ result | get_model_name }}</span></h1> {% if result|get_model_name == "Fixture" %} {% block fixture_data %} {% endblock %} {% elif result | get_model_name == "Cable" %} {% block cable_data %} {% endblock %} {% elif result | get_model_name == "Adapter" %} {% block adapter_data %} {% endblock %} {% endif %} {% endblock %} My custom filters from django import template register = template.Library() @register.filter def get_model_name(value): return value.__class__.__name__ @register.filter def hexify(value): return "#" + value When this is used, I get this error: TemplateSyntaxError at /dashboard/detail/1 Could not parse the remainder: '|' from '|' -
How to access a Serializer field from the model's View?
I'm trying to access a custom defined variable in the model's Serializer class from the model's View. But, I haven't found the correct way to do that. So, any help would be appreciated. I've tried to take its value only by its name, but that wasn't successful. I have this model: class candidates(models.Model): id = models.IntegerField(primary_key=True, blank=True) name = models.CharField(max_length=255, blank=True, null=True) gender = models.CharField(max_length=10, blank=True, null=True) skill = models.TextField(blank=True, null=True) note = models.TextField(blank=True, null=True) class Meta: managed = False db_table = 'candidates' This is the serializer for the model: class CandidatesSerializer(serializers.ModelSerializer): experiences = CandidateExperienceSerializer(many=True) full_duration = serializers.SerializerMethodField('get_full_duration') def get_full_duration(self, candidate): sum = 0 for exp in candidate.experiences.all(): if exp.work_from is None: exp.work_from = now() if exp.work_to is None: exp.work_to = now() sum += int((exp.work_to - exp.work_from).days / 30) return sum class Meta: model = candidates fields = ['id', 'name', 'gender', 'skill', 'note', 'full_duration', 'experiences'] In the serializer, I have an additional custom field 'full_duration' where I store the full experience duration of the candidate (in months). Finally, here's the part of the view for the model, where I have the problem: if min != 'null': candidates_list = candidates_list.filter(full_duration__gte=min) if max != 'null': candidates_list = candidates_list.filter(full_duration__lte=max) I expect the output … -
Django rest framework serialization tutorial confused
I don't get what following code does.I don't get from where "item[1][0]" comes to line 8. It is not defined.I don't get what next line does also. btw I have a java background. I get the idea of comprehensions so I get the seventh line. from django.db import models from pygments.lexers import get_all_lexers from pygments.styles import get_all_styles LEXERS = [item for item in get_all_lexers() if item[1]] LANGUAGE_CHOICES = sorted([(item[1][0], item[0]) for item in LEXERS])` STYLE_CHOICES = sorted([(item, item) for item in get_all_styles()])` -
Exclude records from query set providing they are a part of subquery and meet condition
I need to exclude records from one query set if another contains records with same field values and under certain condition. This is rather hard to digest requirement so I’ll explain on real problem that I’m trying to solve: A restaurant has time slots when guests can book tables (1pm, 1:30pm, 2pm, etc.). Each time slot has availabilities. Availability is a table (table for 2, table for 4, etc.) and number of how many tables of this type are available for this time slot (3 tables for 2, 3 tables for 4, 2 tables for 8, etc.). When booking a table reservation does not keep relation to exact table or availability but just stores timestamp when reservation is going to happen and number of guests. Through time when reservations are created for time slots availabilities should be decreased but not by decreasing number of bookable tables but by counting reservations for each specific time slot. We arrived at the problem itself: At some point I need to list time slots available to book with given number of guests for given date but exclude time slots where table capacity was reached. In terms of query what needs to be done is … -
images not displayed in heroku website despite being visible from s3
the image from s3 haS an enscription, while after opening image from the website there is no enscription.am not sure if this is the problem.how can i solve this or how can i get rid of the enscription AWS_ACCESS_KEY_ID="xxxxxxxxxx" AWS_SECRET_ACCESS_KEY="rrrrrrrrr" AWS_STORAGE_BUCKET_NAME="ddddddddd" SECRET_KEY="44444" AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME AWS_S3_OBJECT_PARAMETERS = { 'CacheControl': 'max-age=86400', } AWS_LOCATION = 'mysite/media' STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage' -
Django. Sort objects in a chain
I set up sorting related to the object manager. I have views.py def Objects(request): Objects_1 = car_type_1.objects.filter(manager_id='1', is_published=True) Objects_2 = car_type_2.objects.filter(manager_id='1', is_published=True) Objects_3 = car_type_3.objects.filter(manager_id='1', is_published=True) queryset_list = list(chain(Objects_1, Objects_2, Objects_3)) paginator = Paginator(queryset_list, 6) page = request.GET.get('page') paged_listings = paginator.get_page(page) context = {'listings': paged_listings} return render(request, 'template.html', context) As a result, I get a page with objects. Responsible manager id 1 for all objects. Everything is sorted correctly on the page. Showing manager id 1 objects. But, they are shown, one by one. First all from the group car_type_1, then the group car_type_2 and car_type_3. Question. How to make objects sorted by price from all groups? Thank! -
How to display the data inputted through TinyMCE in text but not HTML format?
I'm having problem in displaying data inputted through TinyMCE in text format. Instead, data with HTML format is returned. Some of my toolbar also were not displayed Followed the django-tinymce docs but it doesn't show how to change the format custom.js: tinymce.init({ selector: 'textarea', height: 500, theme: 'advanced', plugins: [ 'advlist autolink lists link image charmap print preview anchor textcolor', 'searchreplace visualblocks code fullscreen', 'insertdatetime media table paste code help wordcount' ], toolbar: 'undo redo | formatselect | bold italic backcolor | fontselect | fontsizeselect | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | removeformat | help', content_css: [ '//fonts.googleapis.com/css?family=Lato:300,300i,400,400i', '//www.tiny.cloud/css/codepen.min.css' ] }); models.py: container_description = HTMLField() forms.py: container_description = forms.CharField(required=False, widget=TinyMCEWidget(attrs={'placeholder': 'Enter description'})) I want to display the datas in text not with html tags and also display my missing toolbar icons -
Django limit to one choise in foreing key
Need somehow to limit in models ForeignKey to one from available choices. It's my models : class CustomCompany(models.Model): name = models.CharField(max_length=30, default=None, unique=True ) teams = ListCharField( base_field=models.CharField(max_length=15), size=15, max_length=(15*16), default=["Owners"], unique=True ) class CustomUser(AbstractUser): company = models.ForeignKey(CustomCompany, on_delete=models.CASCADE, default='None', to_field='name', related_name='company' ) team = models.ForeignKey(CustomCompany, on_delete=models.CASCADE, default='Owners', to_field='teams', related_name='team', ) And from this, I have issue with "team" model. Cause to team model assignment all choices, but I want to select only one from available. Someone had this issue? Is it possible to limit this ForeignKey to only one choose? Thx -
How to create class based pagination in django on custom array
I am new to python and I am using django class based pagination with below code: class Demo(LoginRequiredMixin, ListView): model = users template_name = 'users_list.html' context_object_name = 'users' filterset_class = users paginate_by = 10 def get_queryset(self): // Code goes here // Result array after custom array creation : {25474: {'address__is_active': 1, 'id': 1, 'updated_by': '', 'count': 1}, 25475: {'address__is_active': 1, 'id': 2, 'count': 1, updated_by': ''}} return qs def get_context_data(self, **kwargs): context = super(Demo, self).get_context_data(**kwargs) context['filter_form'] = UserFilterForm(self.request.GET) page_query = '' for key,value in self.request.GET.items(): if key != 'page': page_query+='&'+key+'='+value context['page_query'] = page_query return context I am able to see pagination UI in my list but pagination is not working properly because I have make custom array like below: {25474: {'address__is_active': 1, 'id': 1, 'updated_by': '', 'count': 1}, 25475: {'address__is_active': 1, 'id': 2, 'count': 1, updated_by': ''}} Could anyone please assist how I can use class based pagination on custom array.