Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
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 -
Display number of wrong attempts of user
In my django app i want to show that the user has tried logging in with wrong password three time. I am using django-brutebuster I can see a table in my postgres on migrate named BruteBuster_failedattempt This captures: 1.id 2.username 3.IP 4.failures 5.timestamp **Setting.py:** Installed Apps: BruteBuster Middleware BruteBuster.middleware.RequestMiddleware', Block threshhold BB_MAX_FAILURES = 3 BB_BLOCK_INTERVAL = 3 I want to display the count of failed attempts on my django template. -
How could I send post on selected date without pressing any button
Hey I would like to send a date from selected date automatically, for instance when it is july 2019 and I will select August it will send post to the form and refresh page. I am using DatePickerInput(format="%m-%Y") so django create form in template for me. Do you have any ideas how could I do it ? I didn't find any solution. -
Upload any DateTime from CSV in Django
I am uploading CSVs in a django project but it shows error from laptop to laptop. Models.py time = models.DateTimeField(max_length=100, blank=True, default=datetime.date.today) views.py csv_file = request.FILES['file'] data_set = csv_file.read().decode('UTF-8') io_string = io.StringIO(data_set) next(io_string) uploaded_by = request.user for column in csv.reader(io_string, delimiter=',', quotechar='|'): _, created = ItemBatch.objects.update_or_create(name=column[0], pid=column[1], quantity=column[2],date=column[8]) The problem is that it takes only this format : YYYY-MM-DD HH:MM I updated settings.py with this: DATE_INPUT_FORMATS = ['%d-%m-%Y','%Y-%m-%d','%d/%m/%Y','%m/%d/%Y' ] What changes do I need to make such that it accepts every datetime format? -
PATCH Method not allowed
I tried to add a Patch method to my API in Django and I'm always ending with a "Method not allowed". I added mixins.UpdateModelMixin as mention in the Django Rest Framework documentation, however, it still returns the same error. I look and don't find where I need to put authorization for Patch to be allowed. this is the code related to that view and path declaration in urls.py and views.py. urls.py ''' schema_view = get_schema_view( openapi.Info( title="WAF Management Portal API", default_version="v1", description="REST api for interaction between Frontend and Backend.", contact=openapi.Contact(email="soc-dev-automation@bell.ca"), ), public=True, permission_classes=(permissions.AllowAny,), ) path( 'action/dothis/', ActionApiView.as_view(), name="action_api_view" ), ''' views.py ''' class ActionApiView(mixins.UpdateModelMixin, ActionAPIView): """ post: add one or more settings to selected policy patch: modify or more settings to selected policy """ def get_queryset(self): return Policy.objects.allowed_to_user(self.request.user) def get_serializer(self, *args, **kwargs): return SettingsSerializer(*args, **kwargs) @swagger_auto_schema() def post(self, request): queryset = self.filter_queryset(self.get_queryset()) serializer = self.get_serializer(data=request.data) if serializer.is_valid(): selected_policies = serializer.get_selected_policies(queryset) .....do some data manipulation (included action_id variable)... response = { ....prepare response } return redirect("another_view", action_id=action_id) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) @swagger_auto_schema() def patch(self, request): queryset = self.filter_queryset(self.get_queryset()) serializer = self.get_serializer(data=request.data) if serializer.is_valid(): selected_policies = serializer.get_selected_policies(queryset) .....do some data manipulation (included action_id variable)... response = { ....prepare response } return redirect("another_view", action_id=action_id) … -
How to pass arguments to a Python script from JavaScript
I'm need to run python script(s) based on user decisions in frontend. These scripts has to use these user decisions to "complete" the script before it runs. When the script has run it should return the ouput back to the frontend. I'm very new to JavaScript and only know Python basics. The script that is going to run are already created. I only need to edit some of the code so that it doesn't need to be configured from the code it self, but from a website instead. The scripts needs three arguments that should be provided from the frontend: a personal token a project name an array of test plan id's After the user has provided these three inputs, user can push a button which will be inserted into a python script in the correct place and the script should execute. When the output is ready, the script should return the ouput back to the frondend. Some of my HTML: <h4>Token</h4> <p>Insert token:</p> <form id="token_input_form"> <input type="text" name="token_input" size="52" required /> </form> Some of the script: personal_access_token = '<the token>' project = '<project name>' test_plans = [<one or more id(s) to test plan(s)>] -
DRF annotate nested serializer
I have a nested serializer initialized with many=True and would like to add a number of annotated fields to the output using SerializerMethodField(). How can I annotate the OrderLineSerializer queryset? class OrderSerializer(serializers.ModelSerializer): class Meta: model = Order fields = ( 'id' 'lines' ) def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.fields['lines'] = OrderLineSerializer( context=self.context, many=True, write_only=True, ) class OrderAPIViewSet(viewsets.ModelViewSet): queryset = Order.objects.all() serializer_class = OrderSerializer -
django: how to allow post request only from forms?
I have a simple form: {% block content %} <p> Upload invoices </p> <form method="post" action="{% url 'upload_thing ' %}" enctype="multipart/form-data"> {% csrf_token %} <input type="file" name="invoice_thing"> <button type="submit">Upload</button> </form> I have a view @require_POST @transaction.atomic def upload_thing(request): .... How do I make sure that the no one can hit the post endpoint vs curl or postman? I want the end point to be accessible only by hitting the form button. How do I accomplish this?