Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
What programming language should I use? [on hold]
I need to create an application and I don't know which language is suitable for it. This application should have a page where the user fills a lot of forms (like dates, strings, numbers, an adjustable line graph, a table etc...) and at the bottom should be a "SAVE" button. When the user clicks this button these things should happen: 1)The application takes all the values that the user filled the forms with and should save them in variables (or in some other way) 2)The application should create a new pdf document with all these forms that the user filled and add the forms compiled by the user, for example: in the first page, form date: the users inserts 11-05-2019, in the new pdf document there should be at the bottom left of the page a String which says: "This document was created the 11-05-2019" 3)The application should automatically save the new pdf document just created in a folder without asking the user, but using the informations provided by the forms filled by the user, for example: in the first page there is form a which the user fills with: ABC so the folder in which the document must be … -
Can someone suggest how to implement custom Django PasswordResetView?
I want to send password reset link to only confirmed email address. And also user can request password reset link by searching by username or email address which is not available in default django. I have been trying and editing the default django password reset view and form for many days. But not working for me. -
CreateView not creating any object
This is my model: class ObjectCreateView(CreateView): template_name = 'dashboard/add.html' success_url = '/' form_class = ObjectCreateForm and this is how the view is called: <a href="{% url 'dashboard:add' request.resolver_match.kwargs.slug %}"> new </a> And the urls.py: path('<slug:slug>/object/new', views.ObjectCreateView.as_view(), name='add'), Is using slug the problem here ? -
Django shell keeps losing connection to DB
Django (or postgresql) keeps losing connection from manage.py shell. I've been trying to google but couldn't find an answer. Is there a change to Django2.0+ or postgresql10.0+ ? -
Django ModelForm - access custom field in form valid
I have added a custom field to a ModelForm as per the below, however I cannot access the field in my UpdateView. I have tried printing the object, but the field device_circuit_subnet isn't in there, also referencing it outside the object throws an error. is it possible to obtain the value for a custom field in a model form so I can use it before saving the object? Thanks forms.py class SiteServiceForm(forms.ModelForm): class Meta: model = DeviceCircuitSubnets fields = ['device','circuit','subnet','monitored','interface_name','get_bgp','wb_bgp','db_bgp','get_interface','wb_interface','db_interface','get_aws','wb_aws','db_aws'] def __init__(self, *args, **kwargs): self.is_add = kwargs.pop("is_add", False) site_id = kwargs.pop("site_id") super(SiteServiceForm, self).__init__(*args, **kwargs) self.fields['device'].required = False self.fields['circuit'].required = False self.fields['subnet'].required = False self.fields['device_circuit_subnet'] = DeviceCircuitSubnetField( queryset=DeviceCircuitSubnets.objects.filter(device__site_id=site_id).select_related( 'device', 'circuit', 'subnet' ), label='Device / Circuit / Subnet' ) self.helper = FormHelper(self) self.helper.form_id = 'site_service_form' self.helper.form_method = 'POST' self.helper.layout = Layout( Div( Div( Div(HTML('<i class="fa fa-bolt fa-fw"></i> Service'), css_class='card-header'), Div( Div( Div( Field('device_circuit_subnet', css_class='chosen'), css_class='col-lg-6' ), Div( ... views.py class AddSiteService(PermissionRequiredMixin, CreateView): form_class = SiteServiceForm template_name = "app_settings/tabs_form.html" permission_required = 'config.add_device_circuit_subnet' def dispatch(self, *args, **kwargs): self.site_id = self.kwargs['site_id'] return super(AddSiteService, self).dispatch(*args, **kwargs) def get_success_url(self, **kwargs): return reverse_lazy("config:site_services", args=(self.site_id)) def get_form_kwargs(self, *args, **kwargs): kwargs = super().get_form_kwargs() kwargs['is_add'] = True kwargs['site_id'] = self.site_id return kwargs def form_valid(self, form): self.object = form.save(commit=False) print(vars(self.object)) dcs_id = … -
How can i store tournament standings
i have a model class class MobaTournament(Tournament): teams=models.ManyToMany(Team) if a tournament have 20 teams. I want to sort of rank 1st team, 2nd team, 3rd team... How do you suggest I do this? like; 1.XXX team 2.XXX team 3.XXX team -
Allow Redirect To Disallowed Urls
In Django I am writing a view that will redirect the user to a "file:///*" url using the redirect method, but i am getting the Disallowed Url error. I understand that Django does this on purpose(I assume for security reasons), but in my case the use is intentional and by design. So my question is, how can I allow certain disallowed url schema's in django? This server is an internal server with no access to the outside world, also only users with the mapped network drive will be able to benefit from this (otherwise the browser just wont find the folder). These being said, I don't really see a negative security implication of doing this. My redirect method is as follows: def get_job_folder(request,jobnumber): import os jobnumber = "23456" p = r"P:/Engineer/DWGS/00NEW_DWGS/" f = [name for name in os.listdir(p) if name.strip().startswith(jobnumber)] if f != []: ret = "file:///" + p + f[0] response = redirect(ret) return response else: return JsonResponse({'message':"error. job not found"}) -
Send requests.get() from Django and display the parsed response
I am a total beginner with using Django. Using requests.get() and Django 2.2, i want to send a request from django, then display the parsed response. normally to achieve this i would do the following: import requests from bs4 import BeautifulSoup as bs response = requests.get(url) soup = bs(response.text,'lxml') print(soup.title.text) what i am expecting is to use two html pages, the first html page is a form to receive input from the user, and the second page is to display the results of the request after parsing out the response. this may be asking a bit much, because i sort-of need a basic walk-through. (Not only am i new with Django, but many of the concepts of object-oriented programming are new to me as well, so i am having several different types of issues, like with the self variable, and with returning values, and its not unlikely that my code is incorrectly structured. please bear with me.) myproject/myapp/forms.py import requests from django import forms from bs4 import BeautifulSoup as bs class SearchForm(forms.Form): url = forms.CharField() def send_request(self): response = requests.get(url) soup_obj = bs(response.text, 'lxml') soup_title = soup.title.text soup_len = len(soup.title.text) return soup_obj, soup_title, soup_len how do i pass soup_obj, soup_title, … -
How to run another python file in the same directory with manage.py
I cloned a GitHub repo. Everything is working fine. But I need to populate sample data for all the endpoints. There about 20 files with sample data for each endpoint. Then there is a file(dataload.py) in the root folder that should call all those 20 files and populate the database. I ran python dataload.py but I got the error File "manage.py", line 17, in <module> "Couldn't import Django. Are you sure it's installed and " 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? This is the content of dataload.py from subprocess import call print ('**** starting ***') SETTINGS_FILE= 'promedic.settings_prod' # SETTINGS_FILE= 'promedic.settings' call(['python', 'manage.py', 'makemigrations', '--settings=%s'% SETTINGS_FILE]) call(['python', 'manage.py', 'migrate', '--settings=%s'% SETTINGS_FILE]) call(['python', 'manage.py', 'loaddata', 'core/fixtures/allergies.json', '--settings=%s'% SETTINGS_FILE]) call(['python', 'manage.py', 'loaddata', 'core/fixtures/blood_group.json', '--settings=%s'% SETTINGS_FILE]) call(['python', 'manage.py', 'loaddata', 'core/fixtures/disabilities.json', '--settings=%s'% SETTINGS_FILE]) call(['python', 'manage.py', 'loaddata', 'core/fixtures/drug-forms.json', '--settings=%s'% SETTINGS_FILE]) call(['python', 'manage.py', 'loaddata', 'core/fixtures/drug-brands.json', '--settings=%s'% SETTINGS_FILE]) call(['python', 'manage.py', 'loaddata', 'core/fixtures/dispense-types.json', '--settings=%s'% SETTINGS_FILE]) call(['python', 'manage.py', 'loaddata', 'core/fixtures/genotypes.json', '--settings=%s'% SETTINGS_FILE]) call(['python', 'manage.py', 'loaddata', 'core/fixtures/states.json', '--settings=%s'% SETTINGS_FILE]) -
Cant import models.py in views.py
I want to use my model file from my database by importing it in views.py. But I get an error when I call a class from models.py. ERROR: ImportError: cannot import name Vehicle My path +vehicle ---> _ init _.py --->models.py --->views.py vehicle/models.py from __future__ import unicode_literals from django.db import models class Vehicle(models.Model): id = models.BigAutoField(primary_key=True) plate = models.TextField() driver = models.ForeignKey(Driver, models.DO_NOTHING) class Meta: managed = False db_table = 'vehicle' Vehicle/views.py from Vehicle.models import Vehicle s = Vehicle.objects.all() print s -
Add dir to Django search path for tests
I am use a single django for various custom distributions. I arrived at a solution where the apps unique to each distribution live outside of the django projects folder (ltes call it DIR/), and added this outside DIR/ to the Django path using How to keep all my django applications in specific folder (adding to the sys.path solution) The project runs, however now it does not discover tests. Before moving the apps out of the Django project folder I could simply run all tests with: manage.py test However, now the tests aren't found. In addition to adding DIR/ to settings.py, I tried adding it to manage.py and it did not help. By reading the django docs I discovered I can specify a module like this: manage.py test app_name.tests The above works, but is impractical for many apps. How does one add a path for where to search for tests? I read this but it only describes the problem, not the solution: Django test runner not finding tests -
Django: Two models share a many-To-many relationship with one model
I have three models: Internal_Apps, Services and External_Apps.The relationship between Internal_Apps and Services is internal_app can have many services and services belong to many internal_apps.I have defined this relationship through the junction table.However, the External_Apps has same relationship with Services.Could I use the same junction table to define the relationship between External_Apps and Services or make a separate junction table for External_Apps and Services? What is the best practice for this situation? Thanks in advance. -
Post Request to Django Rest APIView lacking user (token authentication)
I'm creating API for Twitter like app in Django and since I implemented token authentication (rest-auth) I have problem with creating new tweets as it throws: { "author": [ "This field is required." ] } I've tried CreateAPIView: class TweetCreateAPIView(CreateAPIView): serializer_class = TweetModelSerializer permission_classes = (IsAuthenticated,) # I've also tried to add the author field mannualy def perform_create(self, serializer): serializer.save(author=self.request.user) but it didn't work so I created custom post method: class TweetCreateAPIView(APIView): permission_classes = (IsAuthenticated,) def post(self, request, format=None): serializer = TweetModelSerializer(data=request.data) if serializer.is_valid(): serializer.save(author=request.user) return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) but it still can't identify the user creating the tweet Model: class Tweet(models.Model): retweeted_by = models.ManyToManyField( TwitterUser, blank=True, related_name='retweets') commented_tweet = models.ManyToManyField( 'self', related_name='comments', blank=True, symmetrical=False) author = models.ForeignKey( TwitterUser, on_delete=models.CASCADE, related_name='tweets') content = models.CharField(max_length=280) created_at = models.DateTimeField(auto_now_add=True) liked_by = models.ManyToManyField( TwitterUser, blank=True, related_name='likes') objects = TweetManager() def __str__(self): return self.content def get_comments(self): comments = Tweet.objects.filter(commented_tweet__pk=self.pk) return comments def get_retweets(self): retweets = TwitterUser.retweets(tweet__id=self.id) def get_absolute_url(self): return reverse('tweets:pk', kwargs={'pk': self.pk}) Serializer: class TweetModelSerializer(serializers.ModelSerializer): likes_count = serializers.SerializerMethodField() already_liked = serializers.SerializerMethodField() def get_likes_count(self, tweet): return tweet.liked_by.all().count() def get_already_liked(self, tweet): user = None request = self.context.get("request") if request and hasattr(request, "user"): user = request.user if user is not None: if user in tweet.liked_by.all(): … -
What is the recommended way to remove fields from django-models without downtime?
My team and I work on a shared django repository on a product which requires 99.99+% uptime. I want to remove a few fields from a frequently used model. Say my model is defined as follows, and I want to remove field2. class MyModel(models.Model): field1 = ... field2 = ... Unfortunately this is accompanied by a migration, which if performed, will cause a downtime during deployments because the old servers still reference the non-existent fields in queries, like the ones below. new_model = MyModel() new_model.save() or MyModel.objects.all() I can't get away without creating a migration because I work on a shared repository, and django will force the new migration on the next developer to make changes to the repo after me. Is there a recommended way of removing fields from models in django ? -
Django Rest Framework - Bad request
I'm trying to call an api endpoint on my Django project from my frontend. The endpoint is at the url /tst/. I need to retrieve data from that endpoint, in order to populate my page with that data. I'm using an ajax request for this, but i keep getting the error 400 - BAD REQUEST, but i don't know why this happens, since the api endpoint is at the right URL. function doPoll(){ dataType: "json", $.post('http://localhost:8000/tst/', function(data) { console.log(data[0]); $('#data').text( data[0].data); setTimeout(doPoll, 10); }); } -
Django: nullable DateTimeField populates with the current timestamp?
In my model I've added a DatetimeField like this: created = models.DateTimeField(auto_now_add=True, null=True) However, after running the migration all existing rows appear to have created set to the current timestamp. I want all existing rows to have created = NULL upon migration. How to achieve this? -
How do i keep alive connection to socket server and receive callbacks when server send messages
USSD gateway require my application to connect via socket. This connection need to be established and maintained for bi-direction communication. I have come across socket library in python import socket HOST = '10.0.0.2' PORT = 9334 with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s: s.connect((HOST, PORT)) data = s.recv(1024) Above code successful connect to server. The issue is how do I keep the connection alive and read data from server as they are sent (may be with callbacks as in socketio). I have triend python socketio but the server is not compliant with socketoi From the documentation (USSD gateway server) Two way connector, peer – to – peer connection, both side must open socket to listen message from the other -
Django - How to write unit test checking if ValidationError was raised by ModelForm clean() for ManyToMany relationship?
I have ManyToMany field in my model and I wrote a custom validation for it in forms ModelForm created only in this purpose. The validation works fine, but I don't know how to write unit test for it correctly. #models.py class Course(models.Model): # some code max_number_of_students = models.PositiveSmallIntegerField( default=30) class ClassOccurrence(models.Model): course = models.ForeignKey(Course, on_delete=models.CASCADE) students = models.ManyToManyField(User, blank=True) # some code # forms.py class ClassOccurrenceForm(forms.ModelForm): # some code def clean(self): # Checks if number of students inscribed is not greater than allowed # for the course cleaned_data = super(ClassOccurrenceForm, self).clean() students = cleaned_data.get('students') course = cleaned_data.get('course') if students and course: if students.count() > course.max_number_of_students: raise ValidationError({ 'students': "Too many students!}) return cleaned_data # tests.py # some code def test_clean_number_of_students_smaller_than_max_students_number(self): self.course_0.max_number_of_students = 5 self.course_0.save() users = UserFactory.create_batch(10) self.assertRaises(ValidationError, self.class_occurrence_0.students.add(*users)) try: self.class_occurrence_0.students.add(*users) except ValidationError as e: self.assertEqual(e.errors, {'students': ["Too many students!"]}) Currently it doesn't work as intended. It seems in test method the ValidationError is not raised where it should be raised. Does anybody can help me to fix it? -
how to get nested relationship in the view
some fields return an empty result like here category_set is empty but I have a category for this what i have tried is here models.py class Category(models.Model): category = models.CharField(max_length=128) profile = models.ForeignKey(Profile, on_delete=models.CASCADE) def __str__(self): return self.category class Interest(models.Model): name= models.CharField(max_length=250, default='') category = models.ForeignKey(Category, on_delete=models.CASCADE) profile = models.ForeignKey(Profile, on_delete=models.CASCADE) def __str__(self): return self.name serializers.py class CategorySerializer(serializers.ModelSerializer): interest_set = InterestSerializer(many=True) class Meta: model = Category fields = ['id', 'category', 'interest_set'] class ProfileSerializer(serializers.ModelSerializer): category_set = CategorySerializer(many=True) class Meta: model = Profile fields = [] How can I get right content? any help? -
Assertion error while testing Django views
This is my testing function for views.py which I have mention below: def test_operation_page(self): url = reverse('operation') response = self.client.get(url) self.assertEqual(response.status_code, 200) self.assertTemplateUsed(response, 'abc.html') self.assertContains(response, '<b>BOOK id having certain title:</b>') This is the error I am having while testing my views AssertionError: Database queries to 'default' are not allowed in SimpleTestCase subclasses. Either subclass TestCase or TransactionTestCase to ensure proper test isolation or add 'default' to home.tests.TestViews.databases to silence this failure. This is my views.py def operation(request): queryset=Mytable.objects.filter(title="The Diary of Virginia Woolf Volume Five: 1936-1941").values('bookid') textset=list(Mytable.objects.order_by('-bookid').values('title')) context={ 'key1' : queryset, 'key2' : textset } return render(request,'abc.html',context) This is my urls.py urlpatterns = [ path('admin/', admin.site.urls), path('',v.index,name='index'), path('abc/',v.operation,name='operation') ] -
Import cycle with Model, ModelManager and ModelSerializer
I am using Django with Django Rest Framework for serializers. I have the following situation. In file models.py: from django.db.models import Manager, Model, CharField from .serializers import MyModelSerializer class MyModelManager(Manager): serializer_class = MyModelSerializer class MyModel(Model): name = CharField(max_length=64) In file serializers.py: from rest_framework.serializers import ModelSerializer from models import MyModel class MyModelSerializer(ModelSerializer): class Meta: model = MyModel fields = ('name',) However, this leads to an import cycle, since both files try to import each other. I could prevent this by making a local import: class MyModelManager(Manager): @property def serializer_class(self): from ow_articlecode.import_cycle_serializers import MyModelSerializer return MyModelSerializer However, this feels like a hack. What would be a proper solution to break this import cycle? -
DRF taking empty stuff in object
I have some problems with nested relationships in django rest framework my models.py class Category(models.Model): category = models.CharField(max_length=128) profile = models.ForeignKey(Profile, on_delete=models.CASCADE) def __str__(self): return self.category class Interest(models.Model): name= models.CharField(max_length=250, default='') category = models.ForeignKey(Category, on_delete=models.CASCADE) profile = models.ForeignKey(Profile, related_name='category_profiles', on_delete=models.CASCADE) def __str__(self): return self.name serializers.py class CategorySerializer(serializers.ModelSerializer): interest_set = InterestSerializer(many=True) class Meta: model = Category fields = ['id', 'category', 'interest_set'] class ProfileSerializer(serializers.ModelSerializer): category_set = CategorySerializer(many=True) class Meta: model = Profile fields = [] in the view when I get profile in category set is empty like here image but if i show in the browser separately it shows me right thing please see the image for now thats all what I have. How can I get proper category set in profile? Thank you beforehand! -
Problem in Deleting Model Entries having ForeignKey Constraint
class Client(models.Model): client_id = models.CharField(primary_key=True, max_length=255) name = models.CharField(max_length=255, blank=False) class Cont(models.Model): contid = models.CharField(max_length=255, primary_key=True) Client = models.ForeignKey(Client, on_delete=models.PROTECT) class ContractDailyIndent(models.Model): id = models.CharField(max_length=255, primary_key=True) cont = models.ForeignKey(Cont, on_delete=models.PROTECT) class VDLContract(models.Model): id = models.CharField(max_length=255, primary_key=True) contractindent = models.ForeignKey(ContractDailyIndent, on_delete=models.PROTECT) Getting error in this line VDLContract.objects.filter(contractindent__cont__Client__in=clients).delete() It's giving error: Traceback (most recent call last): File "/home/puranjay/Documents/FeasOpt/new/fo_ftl_puranjay/mysite/empanelment/views.py", line 10432, in update_client_type delete_client_type(user, client_type_id) File "/home/puranjay/Documents/FeasOpt/new/fo_ftl_puranjay/mysite/empanelment/views.py", line 105, in delete_client_type delete_indent_models(user, clients) File "/home/puranjay/Documents/FeasOpt/new/fo_ftl_puranjay/mysite/empanelment/utility.py", line 968, in delete_indent_models raise e File "/home/puranjay/Documents/FeasOpt/new/fo_ftl_puranjay/mysite/empanelment/utility.py", line 938, in delete_indent_models vdl_market_object.delete() File "/home/puranjay/Documents/FeasOpt/env/venv/lib/python3.6/site-packages/django/db/models/base.py", line 890, in delete collector.collect([self], keep_parents=keep_parents) File "/home/puranjay/Documents/FeasOpt/env/venv/lib/python3.6/site-packages/django/db/models/deletion.py", line 222, in collect field.remote_field.on_delete(self, field, sub_objs, self.using) TypeError: 'NoneType' object is not callable -
When refering to the object pk in the tableview i get a pk error but i still can refere to the object from the context
I have a django app where I want to show a table of user entries and users can delete/edit entries from the table by buttons. I used django-tables2 as the library to render the table. Tables.py class PatientTable(tables.Table): FirstName = tables.Column(linkify=("patients:patient_detail", {"pk": tables.A("pk")})) LastName = tables.Column(linkify=("patients:patient_detail", {"pk": tables.A("pk")})) Telephone_no = tables.Column(linkify=("patients:patient_detail", {"pk": tables.A("pk")})) delete = TemplateColumn('<button type ="button" class ="btn btn-danger" data-toggle="modal" data-target="#modalDelete" >Deleta</button>',extra_context={'patient': 'Patient'}) class Meta: model = Patient attrs = {'class': 'table table-striped table-hover'} exclude = ("user", "Notes", "Adress") template_name = 'django_tables2/bootstrap4.html' Views.py def Patients_list(request): patients = Patient.objects.all() table = PatientTable(patients.filter(user=request.user)) RequestConfig(request).configure(table) return render(request, 'patients/patients_list.html',{ 'table' : table, 'patients':patients, }) here in the views I defined the patients in the context to be callable in the template,It's callable but i can't call the patients.pk, it always return a value error. Template {% extends 'base.html' %} {% load render_table from django_tables2 %} {% block content %} <div id="content"> {% if user.is_authenticated %} <h1> Patients list: </h1> <br> <a href="{%url 'patients:patient_create'%}" class="btn btn-info" role="button">Add Patient</a> <br> <br> {% render_table table %} {% else %} <h2>please login</h2> {% endif %} </div> <div class="modal fade" id="modalDelete" tabindex="-1" role="dialog" aria-labelledby="modalDelete" aria-hidden="true"> <div class="modal-content"> <div class="modal-header"> <h5 class="modal-title">Delete patient!</h5> <button type="button" class="close" data-dismiss="modal" aria-label="Close"> … -
Why accessing to cache from django code and shell are different
I`m trying to clear my cache in save function of model A. But when i try to delete it cant find any data cache.keys('*') returns []. So cache can`t be cleared. But when i do same operations in manage.py shell everyting works. So the question is, what is the difference between using cache in shell and in django codes