Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Improve code: Displaying datetime of url when clicked in middleware
I have written a middleware and I would like some suggestions on how I could improve my code. I have a model that saves the date and the url when a new instance of it is created (in a middleware, so everytime a url is visited). Like so: Middelware: class GetUrlMiddleware(): def __init__(self, get_response): self.get_response = get_response def __call__(self, request): # before view urltime = UrlTime.objects.create(associated_url=request.path) urltime.save() response = self.get_response(request) # after view return response Model: class UrlTime(models.Model): url_track_model = models.ForeignKey(UrlTrack, blank=True, null=True, on_delete=models.CASCADE) associated_url = models.CharField(blank=True, null= True, max_length=250) timestamp = models.DateTimeField('Url viewed on: ',default=datetime.now, blank=True) Now, everytime I click a link, a new object is created with the url and the date. That all works fine. I have the feeling this could be done better. Is there for instance a way in which I could add a column every time I click the link with the url? Or maybe some other better way? Help is of course very much appreciated Thanks in advance! -
different serializers for POST and GET requests, is it the right way?
I want to have the following behavior: # GET # "id": 1, # "name": "test", # "created_date": "date", # "completed_date": "date", # "template": { "name" : "test" } => nested serializers with only the field "name" # "status": 1, # "results": [ { __all__ }, ... ] => nested serializers with all the fields # "groups": [ # { "name" }, ... => nested serializers with only the field "name" # ] # POST # "name": "test", # "template": {"name":"test"}, => nested serializers with only the field "name" # "groups": [ # {"name":"test"} => nested serializers with only the field "name" # ] As you can see the post request doesn't have all the information that get displays. I know that some of the fields can be removed with the read_only_fields variable. But the issue here is more related to the nested serializers. I have trouble validating the data... This issue is related to this issue: empty validated_data when dynamically modifying fields I was wondering if using dynamic serializers and with the combination of read_only_fields was a good maintainable and readable solution: https://www.django-rest-framework.org/api-guide/serializers/#dynamically-modifying-fields Or should I use the following solution: Use different serializer for request and reply Thanks again ! … -
Is there a way to generate XML file from HTML webpage using Django Framework?
The idea is making a website using Django that generates XML files from an "XML Template" with just few changes to it. For example the XML Template is: <exp> <type op="#" np="#" id="#"> <to> <round id="#"> </to> </type> </exp> the "#" value is to be inserted and being able to repeat "tags" from the web page to be like this: <exp> <type op="#" np="#" id="#"> <to> <round id="#"> </to> <to> <round id="#"> </to> </type> </exp> -
Is it possible to add an extra value (a calculation of 2 fields in the query) to a queryset that is returned from class based view
I am trying to add a value to a query that is returned from a class based view. My Db consists of users, a user can have many transactions, and a transaction can have many sales. I want to calculate the profit loss on a sale by means of ((sale.amount_per_coin - sale.transaction.amount_per_coin_sold) * sale.amount) and add it to each row that is returned. Is it possible to do this and if so how would I go about doing it. Transaction Model Below class Transaction(models.Model): currency = models.CharField(max_length=20) amount = models.IntegerField() total_price = models.DecimalField(max_digits=7, decimal_places=2) date_purchased = models.DateTimeField() note = models.TextField(default="") owner = models.ForeignKey(User, on_delete=models.CASCADE) amount_per_coin = models.DecimalField(max_digits=7, decimal_places=2, editable=False) def save(self, *args, **kwargs): self.amount_per_coin = self.total_price / self.amount super(Transaction, self).save(*args, **kwargs) Sale Model Below class Sale(models.Model): amount_sold = models.IntegerField() total_price_sold = models.DecimalField(max_digits=7, decimal_places=2) date_sold = models.DateTimeField(default=timezone.now) note = models.TextField(default="") transaction = models.ForeignKey(Transaction, on_delete=models.CASCADE) amount_per_coin_sold = models.DecimalField(max_digits=7, decimal_places=2, editable=False) def save(self, *args, **kwargs): self.amount_per_coin_sold = self.total_price_sold / self.amount_sold super(Sale, self).save(*args, **kwargs) class based view below class SaleListView(ListView): model = Sale template_name = 'webapp/sale.html' context_object_name = 'sales' paginate_by = 5 def get_queryset(self): return super().get_queryset().filter(transaction__owner=self.request.user) -
How to generate thumbnails in Django app with CKEditor
By default in the Django CKEditor no thumbnails are created and full size images are used as preview. Is there any way to create thumbnails when someone upload an image via CKEditor? I'm used image2 plugin for image processing. -
Django 2.1 - What does namespace= do on an include in urls.py
I pretty much have app_name and namespaces working well, but I have a simple clarification - and I think this might be something that is in my 2.1 Django that might have been different in earlier Django versions. I have an app named route and in its urls.py I have: app_name = 'route' urlpatterns = [ path('first', views.FirstView.as_view(), name='first-view'), ] In my views.py and template files I use route:first-view in my reverse() calls and {% url .. %} calls and it all works. My confusion is in my project wide urls.py where I say something like: urlpatterns = [ path('route/', include('route.urls', namespace='route')), ] The part that is making me crazy is that it appears that the namespace= parameter does absolutely nothing. My code works the same if I leave it out or even if I say namespace='abc' - it seems to be 100% ignored by Django 2.1. Also if I use namespace='route' without app_name being set, I get an error: Specifying a namespace in include() without providing an app_name is not supported. If namespace= is ignored - then I am happy just setting app_name - I will leave it off - It just seems like it must have a purpose … -
invalid literal for int() with base 10: 'RW' when using Django Filter with Wagtail
So I have been trying for several hours for this to work but I am stuck. I am trying to integrate filters on a Wagtail Page model (Which is basically a full Django model, more about that here: http://docs.wagtail.io/en/v2.4/topics/pages.html). Anyway I am getting this error. Seen a lot of posts on stack overflow about this error but none could solve my problem. The code: # Field position model class FieldPosition(models.Model): field_position = models.CharField(max_length=3, choices=FIELD_POSITION_CHOICES, null=True) level = models.CharField(max_length=2, choices=FIELD_POSITION_LEVEL, null=True) panels = [ FieldRowPanel( [ FieldPanel('field_position'), FieldPanel('level'), ] ), ] class Meta: unique_together = ('field_position', 'level') abstract = True # Field position model relationship to Player Detail Page class FieldPositionRelationship(Orderable, FieldPosition): page = ParentalKey('PlayerDetailPage', on_delete=models.CASCADE, related_name='field_position') # Player overview page class PlayerPage(Page): # Overriding the Wagtail page model to add extra variables. This is the Wagtail way as per the creator https://stackoverflow.com/questions/32626815/wagtail-views-extra-context def get_context(self, request): context = super(PlayerPage, self).get_context(request) context['filter'] = PlayerDetailPageFilter(request.GET, queryset=PlayerDetailPage.objects.all()) return context # Player detail page class PlayerDetailPage(Page): club = models.ForeignKey('Clubs', null=True, blank=True, on_delete=models.SET_NULL, related_name='+') agency = models.ForeignKey('Agency', null=True, blank=True, on_delete=models.SET_NULL, related_name='+') picture = models.ForeignKey('wagtailimages.Image', null=True, on_delete=models.SET_NULL, related_name='+', help_text=_('Player profile picture.')) foot = models.CharField(max_length=2, choices=SOCCER_FOOT, null=True, blank=True) content_panels = Page.content_panels + [ SnippetChooserPanel('club'), SnippetChooserPanel('agency'), ImageChooserPanel('picture'), FieldPanel('owner'), FieldPanel('foot'), … -
Unable to run my django&bootstrap site with Let's Encrypt on DO Ubuntu 18.04 with nginx and gunicorn
I am trying to launch a django website and I want to install and make sure https connection works with let's encrypt for my site. I followed DigitalOcean "How To Secure Nginx with Let's Encrypt on Ubuntu 18.04" tutorial on https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04. When my site works with http connection, after the installation of let's encrypt, neither http nor https connection works now. My nginx/sites-available/mysite.com file code is shown below: server { server_name mysite.com www.mysite.com; location = /favicon.ico { access_log off; log_not_found off; } location /static/ { root /root/project/app/; } location / { include proxy_params; proxy_pass http://unix:/run/gunicorn.sock; } listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/mysite.com/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/mysite.com/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } server { if ($host = www.mysite.com) { return 301 https://$host$request_uri; } # managed by Certbot if ($host = mysite.com) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; server_name mysite.com www.mysite.com; return 404; # managed by Certbot } Normally, my website with http works fine, yet when I follow the tutorial and try to connect my site, it does not load on browser. If I try with curl, I get … -
how can i make relation between two fields with different django classes?
i have two models "Club" class and "Match" class and it has foreign key to Club but i can not increment "won" field (or draw or lost) in "Club" class by "score_local" and "score_visitor" in "Match" class .. how can i do this class Club(models.Model): league_names = models.ForeignKey(League, on_delete= models.CASCADE, related_name='club') name = models.CharField(max_length=100) logo = models.ImageField(upload_to='media/core', max_length=255, null=True, blank=True) year_of_establishment = models.IntegerField(default=1900) won = models.IntegerField(default=0) draw = models.IntegerField(default=0) lost = models.IntegerField(default=0) def CalcPoints(self): return self.won*3 + self.draw total_points = property(CalcPoints) class Match(models.Model): play_date = models.DateTimeField('play date') occasion = models.ForeignKey(League, on_delete=models.CASCADE, related_name='match') club_visitor = models.ForeignKey(Club, on_delete=models.CASCADE, related_name='match_club_visitor') club_local = models.ForeignKey(Club, on_delete=models.CASCADE, related_name='match_club_local') score_visitor = models.IntegerField() score_local = models.IntegerField() -
Changing MySQL isolation level on Django/Elastic beanstalk/RDS
I'm trying to change the isolation level of my MySQL instance on RDS through a Django application. I've followed the recommendation here So, I changed my Django db settings to: 'OPTIONS': { 'init_command': 'SET default_storage_engine=INNODB; SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED' } This command works fine when I run directly in the database (MySQL version 8.0.13) However, when I try to deploy it to Elastic Beanstalk I got the following error: Command failed on instance. Return code: 1 Output: (TRUNCATED)... manual that corresponds to your MySQL server version for the right syntax to use near 'SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED' at line 1") I also tried changing the line to: SET default_storage_engine=INNODB, SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED With the same results Any idea what might be wrong? Or another solution that I can use (maybe I can just run this command and set it to global isolation level instead?) -
Django not serving common static files
I've made a static folder in the root path of my project as it contains basic css/js which aren't tied to a specific app. Project setup: main static css styles.css accounts settings.py STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles') STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(PROJECT_ROOT, 'static/'), ] I have also installed django.contrib.staticfiles. I have included files like this <link rel="stylesheet" href="{% static 'css/styles.css' %}"> But it doesn't seem to work and results in 404 error. Thank you for any help. -
Updating nested PrimaryKeyRelatedField in Django Rest Framework
I'm trying to create an update function for one of my serializers which represents a Company where some people (user of my app) are admin. However when I make my request with Postman to update my Company object, it seems that my serializer does not get all the data i PUT in my API. I have tried to use the full user object serializer in my Company serializer but in this case i must specify data I don't need and it does not even work. When i PUT the name of my company like so: { "id": 1, "admins": [ 5, 6, 7, 9, 11 ], "name": "Mon entreprise" } My api responds this (the users with IDs 6 and 11 were added using the admin panel): { "name": "Mon entreprise", "logo": "http://localhost:8000/api/companies/1/mwe.jpg", "admins": [ 6, 11 ] } I tried this to see what data were passed through the API def update(self, instance, validated_data): print (validated_data) and the output was: {'name': "Mon entreprise"} My serializer: class CompanySerializer(serializers.ModelSerializer): admins = serializers.PrimaryKeyRelatedField(many=True, read_only=True) class Meta: model = Company fields = ('name', 'logo', 'admins',) def update(self, instance, validated_data): print (validated_data) instance.logo = validated_data.get('logo', instance.logo) instance.name = validated_data.get('name', instance.name) instance.save() admin_list = validated_data.get('admins') … -
building highcarts options in views.py
In my application i'm building the whole chart options in the backend and returning it as a json response def get_chart_data(request): chart = { 'title': { 'text': '' }, 'xAxis': { 'categories': [], 'title': { 'text': '' }, 'type': 'category', 'crosshair': True }, 'yAxis': [{ 'allowDecimals': False, 'min': 0, 'title': { 'text': '' } }, { 'allowDecimals': False, 'min': 0, 'title': { 'text': '' }, 'opposite': True }], 'series': [{ 'type': 'column', 'yAxis': 1, 'name': '', 'data': [] }, { 'type': 'line', 'name': '', 'data': [] }, { 'type': 'line', 'name': '', 'data': [] }] } return JsonResponse(chart) And then get the data using ajax and use the response for the data Highcharts.chart('dashboard1', data); I'm ok with this so far but i've run into problems if I want to use highcharts functions as part of the options, for example setting the color of text using Highcharts.getOptions().colors[0], 'title': { 'text': 'Rainfall', 'style': { 'color': Highcharts.getOptions().colors[0] } }, If i don't put quotes to this when building the options in views.py it would be treated as python code and result in an error, however if i add quotes to it, it will be treated as string in javascript which would not work. … -
test fails : matching query does not exist
I've spent the whole day trying to find out where this error might come from, but in vain. My test function is the following: def test_match_data_while_updating(self): # TST N.1 : Match is live # ------- match_updated_data1 = { 'match': { 'id': 1, 'status': 'not_started', }, } match1 = Match.objects.get(id=1) request = self.__class__.factory.put('', match_updated_data1, format='json') add_authentication_to_request(request, is_staff=True) response = update_match_video(request) self.assertEqual(Match.objects.get(id=1).status,'live') And this the relevant part of the function I am testing: def update_match_video(request): if not request.data.get('match').get('id'): return JsonResponse({}, status=status.HTTP_400_BAD_REQUEST) match_id = valid_data_or_error(request.data, method='PUT')['match_data']['id'] try: match = Match.objects.get(id = match_id) db_match_status = match.status if db_match_status == 'live': valid_data_or_error(request.data, method='PUT')['match_data']['status'] = 'live' else: if db_match_status == 'closed': valid_data_or_error(request.data, method='PUT')['match_data']['status'] = 'closed' except : print('Match does not exist') When I use the command: $ python server/manage.py test --settings=skill.settings_test api.views.tests.test_views.ViewsTestCase to run all the test functions that the file /views/tests/test_views contain (this file contains only one class which is ViewsTestCase with many tests) all the tests succeed, but when I run all my tests that are located in folder api using: $ python server/manage.py test --settings=skillcorner.settings_test api.views I get the following error: ERROR: test_match_data_while_updating (api.views.tests.test_views.ViewsTestCase) Traceback (most recent call last): File "/home/yosra/Bureau/app/master/server/api/views/tests/test_views.py", line 226, in test_match_data_while_updating match1 = Match.objects.get(id=1) File "/home/yosra/Bureau/app/master/venv/lib/python3.6/site-packages/django/db/models/manager.py", line 82, in … -
Django: Get number of 25th of every month since certain date
Hi in my django project I've got the following model and I want to make an @property function that counts how many times since the date_created there was the 25th of a month. For Example: I created the model on the 1.1.2019 The function should now return 3 because there were 3 25th since than. class Plan(models.Model): title = models.CharField(max_length=50) date_created = models.DateTimeField(default=timezone.now) plan_type = models.IntegerField() owner = models.ForeignKey(User, on_delete=models.CASCADE) Thanks for any help <3 -
Django how to fetch API properly
i try to fetch the results of coinmarketcap.com prices, ranking etc but im running into serveral issues on coinmarketcap.com -> "https://api.coinmarketcap.com/v1/ticker/" while the ranking is changing from time to time my workflow does not exactly saves the 100 results you get trough the api it also keeps old records and counts above id == 100 so i have more then 100 results. My question now is how i can solve this behaviour and just save exactly the 100 results i get from the API? tasks.py (celery): def get_exchange_rate(): api_url = "https://api.coinmarketcap.com/v1/ticker/" try: exchange_rates = requests.get(api_url).json() for exchange_rate in exchange_rates: CryptoPrices.objects.update_or_create( key=exchange_rate['id'], symbol=exchange_rate['symbol'], defaults={ "market_cap_usd": round(float(exchange_rate['market_cap_usd']), 3), "volume_usd_24h": round(float(exchange_rate['24h_volume_usd']), 3), "value": round(float(exchange_rate['price_usd']), 3), }) logger.info("Crypto exchange rate(s) updated successfully.") except Exception as e: print(e) models.py class CryptoPrices(models.Model): key = models.CharField(max_length=255, unique=True) value = models.CharField(max_length=255) symbol = models.CharField(max_length=255) volume_usd_24h = models.CharField(max_length=255) market_cap_usd = models.CharField(max_length=255) views.py def crypto_ticker(request): list_prices = CryptoPrices.objects.get_queryset().order_by('pk') paginator = Paginator(list_prices, 25) page = request.GET.get('page') price = paginator.get_page(page) return render(request, 'crypto_ticker.html', {'price': price}) -
How to safely encode the content disposition HTTP header with Python?
response['Content-Disposition'] = 'attachment; filename=%s' % filename In this Django response, the header causes the intended download but commas in the filename makes Chromium throw a low-level error and spaces cause the filename to change on download. How do I encode filename so the downloaded file gets the same filename? Converting filename to slug seems to solve the issue, but I want to download with the original filename. -
How do i setup routing for my API and my websocket
I'm making a django/react webapplication and would like to be able to use both a rest api and a websocket. to set the stage a bit: I have a authentication app and a project app and some other apps. i want my authentication app to use the rest framework to process login/register requests, after this i want to be able to use the project apps websocket in order to keep things flowing smoothly. i have however no idea how to set this up. Backend (main project app with settings etc) Authentication (small app used for authenticating users)(only really needs rest) Project (bigger app that uses websockets to do stuff as it needs to be updated constantly) i hope somebody can provide some details or informational links that would point me in the correct direction.or perhaps some code examples -
Django Admin view table subsets
I am trying to alter the default django admin pages on my website. Currently, when I open the admin page I see each of the models I created for my app. And when I click through these I can also access each row of the tables they relate to. However, I would prefer to view the tables in their entirety, since it's easier to review the data this way. How can I achieve this? Should I be creating a specific model for this? Example: If the database table is in this format: group term 1 a 1 b 1 c 2 d 2 e 2 f Currently I can only alter one row at a time ie. group: 1 term: a But I would rather view the table as it was shown first. Ultimately I would also like to view each group independently - but I am moving one step at a time. -
Django prefetch nested through table
I'm trying to prefetch order lines grouped by product on a customer queryset. Is it possible to prefetch a through table using Django ORM or should I be going about this in a different way? class Customer(models.Model): ... class Order(models.Model): customer = models.ForeignKey(Customer, related_name='%(class)s_customer') lines = models.ManyToManyField( Product, through='OrderLine' through_fields=('order', 'product') ) ... class OrderLine(model.Model): product = models.ForeignKey(Product, related_name='%(class)s_product') order = models.ForeignKey(Order, related_name='%(class)s_order') The following does not work because customer isn't a field on the OrderLine model customers = Customer.objects.all().prefetch_related( Prefetch( 'order_customer', queryset=OrderLine.objects.filter(**query), to_attr='order_lines' ) ) -
How to execute external script in views.py in django application?
Following is my django file structure myproject myapp migrations static templates myapp demo.html urls.py views.py models.py admin.py apps.py tests.py Scripts demo_script.py manage.py What I want to know is how to execute the script Scripts/demo_script.py (which is outside the application "myapp") from views.py and if it is executed successfully, show "Success" on myapp/templates/myapp/demo.html or else show "Failed to execute" on the same html page. Thanks in advance! -
Create a chatbot for a web application with react js, django and dialogflow
I'm setting up a web application using django and react js and I want to integrate a chatbot in my application, wich I've created using dialoglow. I dont know how to proceed to integrate dilogflow between my server and the client side, Any recommendations? -
How do mysql work with django, when you upload your project to a hosting service?
I am having trouble understanding how something works with mysql and django. Im trying to create a project and connect it to mysql. By the way, im completely new to DJANGO and MYSQL. I know you have to get xampp to get into the localhost/phpmyadmin... I know you have to change the database in your django project settings. i know you have to install mysqlclient with pip.. i have did all of that and dont get me wrong, it works perfect. i understand how it connects to localhost/phpmyadmin.. uses the username and password i passed in to get the data --- ****i think???*** BUT my question is this.. when i put my project on a hosting service, like pythonanywhere......how would my django project know how to get the mysql at my localhost?????? Im confused at this point and i havent been able to publish my website. The code works perfectly in localhost of course, but what happens when i publish it on a hosting service? -
Django rest - convert field into a list?
I have a Textfield in my model which stores dictionaries, I would like to convert this field into a dictionary if possible in a rest serializer. at the moment the field returns the dictionary but backslashes all the quotes, it is possible to convert the strings into a nested list of dicts? Thanks api currently returns below: { "id": 3, "hostname": "WAN-EDGE", "timestamp": "2019-04-12T11:34:36.654521", "routing_table": "[{\"route\": \"0.0.0.0\", \"subnet_mask\": \"0.0.0.0\", \"next_hop\": \"172.16.66.193\"}, {\"route\": \"10.10.21.0\", \"subnet_mask\": \"255.255.255.0\", \"next_hop\": \"172.16.67.146\"}, {\"route\": \"10.22.0.0\", \"subnet_mask\": \"255.255.0.0\", \"next_hop\": \"172.18.1.5\"}, {\"route\": \"10.31.0.0\", \"subnet_mask\": \"255.255.0.0\", \"next_hop\": \"172.16.67.146\"},...]" },... } desired result a nested list of dicts { "id": 3, "hostname": "WAN-EDGE", "timestamp": "2019-04-12T11:34:36.654521", "routing_table": [ { "route": "0.0.0.0", "subnet_mask": "0.0.0.0", "next_hop": "172.16.66.193" }, { "route": "10.10.21.0", "subnet_mask": "255.255.255.0", "next_hop": "172.16.67.146" }, { "route": "10.22.0.0", "subnet_mask": "255.255.0.0", "next_hop": "172.18.1.5" }, { "route": "10.31.0.0", "subnet_mask": "255.255.0.0", "next_hop": "172.16.67.146" },... ] },... } -
Annotate reverse related object via Django QuerySet
The situation Suppose I have the followings models: class Garage(models.Model): width = ... height = ... class Car(models.Model): garage = models.ForeignKey('Garage', on_delete=models.CASCADE) brand = ... Now suppose I have to find all garages with width > 30 and containing Toyota cars. Then I may do a simple query like this: results = Garage.objects.filter(width__gt=30, car__brand__icontains='Toyota') The problem Showing my results grouped by Garage I need to highlight the Toyota cars, like this: GARAGE A Volvo C30 Toyota Prius <---highlight this Saab 93 GARAGE B Toyota Yaris <---highlight this GARAGE C Fiat 500 Toyota C-HR <---highlight this Django annotate() appears not to be working with related fields. Any smart solution? N.B. I need to do the query on Garage model (not on Car).