Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to use connection pooling for redis.StrictRedis?
In order to be able to user redis lists in my django app, I use a lot of redis.StrictRedis connections instead of stardard django cache.get and cache.set. My example codes are like: r = redis.StrictRedis(unix_socket_path='/var/run/redis/redis.sock', db=3) posts = r.lrange('posts', 0 , -1) However I have encountered some performance issues (gunicorns being stalled at high load and I get frequent 502) I suppose these are due to excessive redis.StrictRedis connections without using a pooling. So I'm wondering how can I use a connection pool instead of making a connection to redis for each data fetch? -
How to fix csv upload with ID "3/Ewurafua/processed.cleveland.data.csv" doesn't exist. Perhaps it was deleted?
I am trying to upload a csv file on the django admin site using the tomwalker django-quiz-app open source library from github, when it tells me that the upload was successful but when i try to access the uploaded file, it gives an error. def csv_upload_post_save(sender, instance, created, *args, **kwargs): if not instance.completed: csv_file = instance.file decoded_file = csv_file.read().decode('utf-8') io_string = io.StringIO(decoded_file) reader = csv.reader(io_string, delimiter=';', quotechar='|') header_ = next(reader) header_cols = convert_header(header_) print(header_cols, str(len(header_cols))) parsed_items = [] ''' if using a custom signal ''' for line in reader: # print(line) parsed_row_data = {} i = 0 print(line[0].split('\t'), len(line[0].split('\t'))) row_item = line[0].split('\t') for item in row_item: key = header_cols[i] parsed_row_data[key] = item i+=1 # create_user(parsed_row_data) # create user parsed_items.append(parsed_row_data) # messages.success(parsed_items) print(parsed_items) csv_uploaded.send(sender=instance, user=instance.user, csv_file_list=parsed_items) instance.completed = True instance.save() post_save.connect(csv_upload_post_save, sender=CSVUpload) I expected to be able to view the file since it showed that the upload was successful but it prints out "Csv upload with ID "3/Ewurafua/processed.cleveland.data.csv" doesn't exist. Perhaps it was deleted?". -
How to pass validation of uniquely constrained field in order to update model record
I've created a view that pre-loads information from a previous entry and allows the user to make and save changes. I have a unique constraint in my model for a particular field which I need to maintain for initial record creation. However if they submit the form in the edit view with the same entry value for this field it obviously doesn't validate. I see there being some scenarios: Initial unique field value == edited unique field value: Doesn't pass validation but I want it to update the record. Initial unique field value != edited unique field value: Validates if field value not in fields list then Creates new record or doesn't validate as field value is in field list and returns unique errors to template I need to provide a means to pass validation in '1.' to enable me to update the record. Should I override the Validate method for the field or is there a better way of doing this? Thanks -
Using Django in Docker unable to fetch rest api
I have built the webapp using Django and also configured for Rest api. I am able to fetch the data via curl and also by python script. Issue I'm running into, when using Docker container, when user fill in the data and submit, it goes thru and ran the python function, but it stop working at when fetching the data via api and thru errors Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python3.6/site-packages/requests/models.py", line 897, in json return complexjson.loads(self.text, **kwargs) File "/usr/lib64/python3.6/json/__init__.py", line 354, in loads return _default_decoder.decode(s) File "/usr/lib64/python3.6/json/decoder.py", line 339, in decode obj, end = self.raw_decode(s, idx=_w(s, 0).end()) File "/usr/lib64/python3.6/json/decoder.py", line 357, in raw_decode raise JSONDecodeError("Expecting value", s, err.value) from None json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) I'm able to curl the URL inside the container. I get the same error when shell into the container in python3.6 sh-4.2# python3.6 Python 3.6.8 (default, May 2 2019, 20:40:44) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> >>> import requests, json >>> url = 'http://127.0.0.1:8001/db/data/' >>> response = requests.get(url) >>> parsedjson = json.loads(response.text) ERROR SAME AS ABOVE . . . parsedjson = … -
Coverage couldn't run 'manage.py' as Python code
I am using Coverage to measure my test code, but I stuck on a weird problem. I tried the command according to Django Documentation: coverage run --source='.' manage.py test myapp But I got this error: Coverage.py warning: No data was collected. (no-data-collected) Couldn't run 'manage.py' as Python code: SyntaxError: invalid syntax (manage.py, line 16) It works correctly using python manage.py test myapp. Here is my manage.py (I didn't modify anything). #!/usr/bin/env python """Django's command-line utility for administrative tasks.""" import os import sys def main(): os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'mytestsite.settings') try: from django.core.management import execute_from_command_line except ImportError as exc: raise ImportError( "Couldn't import Django. Are you sure it's installed and " "available on your PYTHONPATH environment variable? Did you " "forget to activate a virtual environment?" ) from exc execute_from_command_line(sys.argv) if __name__ == '__main__': main() What am I missing exactly? I ran my code and coverage in virtualenv, but I don't think this could be a problem. ENV: Python 3.6.8 Django 2.2.3 Coverage 4.5.4 -
Performance Issues in Celery
I already read other posts about that but none of them solve my problem. I have function to scan all ports for a given ip. It takes 5-10 seconds to complete without celery but if I use the celery to run this function, it takes approximately 15 minutes. I observed that function without celery, consuming approximately %90 cpu power but with celery this value is %2-%3 so task takes 15 minutes to finish. This causes a serious performance problem for me. How to increase a cpu power for a specific task or any idea to solve this problem? -
How to render Form-sets out of forms.ModelForm [model is connected to User using a ForeignKey]
I have a model called Ans as class Ans(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) ans = models.CharField(max_length=1024) and a Form called AnswerForm class AnsForm(forms.ModelForm): class Meta: model = Ans fields = ('ans',) labels = { "ans": '' } help_texts = { 'ans': 'Click ADD or press Enter to add' } I want users to add multiple answers to the question. I have done a not so very exciting way around on this. What I do is that I render the form every time no matter what and then I have provided a link to Proceed so that users can go to next page. It does not look good. Can Somebody please tell me how can I use FormSets on this so that when user press the submit button, he can be directed to the link instead of re rendering the form again again. NOTE- When my form re-renders, it shows the old value as initial input. Can some one help me on this one too about how to clear that field when the form re renders -
Faling to create superuser using custom User model django
I'm trying to make a custom django user model. My User model looks like this: class User(AbstractBaseUser): username = models.CharField(max_length=255,unique=True, default='ak') builder = models.BooleanField(default=False) broker = models.BooleanField(default=False) standarduser = models.BooleanField(default=True) active = models.BooleanField(default=True) staff = models.BooleanField(default=False) # a admin user; non super-user admin = models.BooleanField(default=False) # a superuser objects = UserManager() USERNAME_FIELD = 'username' REQUIRED_FIELDS = ['builder','broker','standarduser'] UserManager.create_user() def create_user(self, username, password, builder=False, broker=False, standarduser=True, active=True, staff=False, admin=True): if not username: raise ValueError('enter username') if not password: raise ValueError('enter password') user_obj = self.model( username, password=password, builder=builder, broker=broker, standarduser=standarduser ) user_obj.active = active user_obj.staff = staff user_obj.admin = admin user_obj.set_password(password) user_obj.save(using=self._db) return user_obj UserManager.create_superuser() def create_superuser(self, username, password, builder, broker, standarduser): user = self.create_user( username, password, builder=builder, broker=broker, standarduser=standarduser, staff=True, admin=True ) return user When I try to create a superuser (username: ak), after entering all the required fields I receive the following error: ValueError: invalid literal for int() with base 10: 'ak' I tried clearing the migrations as well but still received the same error. I have also tried this in a new environment, still no luck. Please help me out here, thanks. -
datefield does not show today date in html form even if datetime.date.today is specified
I am using a datefied in my form and html form to render it. i want the date field to be non-editable todays date, but it shows the datepicker. Could anyone please help model: Dataset_Creation_Date = models.DateField(null=True, blank=True, default=datetime.date.today) HTML: <label for="field3"><span>Date of Dataset Creation <span class="required">*</span></span><input class="input-field" name="Dataset_Creation_Date" value="" type="date"/></label> -
ImageField doesn't work when creating the ModelForm
models.py from PIL import Image class Award_list(models.Model): award_id=models.AutoField(primary_key=True) award_name=models.CharField(max_length=50,blank=False) award_image = models.ImageField(upload_to='award_pics') def __str__(self): return f'{self.award_name} award' def save(self,*args,**kwargs): super().save(*args,**kwargs) img = Image.open(self.award_image.path) if img.height > 300 or img.width > 300: output_size = (300, 300) img.thumbnail(output_size) img.save(self.award_image.path) class Meta: verbose_name_plural = 'Award' forms.py from django import forms from movies_list.models import Award_list class Create_Award_list_form(forms.ModelForm): class Meta: model=Award_list fields=['award_name','award_image'] views.py from .models import Award_list from .forms import Create_Award_list_form def Award_list_create(request): form=Create_Award_list_form(request.POST or None) if form.is_valid(): # print(request.POST['award_name']) form.save() else: print(form.errors) context={ "hell":'sanjeet', "form":form } return render(request,'movies_list/award_list_create.html',context) urls.py urlpatterns = [ path('movies/awards/new/',Award_list_create,name="award-list-create"), ] award_list_create.html {% extends "users/base.html" %} {% load crispy_forms_tags %} {% block content %} <div class="content-section"> <form method="POST"> {% csrf_token %} <fieldset class="form-group"> <legend class="border-bottom mb-4">Blog Post</legend> {{form.as_p}} {{hell}} </fieldset> <div class="form-group"> <button class="btn btn-outline-info" type="submit">Submit</button> </div> </form> </div> {% endblock content %} This all line of code work all fine with admin page and selecting image save into MEDIA_URL/awards_pics file but when i create a models based forms it select the default option even after selecting the image in .html form. -
How to add 2FA in Django and DRF
I am not sure how to add 2FA to my django project and DRF ? I will eventually connect this DRF with react and redux as the frontend. Here is my Django side: Models: from django.contrib.auth.models import AbstractUser class User(AbstractUser): def __str__(self): return self.username My Django api serializer # Login API class LoginAPI(generics.GenericAPIView): serializer_class = LoginSerializer def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.validated_data return Response({ "user": UserSerializer(user, context=self.get_serializer_context()).data, "token": AuthToken.objects.create(user)[1] }) # Check token and send back what user is associated with that token class UserAPI(generics.RetrieveAPIView): permission_classes = [ permissions.IsAuthenticated, ] serializer_class = UserSerializer def get_object(self): return self.request.user My Django urls: from django.urls import path, include from knox import views as knox_views from accounts.api.views import LoginAPI, UserAPI urlpatterns = [ path('api/auth', include('knox.urls')), path('api/auth/login', LoginAPI.as_view()), path('api/auth/user', UserAPI.as_view()), path('api/auth/logout', knox_views.LogoutView.as_view(), name='knox_logout'), My django views: from rest_framework import generics, permissions from rest_framework.response import Response from knox.models import AuthToken from accounts.api.serializers import LoginSerializer, UserSerializer # Login API class LoginAPI(generics.GenericAPIView): serializer_class = LoginSerializer def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) user = serializer.validated_data return Response({ "user": UserSerializer(user, context=self.get_serializer_context()).data, "token": AuthToken.objects.create(user)[1] }) # Check token and send back what user is associated with that token class UserAPI(generics.RetrieveAPIView): permission_classes = … -
How to check is user email is already saved in DB
I am trying to add email field to a model but before saving data I need to check whether the entered email already exists in DB or not. If not then save the data otherwise display error message. The current code is saving duplicate data. Can someone explain me what I am doing wrong here? I am new to Djanog DRF, please bear with me. What I have done so far Model.py class Subscribe(models.Model): subscribe_email = models.EmailField(max_length=250) contacted_on = models.DateTimeField(auto_now_add=True) def __str__(self): return self.subscribe_email Serializer.py class SubscriberCreateSerializer(ModelSerializer): class Meta: model = Subscribe fields = ('subscribe_email', ) def create(self, validated_data): instance = super(SubscriberCreateSerializer, self).create(validated_data) email = format(instance.subscribe_email) is_exists = Subscribe.objects.filter(subscribe_email=email).exists() if is_exists: raise ValidationError('Email exists') else: try: send_mail( 'Thank You for subscribing newsletter!', 'This mail is intended to confirm your successful submission for newsletter', settings.EMAIL_HOST_USER, [email, ], fail_silently=False, ) except BadHeaderError: raise ValidationError('Something went wrong, Please try again') return instance Views.py class SubscriberCreateAPIView(CreateAPIView): queryset = Contact.objects.all() serializer_class = SubscriberCreateSerializer def perform_create(self, serializer): serializer.save() Edited: I have found the solution. I changed the perform_create method Can someone please review this? def perform_create(self, serializer): subscribe_email = serializer.validated_data.get('subscribe_email') is_exists = Subscribe.objects.filter(subscribe_email=subscribe_email).exists() if is_exists: raise ValidationError('Email exists') else: serializer.save() -
In Django prevent deletion or editing of a single default record
I am using Django 1.6 version and I have a Django model for which i want to add a default record. I can add that record using fixtures or sql, but i want to prevent that particular record from being deleted. In the admin panel i should see the record say as disabled so that no user can edit or delete only that record. They can edit or delete other records. Is this possible? I tried looking at get_actions or has_delete_permission but that apply to all records and not just the single default record -
Make models fields readonly in all admin pages
My project has multiple Models and custom admin pages for the models. All the Models inherit from a "BaseModel". For business functionality, we had to update our "Base model" to include 2 new fields. Given that all models inherit these 2 new fields, they are now showing up in admin pages as editable fields. As per business functionality, these two fields should be displayed as read-only fields For making fields readonly we normally use readonly_fields = [read only..] in admin class. Is there a way to achieve this without touching all the admin classes? -
How can I block certain words from being used in Django forms?
So I am creating a website where users can make posts, but I want to block certain words from being used in the Django form. For example, I don't want them to mention CP, or anything else illegal. How could I block these words from being displayed or throw an error message at the user I haven't really tried anything, but I have been researching, to no luck. Here is the post model. class Post(models.Model): author = models.ForeignKey(User, on_delete=models.CASCADE) title = models.CharField(max_length=75) text = models.TextField(max_length=250) created_date = models.DateTimeField(default=timezone.now) published_date = models.DateTimeField(blank=True,null=True,auto_now_add=True) tags = TaggableManager() def publish(self): self.published_date = timezone.now() self.save() def approve_comments(self): return self.comments.filter(approved_comment=True) def get_absolute_url(self): return reverse('mainapp:post_detail',kwargs={'pk':self.pk}) def __str__(self): return self.title And here is the form class PostForm(forms.ModelForm): class Meta(): model = Post fields = ('title','text') widgets = { 'title':forms.TextInput(attrs={'class':'textinputclass'}), 'text':forms.Textarea(attrs={'class':'textareaclass'}), } And here is the CreatePostView class CreatePostView(LoginRequiredMixin,CreateView): login_url = '/login/' redirect_field_name = 'mainapp/post_details.html' form_class = PostForm model = Post def form_valid(self, form): form.instance.author = self.request.user return super().form_valid(form) I want it so that it throws an error if you include a word that's blocked Thank you for any help that you can give :) -
Django abstract model field depends on children attributes
I want to code some model structure containing abstract parent model and child models with same fields but different names, help_text and related_name. How better and shorter implement this behavior? Is there any solutions at all? class AbstractModel(models.Model): class Meta: abstract = True name = None code = None rel_field = models.OneToOneField(RelatedModel, on_delete=models.CASCADE, primary_key=True, related_name=code) field = models.CharField(name, null=True, help_text=HELP_DICT.get(code, None)) class ChildOne(AbstractModel): name = "child one name" code = "one" class ChildTwo(AbstractModel): name = "child two name" code = "two" child_field = SomeAnotherField() I want to make ChildOne.field == models.CharField("child one name", help_text=HELP_DICT.get("one", None) ...) ChildTwo.field == models.CharField("child two name", help_text=HELP_DICT.get("two", None) ...) and RelatedModel.one == ChildOne RelatedModel.two == ChildTwo -
How to create a one time link on Django for video file
i need to create a one time link on Django for my videos from theft. Please show an example of how something can be done. Sorry for my bad English -
Issue while trying to use a tag
I'm trying to use this tag which allow me to check if a map got permissions in the site I'm working on using Geonode. {% get_obj_perms request.user for resource.get_self_resource as "layer_perms" %} {% if "view_resourcebase" in layer_perms %} When I used it in other files, everything works fine but in this file, the page I'm on crash and when I check the apache log I have this problem. [Thu Sep 12 01:05:26.388898 2019] [wsgi:error] [pid 18375:tid 140402620434176] VariableDoesNotExist: Failed lookup for key [resource] in u"[{'False': False, 'None': None, 'True': True}, {}, {}, {'facet_type': 'layers', 'is_layer': True, u'view': <django.views.generic.base.TemplateView object at 0x7fb1dce66390>}]" -
IOError: [Errno 13] Permission denied: '/home/ubuntu/autobot/nohup.out'
I used virtual env to host my python project on to the local host but when visit my localhost, the website is IOError at /gobot/dcae3db9ad68d046e97d503da41a068aed8663feec28882d94 [Errno 13] Permission denied: '/home/ubuntu/autobot/nohup.out' Why am I getting this error ? Please help. Internal Server Error: /gobot/dcae3db9ad68d046e97d503da41a068aed8663feec28882d94 Traceback (most recent call last): File "/home/ubuntu/autobot/myproject/myprojectenv/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 42, in inner response = get_response(request) File "/home/ubuntu/autobot/myproject/myprojectenv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "/home/ubuntu/autobot/myproject/myprojectenv/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "/home/ubuntu/autobot/myproject/myprojectenv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 68, in view return self.dispatch(request, *args, **kwargs) File "/home/ubuntu/autobot/myproject/myprojectenv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 67, in _wrapper return bound_func(*args, **kwargs) File "/home/ubuntu/autobot/myproject/myprojectenv/local/lib/python2.7/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) File "/home/ubuntu/autobot/myproject/myprojectenv/local/lib/python2.7/site-packages/django/utils/decorators.py", line 63, in bound_func return func.__get__(self, type(self))(*args2, **kwargs2) File "/home/ubuntu/autobot/gobot/views.py", line 360, in dispatch return generic.View.dispatch(self, request, *args, **kwargs) File "/home/ubuntu/autobot/myproject/myprojectenv/local/lib/python2.7/site-packages/django/views/generic/base.py", line 88, in dispatch return handler(request, *args, **kwargs) File "/home/ubuntu/autobot/gobot/views.py", line 352, in get -
Django admin, setting TIME_ZONE not showing correct datetime
I have set these settings on my project: TIME_ZONE = 'Asia/Almaty' USE_TZ = True Asia/Almaty will add extra 6 hours to UTC. When I create my models objects in django admin, it shows date times likes this: Where time should be 9:21. It still shows UTC datetime. date command on my linux machine returns this: Thu Sep 12 11:41:21 +06 2019 Why django admin still shows wrong datetime? -
How to update (put/post request) related fields with Django rest api?
I'm new to Django. Within the Django rest framework, I'm struggling with the put & post request on a table that's linked to other table(s). The main table I want to update via API is person, that's basically where contact details are kept. It joins to 2 other tables, email, phone, where each of them only contains 1 piece of information (as their name stated). What I'm trying to achieve is that when I submit a put/post request to person's api, it also updates email, phone without needing to update each of them respectively. Below are some code: Models: class Person(models.Model): id = models.AutoField(primary_key=True) last_name = models.CharField(max_length=50) first_name = models.CharField(max_length=50) email = models.ManyToManyField('Email') phone = models.ManyToManyField('Phone') class Meta: ordering = ['last_name', 'first_name'] def __str__(self): return f'{self.first_name} {self.last_name}' class Phone(models.Model): phone = models.CharField(max_length=50, blank=True, null=True) def __str__(self): return f'{self.phone}' class Email(models.Model): email = models.CharField(max_length=50, blank=True, null=True) def __str__(self): return f'{self.email}' Serializers: class PersonSerializer(serializers.ModelSerializer): email = serializers.StringRelatedField(many=True) phone = serializers.StringRelatedField(many=True) class Meta: model = Person fields = ('id', 'last_name', 'first_name', 'email', 'phone') class PhoneSerializer(serializers.ModelSerializer): class Meta: model = Phone fields = ('id', 'phone') class EmailSerializer(serializers.ModelSerializer): class Meta: model = Email fields = ('id', 'email') Views: class PersonViewSet(viewsets.ModelViewSet): queryset = Person.objects.all() serializer_class = … -
“How to fix ‘raise EOFError’ error in python django when running program”
i'm trying to convert python 2.7 to 3.7 and django 1.8 to 2.x, when trying to convert code in program,i'm getting an Error: " File "/usr/lib/python3.6/multiprocessing/connection.py", line 389, in _recv raise EOFError EOFError " my code is: def _recv(self, size, read=_read): buf = io.BytesIO() handle = self._handle remaining = size while remaining > 0: try: chunk = read(handle, remaining) except InterruptedError: continue n = len(chunk) if n == 0: if remaining == size: raise EOFError else: raise OSError("got end of file during message") buf.write(chunk) remaining -= n return buf -
Exclude model fields in all admin pages
My project has multiple Models and custom admin pages for the models. All the Models inherit from a "BaseModel". For business functionality, we had to update our "Base model" to include 2 new fields. Given that all models inherit these 2 new fields, they are now showing up in admin pages. As per business functionality, these two fields should not be displayed in the admin pages. For excluding fields we normally use excludes = [fields to be excluded..] in admin class. Is there a way to achieve this without touching all the admin classes? -
Django. How i can get list of Users with a duplicate?
i have array like this: [1, 1, 1, 2, 3]. How i can get users with a duplicate? For example this query return list without duplicate list= User.objects.filter(id__in=[1, 1, 1, 2, 3]) for example it will be users with id's: 1, 2, 3 but i need list of users like this: 1, 1, 1, 2, 3 -
How to query related models in django models.py
I have a user model which looks like this: class User(AbstractUser): first_name = models.CharField(max_length=70) last_name = models.CharField(max_length=70) middle_name = models.CharField(max_length=70, blank=True) email = models.EmailField( max_length=254, unique=True, verbose_name='Email Address', blank=True ) is_student = models.BooleanField(default=False, verbose_name='Student') is_superuser = models.BooleanField(default=False, verbose_name='Administrator') is_teacher = models.BooleanField(default=False, verbose_name='Teacher') is_staff = models.BooleanField(default=False, verbose_name='Staff') is_registrar = models.BooleanField(default=False, verbose_name='Registrar') and in my StudentPorile model I have a field called class_advisor which is related to User model and I need to get the users with is_teacher field is True. Now, how do i filter User model so it will just return Users with is_teacher=True value? here's my StudentProfile model: class StudentProfile(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True) lrn = models.CharField(max_length=20) landline_number = models.CharField(max_length=11, null=True, blank=True) mobile_number = models.CharField(max_length=11, null=True, blank=True) address = models.TextField(max_length=300, blank=True) mothers_name = models.CharField(max_length=50) fathers_name = models.CharField(max_length=50) contact_person_name = models.CharField(max_length=50) contact_person_number = models.CharField( max_length=12, verbose_name='Phone number of Contact Person') # class_advisor = IM STUCK HERE year_level = models.OneToOneField(Year_Level, on_delete=models.SET_NULL, null=True) section = models.OneToOneField(Section, on_delete=models.SET_NULL, null=True)