Posts

Showing posts with the label django

Python/Django: Populating Model Form Fields with values from Imported JSON file

Python/Django: Populating Model Form Fields with values from Imported JSON file I have a model form that saves all form field inputs to the backend database as one entry. I also have a JSON file that contains multiple JSON objects whose fields corresponds to the model form field. This JSON file is being uploaded via FileField in the model. Ultimately, I want to be able to upload a JSON file with the multiple JSON objects into my model form and populate the fields with the corresponding values from the uploaded JSON file. Each JSON object will be a single entry to my database and they can have null values for at least one field. Ideally, I would like to be able to choose which JSON object (from the uploaded JSON file) gets loaded to my model form fields to eventually be saved in my database. How would I go about implementing this? docs.python.org/3/tutorial/… – Ignacio Vazquez-Abrams Jun 29 at 23:50 ...

Django2.0; still cannot get how redirect works

Django2.0; still cannot get how redirect works I'm new to Django and I still don't get how redirect works. For now, I use this way to redirect. return HttpResponseRedirect(reverse_lazy('main:index')) and this way works. And now I'm creating another page, and what I want to do is to redirect to the same page after I submit the form data. view.py is like this def add_comment(request, pk): entry = Entry.objects.get(id=pk) if request.method != 'POST': form = CommentForm() else: form = CommentForm(request.POST) if form.is_valid(): new_comment = form.save(commit=False) new_comment.user = request.user new_comment.save() return redirect('add_comment', pk=entry.id) return render(request, 'main/add_comment.html', {'form': form, 'entry': entry, 'comments': comments}) urls.py is like this path('add_comment/<int:pk>', views.add_com...

star ratings not working with turbolinks django

star ratings not working with turbolinks django I just finished implementing star ratings into my django app and turbolinks is preventing the ability to rate posts. It just clicks out to a dead link - example: ratings/17/24/?return=/post/post.id/ When I refresh the post page it works just fine. I followed instructions from jquery turbolinks repo but still having the same issues. Any tips would be appreciated. By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Heroku caching api responses [django/restframework]

Heroku caching api responses [django/restframework] Heroku seams to be caching api responses from my Django/RestFramework app. I have no caching mechanism enabled on restframework. Looking at the logs, I can see a error response (400) caused by a object not found, and, right before, the python stacktrace. Then I populate the object table and here happens the issue. On next requests, que error doesn't happen anymore, but the response is exactly the same 400 as before! Eventually, I get alternate 200/400 responses. Edit 1: When I get 400 response, it's not just the code, but also the body. My code throws no error, but the response is code 400 and the first error message. Edit 2: Added bellow my view and my serializer, with not relevant parts ommited The log with error: 2018-06-29T11:34:45.991094+00:00 app[web.1]: INFO 2018-06-29 08:34:45,990 dados: 1 [1/1] 6.34 Kb AGUARDANDO: importação síncrona iniciada 2018-06-29T11:34:46.044547+00:00 app[web.1]: UF Destino AC 2018-06-29T11:3...

How to call Python functions from JavaScript in Django?

How to call Python functions from JavaScript in Django? I've been struggling on this for a couple days. I've read through many posts, blogs, and watch video demo on Django projects, but nothing quite answers my problem. I have a Django webapp that uses a Python API to connects to a third-party service to analyze text data. The user writes into a text area, presses the button, then the text is sent to the service which returns a list of JSON objects. The API call to this service is a Python function. I'm trying to use Ajax to make an async call to the Python function using the text data as a param when the button is pressed. I'm not at this time trying to invoke my third-party service, yet. I'm just trying to see if I can get a response from Python sending input and receiving output. Most of the examples I've found involve creating a form and submitting it so the Django framework automatically calls it's Python functions. A form is not exactly what I'm tr...

Is it possible to persist a joined field in Djangos SearchVectorField?

Is it possible to persist a joined field in Djangos SearchVectorField? Is it possible to persist a joined field with Djangos SearchVectorField for full text search? For example: class P(models.Model): brand = models.ForeignKey(Brand, on_delete=models.CASCADE) search_vector = SearchVectorField(null=True, blank=True) code: p = P.objects.get(id=1) p.search_vector = SearchVector('brand__name') p.save() raises this exception: FieldError: Joined field references are not permitted in this query If this is not possible how can you increase the performance of joined annotated queries? 1 Answer 1 I found a workaround to your issue: p = P.objects.annotate(brand_name=SearchVector('brand__name')).get(id=1) p.search_vector = p.brand_name p.save() Update 2018-06-29 As reported in official documentation: If you’re just updating a record and don’t need to do anything with the model object, th...

Django model to admin, integer fields and textfield are not visible in admin section

Image
Django model to admin, integer fields and textfield are not visible in admin section class Student(models.Model): first_name = models.CharField(max_length=30) last_name = models.CharField(max_length=30) father_name = models.CharField(max_length=60) age = models.SmallIntegerField created = models.DateTimeField(auto_now=False, auto_now_add=True) modified = models.DateTimeField(auto_now=True, auto_now_add=False) address = models.TextField email = models.EmailField(default="") id = models.AutoField std = models.SmallIntegerField remarks = models.TextField I have created a model called Student and added it to Django admin and migrated it. There was no problem with migration. When I tried to add a new student via admin page, I am only getting following fields, How can I get integer fields like age in UI? It should be age = models.SmallIntegerField() . Note () signs. Just add it for all fields and run makemigr...

Django Error while trying to migrate model on Postgre DB

Django Error while trying to migrate model on Postgre DB I'm still learning to use Django and I have an issue while trying to create a model. Here's my models.py class Identifiants(models.Model): id = models.AutoField(primary_key=True) taxon = models.IntegerField(unique=True) noms = models.TextField(blank=True, null=True) fiche = models.IntegerField(blank=True, null=True) comestible = models.TextField(blank=True, null=True) sms = models.NullBooleanField() a_imprimer = models.NullBooleanField() lieu = models.TextField(blank=True, null=True) apparition = models.TextField() class Meta: managed = True db_table = 'identifiants' The makemigrations command works with no issues but the migrate one is where I have problems Running migrations: Applying app.0001_initial...Traceback (most recent call last): File "/usr/lib/python3.5/site-packages/django/db/backends/utils.py", line 83, in _execute return self.cursor.execute(sql) psycopg2.DataError: NUMERIC precision...

Convert Html to PDF Python/Django on Unix Platform

Convert Html to PDF Python/Django on Unix Platform I’m working on a functionality where I need to convert a huge html file (size more than 1 mb) into pdf. I’ve tried below two open-source python libraries. 1. Xhtml2pdf (Pisa) 2. Weasyprint But none of them solves my problem as they take around 4-5 mins in generating 1 MB PDF file (around 500 pages) causing my app server’s worker process (Gunicorn and Nginx) to get down and throwing ‘GATEWAY TIMEOUT ERROR’ on browser. CPU utilization also goes up to 100% while PDF conversion is in process. If anybody is having any idea which API/library will be a best suit for large html files. Possible duplicate of Render HTML to PDF in Django site – Ajay Singh Jun 29 at 10:49 I've already used pisa but it takes around 5 minutes in conversion – Sachin Ch...

Get data of another child in a template

Image
Get data of another child in a template I'm making a Board which can write comments. All functions work well. But the only thing I need is displaying profile of the author of the comment. {{ board.title }} {{ board.text }} {% for comment is context.commentmodel_set.all %} {{ comment.comment }} {{ comment.whose.profile??? }} <- ???? {% endfor %} ////////////// EDIT [Add Models] /////////// # Appname : board / model class Board(models.Model): whose = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) title = models.CharField() text = models.TextField() created_at = models.DateTimeField(auto_now_add=True) updated_at = models.DateTimeField(auto_now=True) class Comment(models.Model): post = models.ForeignKey(Board, on_delete=models.CASCADE) whose = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE) comment = models.TextField() created_at = models.DateTimeField(auto_now_add=True) updated_at = models....

Django project with core code stored in an app with the same name

Django project with core code stored in an app with the same name When creating a Django project (e.g. myapp ), the framework also creates a subdirectory/python package with the same name as the project to hold settings.py , urls.py and wsgi.py : myapp settings.py urls.py wsgi.py ─ myapp └─ myapp ├─ __init__.py ├─ settings.py ├─ urls.py └─ wsgi.py Although I then add a number of apps, I almost always have some core functionality that belongs to the project itself. Is there any reason why I should not add the core functionality to the myapp package alongside settings.py and wsgi.py and include this in settings.INSTALLED_APPS ? E.g.: myapp settings.py wsgi.py settings.INSTALLED_APPS ─ myapp ├─ app1 ├─ app2 └─ myapp ├─ __init__.py ├─ models.py ├─ settings.py ├─ templates | └─ homepage.html ├─ urls.py ├─ views.py └─ wsgi.py I've seen other people create a core app instead to hold the 'core' code that is s...

How to configure OpenEdx LMS in Django?

How to configure OpenEdx LMS in Django? I need to develop a LMS in Django with OpenEdx, but I don't know how to configure OpenEdx with Django. Can anyone please help me to do this? What kind of help do you need? Installation of all Open edX platform? – Rizky Ariestiyansyah Jun 17 '16 at 14:14 By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.