Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
Django + Celery 4 wrong settings
I'm using Django 1.10 and Celery 4. I found leaks in Celery's Documentation :( The worker configuration is well done and It run fine (I can see the worker connected in RabbitMQ webmin). But my tasks can't connect to RabbitMQ to publish their messages. settings.py CELERY_BROKER_URL = 'amqp://dev:dev@localhost/dev_virtualhost' CELERY_ACCEPT_CONTENT = ['json'] CELERY_TASK_SERIALIZER = 'json' CELERY_RESULT_SERIALIZER = 'json' celery.py from __future__ import absolute_import import os from celery import Celery os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings') app = Celery('dev_app') app.config_from_object('django.conf:settings', namespace='CELERY') app.autodiscover_tasks() @app.task(bind=True) def debug_task(self): print('Request: {0!r}'.format(self.request)) tasks.py from celery import shared_task @shared_task(queue='data_to_write') def test_task(data): open('/tmp/test', 'w').write(data) From Django's Shell I run this code and fail :( test_task.delay('hello world') AccessRefused: (0, 0): (403) ACCESS_REFUSED - Login was refused using authentication mechanism AMQPLAIN. For details see the broker logfile. In the RabbitMQ logs I see that credentials are guest:guest, no dev:dev like I wrote in the settings.py. Where is my error? Thanks -
OpenFace with Django
I'm currently working on a face recognition feature for our site and I've decided to use OpenFace- https://cmusatyalab.github.io/openface/ It's quite fast and quite accurate. I've tried this out with Django copying and pasting the required files and folders to make it work. *Screenshot 1 After that I edited my views a bit. The basic steps to doing this is: Aligning the faces Training the faces Generate an "Embeddings Files" Train the Embeddings Finally, predicting the image. First I took all the photos (specifically the urls) to feed it to the api so that I can align the faces: def align_faces(inputDir, outputDir): os.system("./util/align-dlib.py"+ " " + inputDir + " " + "align outerEyesAndNose"+ " " + outputDir + " " +"--size 96") After that I then generate the embeddings files: def generate_embeddings_files(path, company): os.system("./batch-represent/main.lua -outDir"+" "+path+" "+"-data ./aligned-images/"+company+"/") And then train the embeddings file: def train_embeddings(company): os.system("./demos/classifier.py train ./generated-embeddings/"+company+"/") Then I predict the faces: def predict_face(company, image_path): conf = subprocess.check_output(["./demos/classifier.py", "infer", "./generated-embeddings/"+company+"/"+"classifier.pkl", image_path]) return conf Ofc, taking the urls from the database I did some queries and then feeding the results of the queries to the functions above I was able to accomplish face recognition in my local server. However, here's … -
Django MPTT queryset for instances with children with a certain attribute
I'm using Django MPTT. If I want to select all Foobars with children, I can do this: [x for x in Foobar.objects.all() if x.get_children().exists()] If I want to select all Foobars that have children with a certain attribute (like published), I can do this: [x for x in Foobar.objects.all() if x.get_children().filter(published=True).exists()] I can't work out a way to do this in one query, though. I need to do it in one query in order to be able to use it for the limit_choices_to argument of ForeignKey: class Example(models.Model): related_foobar = models.ForeignKey( Foobar, limit_choices_to=Q(....), # How do I make this work? ) -
Testing api created in DRF, behind oauth
I'm trying to run the below test case class: class MealsCRUDTestCase(TestCase): def setUp(self): self.user = User.objects.create_user(username='test', email='test@test.pl', password='test', is_active=True) self.restaurant = Restaurant.objects.create(owner=self.user, phone='123', name='Test Restaurant') self.factory = APIRequestFactory() self.view = MealsCrudView.as_view() self.application = Application( name='fullybelly tests', redirect_uris="http://localhost", user=self.user, client_type=Application.CLIENT_PUBLIC, authorization_grant_type=Application.GRANT_PASSWORD, ) self.application.save() self.token = AccessToken.objects.create( user=self.user, token='1234567890', application=self.application, scope='read write groups', expires=timezone.now() + timedelta(days=10) ) def tearDown(self): self.application.delete() self.user.delete() self.restaurant.delete() def test_given_one_meal_should_save_one_meal(self): request = self.factory.post(reverse('edit_meals'), [ { 'restaurant': self.restaurant.pk, 'collection_from': '2016-12-23 10:00', 'collection_to': '2016-12-23 20:00', 'discount_price': '1', 'normal_price': '20', 'available_count': '9', 'name': 'foods' } ], format='json') force_authenticate(request, user=self.user) response = self.view(request) print(response) Unfortunatelly the last print statement gives me <Response status_code=403, "text/html; charset=utf-8">. Any suggestions what am I doing wrong here? -
How do i increase the number of variations in a dango product model
I have a product variation as below. It works but only allows me to add an additional 10 variations. I need to add 25 variations of the product. How can I can it so that it allows me to add more than 10 variations in the model? class Variation(models.Model): product = models.ForeignKey(Product) title = models.CharField(max_length=120) price = models.DecimalField(decimal_places=2, max_digits=20) sale_price = models.DecimalField(decimal_places=2, max_digits=20, null=True, blank=True) active = models.BooleanField(default=True) inventory = models.IntegerField(null=True, blank=True) #refer none == unlimited amount def __unicode__(self): return self.title -
How to get a queryset estimated size on the DB?
I'm running a cleanup process each month that deletes old unnecessary objects from the database. Is there a way to get an estimation of the file size gained from the cleanup process? I'm working with both sqlite3 and PostgreSQL DBs, but of course a DB-indinpendet solution would be better. -
Should I reverse order a queryset before slicing the first N records, or count it to slice the last N records?
Let's say I want to get the last 50 records of a query that returns around 10k records, in a table with 1M records. I could do (at the computational cost of ordering): data = MyModel.objects.filter(criteria=something).order_by('-pk')[:50] I could also do (at the cost of 2 database hits): # assume I don't care about new records being added between # the two queries being executed index = MyModel.objects.filter(criteria=something).count() data = MyModel.objects.filter(criteria=something)[index-50:] Which is better for just an ordinary relational database with no indexing on the criteria (eg postgres in my case; no columnar storage or anything fancy)? Most importantly, why? Does the answer change if the table or queryset is significantly bigger (eg 100k records from a 10M row table)? -
Why is Django putting a '.' on its Date field?
Why is Django putting a '.' on its Date field's month? For example Jan. Feb.? All of Python's format strings don't have a '.' on their months. How do you remove this from Django automatically on it's settings? -
Cant Import Model in the views
I am following this tutorial to learn django. I wanted to list the product in the view, here is my /product/views.py file from django.shortcuts import render def product_list(request): products = "Motorola"#Product.objects.order_by('published_date') return render(request, "product_list.html", {'products':products}) without model import it works well. but, when I try to import the models in view file(/product/views.py) like from .models import Product it gives me below error. django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet. I have looked for the similar questions but got no help from that. can anyone guide me what I am doing wrong? -
Apache , uwsgi , django lookup time
I have my setup hosted in AWS EC2, on am ubuntu machine, running a django server with uwsgi and apache. I've been trying to figure out for a while why the dev env VS local env have such different performance. With local server i return my index.html page in 80ms and in dev it takes almost 1s. I have django-debug-toolbar implemented and the CPU time is 300ms but chrome says the loading time is 1.3s (Waiting (TTFB)). Other big difference is that when i open then page with the URL it takes 1s but if I enter the server's IP it loads in 300ms. I already tried everything and can't figure why the loading difference. My apache virtual host: <VirtualHost *:80> <Location /> Options FollowSymLinks Indexes SetHandler uwsgi-handler uWSGISocket 127.0.0.1:3031 </Location> </VirtualHost> uWsgi conf: [uwsgi] socket = 127.0.0.1:3031 chdir = /home/ubuntu/production/<mysite> processes = 4 threads = 2 wsgi-file=<mysite/project>/wsgi.py virtualenv=/home/ubuntu/production venv = /home/ubuntu/production buffer-size=32768 -
How do I make my own ajax table grid
So I know there are many of these around, I use datatables as my JS plugin, and the editor plugin costs money, and I want to use my own styles for editing. So I have a simple bootstrap table with the first column being an edit button <tr> <td><button type="button" data-toggle="modal" data-target="#edit-row-modal" class="btn btn-default">Edit</button></td> <td>some data</td> <td>some data</td> </tr> Clicking on an edit button opens a bootstrap modal with a form in it. And also call a JS function to fill the form with the <tr> values: $("#table-grid").on("click", ".btn", function() { var tr = $(this).closest("tr"); // here I need to insert the row data into the form in the modal }); The problem is I'm somewhat stuck on how to make this "plugin" generic. So I can use this in multiple table grids. I need it all to work with ajax, i.e saving the form should update the specific row that get updated. One problem I have: One cell maps to a checkbox field in the form, though in the table I display it like: <td><i class=" glyphicon glyphicon-ok"><i></td> For 'not checked' I use: <td><i class=" glyphicon glyphicon-remove"><i></td> So I can't use input.value, I need to somehow update these after … -
Using djagno--restframework-filters how to call multiple parameters?
Models.py from django.contrib.auth.models import User from django.db import models class Wbcis(models.Model): Fruit = models.CharField(max_length=50) District = models.CharField(max_length=50) Taluka = models.CharField(max_length=50) def __str__(self): return self.Fruit def save(self, *args, **kwargs): super().save(*args, **kwargs) class Meta: verbose_name_plural = 'wbcis' views.py from rest_framework.viewsets import ModelViewSet from rest_framework import filters from WBCIS.serializers import WbcisSerializer from WBCIS.models import Wbcis class WbcisViewSet(ModelViewSet): serializer_class = WbcisSerializer def get_queryset(self): queryset = Wbcis.objects.all() fruit = self.request.query_params.get("fruit") if fruit is not None: queryset=queryset.filter(Fruit=fruit) taluka = self.request.query_params.get("taluka") if taluka is not None: queryset = queryset.filter(Taluka=taluka) district = self.request.query_params.get("district") if district is not None: queryset = queryset.filter(District=district) return queryset So, when I run server http://127.0.0.1:8000/WBCIS/wbcis/?district=Pune then I will get data for district pune htp://?taluka=Haveli then I get data for taluka haveli htp://?fruit=Banana then I get data for fruit banana. up to here all is working. Now, I want data for pune district for fruit banana. So I use http://127.0.0.1:8000/WBCIS/wbcis/?district=Pune&?fruit=Banana then it's give me data only for district pune,but in district pune there are so many fruits data, but I need only for fruit which is banana, so, here django show data only for first parameter(district=pune) Any idea how to get data for ht://?district=Pune&?fruit=Banana? Also, if I want data for fruit--> Guava in District--> Pune … -
django many to many related name not working
I have this relation model where user can follow each other: class Relation(models.Model): user = AutoOneToOneField(User) follows = models.ManyToManyField(User, related_name='followers') pub_date = models.DateTimeField(default=timezone.now) def __unicode__(self): return unicode(self.user) What I want to get is the Follower list of a user by the related_name of the Relation model. However I am getting an error saying: AttributeError: 'Relation' object has no attribute 'followers' Suppose these are the users: >>> one = CustomUser.objects.get(email="one@gmail.com") >>> two = CustomUser.objects.get(email="two@gmail.com") >>> three = CustomUser.objects.get(email="three@gmail.com") And to get the following list of users I can do this: >>> one.relation.follows.all() <QuerySet [<CustomUser: two@gmail.com>, <CustomUser: three@gmail.com>]> >>> two.relation.follows.all() <QuerySet [<CustomUser: three@gmail.com>]> >>> three.relation.follows.all() <QuerySet [<CustomUser: one@gmail.com>, <CustomUser: two@gmail.com>]> Now, Three should have two followers (i.e., One and Two). But when I query to get the followers of Three I get an error: >>> three.relation.followers.all() Traceback (most recent call last): File "<console>", line 1, in <module> AttributeError: 'Relation' object has no attribute 'followers' What am I missing here? Could you please help me? Thank you. -
Where to define a QuerySet in Django
I want to define a queryset. In the shell it is all fine and I can filter the column I want with: pat1 = Patient.objects.get(pk=1) pat1.examinationgeometry_set.filter(examination='FIRST') now I want to define a QuerySet out of it but I don't know where to define it and how. In the Views, Templates, Models? And how do I write it? I know that I have to define it with a function but is there any function from django for it? Idea behind this queryset is to show all of the results in my database from the first examination. So in my template I have sth like this: {% if Patient.examinationgeometry_set.filter(examination='FIRST') %} {% for geo in patient.examinationgeometry_set.all %} <li> x: {{ geo.x }}<br/> c: {{ geo.c }} <br/> b: {{ geo.b}}<br/> n: {{ geo.n}}<br/> </li> {% endfor %} {% endif %} I am thankful for every hint! -
Is there a function for generating settings.SECRET_KEY in django?
I wrote an ansible-role for openwisp2 to ease its deployment, it's a series of django apps. To ease the deployment as much as possible, I wrote a simple (probably trivial) SECRET_KEY generator script which is called by ansible to generate the secret key the first time the ansible playbook is run. Now, that works fine BUT I think it defeats the built-in security measures Django has in generating a strong key which is also very hard to guess. At the time I looked at other ways of doing it but didn't find much, now I wonder: is there a function for generating settings.SECRET_KEY in django? That would avoid this kind of home baked solutions that even though they work they are not effective when it comes to security. -
Add CSS stylesheet to HTML template
It's the first time I'm using CSS and I would like to apply my css stylesheet to my HTML template (inside my Django Project). So I've an HTML template which looks like to : <html> <head> {% load staticfiles %} <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link rel="stylesheet" type="text/css" href="{% static 'css/BC_raw.css' %}"/> </head> <body> <h2 align="center"> ACTE DE NAISSANCE </align> </h2> {% block content %} <h3 align = "right"> Acte de Naissance n° {{birthcertificate.id}} </align> </h3> <h3> Informations concernant l'enfant : </h3> <li> Nom de l'enfant : {{birthcertificate.lastname}}</li> <li>Prénom(s) de l'enfant : {{birthcertificate.firstname}}</li> <li>Est né(e) à : {{birthcertificate.birthcity}}</li> <li>Le : {{birthcertificate.birthday}} à {{birthcertificate.birthhour}}</li> <li>De sexe : {{birthcertificate.sex}}</li> <hr width = "50%"></hr> <h3> Informations concernant le père : </h3> <li>Fils ou fille du nommé : {{birthcertificate.fk_parent1.lastname}} {{birthcertificate.fk_parent1.firstname}}</li> <!-- Get only firstname and lastname element --> <li>Né le : {{birthcertificate.fk_parent1.birthday}}</li> <!-- Get only birthday element --> <li>A : {{birthcertificate.fk_parent1.birthcity}}</li> <!-- Get only birthcity element --> <li>De nationalité : {{birthcertificate.fk_parent1.nationality}}</li> <!-- Get only nationality element --> <li>Domicilié au : {{birthcertificate.fk_parent1.adress}}</li> <li>A : {{birthcertificate.fk_parent1.city}} ({{birthcertificate.fk_parent1.zip}}) - {{birthcertificate.fk_parent1.country}}</li> <!-- Get only adress, zip, city, country element --> <li>Exerçant la profession de : {{birthcertificate.fk_parent1.job}}</li> <!-- Get only job element --> <hr width = "50%"></hr> <h3> Informations … -
Set div id to value from dictionary
I'm creating table rows as I iterate over a dict from the server, but unable to set the id I want. <table> {% for key, values in products.products.items %} <tr> <td><div onClick="hello(this.id)" id={{values.pk}}>{{values}}</div><td> </tr> {% endfor %} </table> The value of the div is being displayed fine. But the id is empty. -
Why django added full path in database when we are uploading image
I am uploading image using django after save image in database path goes like this '/var/www/html/project/media/uploads/image_name.jpg' instead of 'uploads/image_name.jpg' My media settings are: BASE_DIR_NEW = os.path.join('/', *_dirs[0:-2]) MEDIA_URL = '/media/' STATIC_URL = '/static/' MEDIA_ROOT = os.path.join(BASE_DIR_NEW, 'media') In model : profile_image = models.ImageField( _("Profile Image"), upload_to='uploads/profile_img/', max_length=254, blank=True, null=True ) -
cannot pass json in django context
I have some code like this. def foobar(): foo = [" 1", " 2", " 3"] context['json'] = json.dumps(foo) print context['json'] return render_to_string('template', context) I am getting what I think is correct printed to the terminal... [" 1", " 2", " 3"] but then in my javascript console I get the error (index):59 Uncaught SyntaxError: Unexpected token ; and when I go to where it is I see this... [\u0022 1\u0022, \u0022 2\u0022, \u0022 3\u0022] so I can see it is turning to unicode somewhere and then not being converted back but IDK what to do about it. In my template (in <script> tags) I am doing this... var data = { labels: {{ labels|escapejs }} datasets: [] } -
Django url reverse error
I have a url configuration of this sort urlpatterns = [ url(r'webhook/', include('foward_bot.telegram_API.urls', namespace='api_webhook'), name='webhook'), ] In telegram_API.urls I have urlpatterns = [ url(r'^(?P<token>[-_:a-zA-Z0-9]+)/$', TelegramView.as_view(), name='api_webhook'), ] When I try to access this url with reverse in this manner webhook = reverse('webhook', args={instance.token}) I get the error: `Reverse for 'webhook' with arguments '(u'297704876:AAHiEy-slaktdaSMJfZtcnoDC-4HQYYDNOs',)' and keyword arguments '{}' not found. 0 pattern(s) tried: []` I have tried different variations like webhook = reverse('webhook', kwargs={'token': instance.token}), webhook = reverse('webhook:token', kwargs={'token': instance.token}), But I always similar NoReverseMatch errors -
Django - TypeError: int() argument must be a string or a number, not 'dict'
I'm trying to save a multiple Heat instances and store it to a list heats then used the list to a bulk_create as data. @transaction.atomic def create(self, request): heats = [] for item in request.data: animal = Animal(item['animal']) heat = Heat(item['heat']) heat.animal = animal heats.append(heat) Heat.objects.bulk_create(heats) But i got the error: TypeError: int() argument must be a string or a number, not 'dict' Here is the full traceback: Traceback (most recent call last): File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\core\handlers\exception.py", line 39, in inner response = get_response(request) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response response = self.process_exception_by_middleware(e, request) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view return view_func(*args, **kwargs) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\rest_framework\viewsets.py", line 83, in view return self.dispatch(request, *args, **kwargs) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\rest_framework\views.py", line 477, in dispatch response = self.handle_exception(exc) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\rest_framework\views.py", line 437, in handle_exception self.raise_uncaught_exception(exc) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\rest_framework\views.py", line 474, in dispatch response = handler(request, *args, **kwargs) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\utils\decorators.py", line 185, in inner return func(*args, **kwargs) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\farm_management\heat\views.py", line 188, in create Heat.objects.bulk_create(heats) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\db\models\manager.py", line 85, in manager_method return getattr(self.get_queryset(), name)(*args, **kwargs) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\db\models\query.py", line 449, in bulk_create self._batched_insert(objs_with_pk, fields, batch_size) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\db\models\query.py", line 1068, in _batched_insert self._insert(item, fields=fields, using=self.db) File "C:\Users\Web\Desktop\PyDev\Envs\djangular\lib\site-packages\django\db\models\query.py", line 1045, in _insert return … -
Django class based view pagination
Hi i want to paginating queryset(lectures). and i tried. but it doesn'work how can i do? class tag_detail(View): def get(self, request, pk): lectures_data = LectureModel.objects.filter(tags__id=pk).order_by('-id') paginator = Paginator(lectures_data, 2) page = request.GET.get('page') try: lectures = paginator.page(page) except PageNotAnInteger: lectures = paginator.page(1) except EmptyPage: lectures = paginator.page(paginator.num_pages) return render(request, 'web/html/tag/tag_detail.html',{ 'lectures':lectures }) -
Upgrade project from Django version 1.8 to Django version 1.10
I have been developing Django/python application using django version 1,8 and works well but i upgraded to django 1.10 and i am getting this error " AttributeError: 'Options' object has no attribute 'get_field_by_name' " after running python manage.py runserver. any idea!? -
Performance boost by storing Django static files and templates in tmpfs
Would storing templates and static files in tmpfs reduce response latency for a Django webapp running on a server, assuming there is enough more than enough memory to serve content? -
How to give admin ability to change media path using frontend in django?
I want to give admin ability to change media path in django whatever he want in server system. can anybody tell me how can i do this? below is code in Settings.py MEDIA_URL = '/media/' MEDIA_ROOT = location('public/media') I want to change Media_Root directory . But only admin can change media directory