Django community: RSS
This page, updated regularly, aggregates Django Q&A from the Django community.
-
How to integrate Angular 6 and Danjgo
I am using Angular 6 for front end and Django back end. First I build angular project using CLI command ng build which will create a dist file inside Angular 6 project. After that I created a static file inside Django structure and pasted that dist file contents. Displaying Forntend structure with the help of django template. My question is, whether I am following correct procedure or not. -
Outputting a different structure in Django REST Framework
Let's say I have the following model: class Attribute(models.Model): name = models.CharField(max_length=50) code = models.CharField(max_length=50) value = models.IntegerField(default=0) How could I get a Django REST Framework serializer to render it as the following? { attribute { 'name' : 'shoes', 'code': 'sho', 'group': { 'name' : 'shoes', 'code': 'sho' } }, value': 1 } -
Django admin manage ForeignKey in Add/Edit form
In my django app i have this model: class temp_test_keywords(models.Model): id = models.AutoField(primary_key=True) main_id = models.ForeignKey(temp_main, on_delete=models.CASCADE, related_name='tm_ttk') test_id = models.ForeignKey(temp_case, on_delete=models.CASCADE, related_name='tc_ttk') key_id = models.ForeignKey(temp_keywords, on_delete=models.CASCADE, related_name='tk_ttk') key_val = models.CharField(max_length=200, null=True, blank=True, verbose_name='Value') key_group = models.CharField(max_length=200, null=True, blank=True, verbose_name='Group') #Fields for API permissions owner = models.ForeignKey('auth.User', related_name='ttestkey_owner', on_delete=models.CASCADE, verbose_name="API Owner") In admin.py i create the relative ModelAdmin for manage list, add and edit options: class temp_test_keywordsAdmin(admin.ModelAdmin): list_filter = ('main_id__descr', 'test_id__descr') list_display = ('get_main_id', 'get_test_id', 'key_id', 'key_val', 'key_group') def get_main_id(self, obj): return obj.main_id.descr def get_test_id(self, obj): return obj.test_id.descr get_main_id.short_description = 'Main Template' get_main_id.admin_order_field = 'main_id__descr' get_test_id.short_description = 'Test Case' get_test_id.admin_order_field = 'test_id__descr' Now when i view m data in changelist_view template all was done, but when i try to add or edit a record in my test_id field i see the object (test_case_obj(1)) and not the descr field. How can i manage ForeignKey for point to a specific field also in add/edit admin form? So many thanks in advance -
Sqlalchemy json column - how to perform filter based on list of ids on a key
I have a list of ids like: tracker_ids = [69, 70] I need to get all the APInformation objects based on the tracker_id. data looks like this: { 'tracker_id' : 69, 'cpu_core_avg': 89.890', 'is_threshold': true,'datetime':1539053379040 } { 'tracker_id' : 70, 'cpu_core_avg': 65.0', 'is_threshold': false, 'datetime':1539053379040 } { 'tracker_id' : 69, 'cpu_core_avg': 34.9', 'is_threshold': false,'datetime':1539053379040 } I tried the following but it raises an error. session.query(APInformation).filter(APInformation.data['tracker_id'].in_(tracker_ids), APInformation.data['datetime'].astext.cast(BigInteger) > 1539053379040).all() -
Django: cannot save data from Form in DB
I've been following a tutorial, but making small changes, doesn't allow me to save the form in DB. However, I know the model is correct because I can save objects from within shell. I'm not getting any error, after submitting I'm redirected to the home page. But if I submit form and then go to the admin, I see the registered model, but with no records on in (except the ones saved through shell). Howcome? models.py: class TamaniosCantidades(models.Model): TAMANIOS = (('498', '2" x 2"',), ('499', '3" x 3"',), ('500', '4" x 4"',), ('501', '5" x 5"',)) CANTIDADES = (('100', '50',), ('100', '100',), ('150', '150',)) tamanios = models.CharField(max_length=10, choices=TAMANIOS) cantidades = models.CharField(max_length=10, choices=CANTIDADES) forms.py: from django import forms from .models import TamaniosCantidades class TamaniosCantidadesForm(forms.ModelForm): class Meta: model = TamaniosCantidades fields = ['tamanios', 'cantidades'] urls.py: from . import views from django.urls import path, include urlpatterns = [ path('', views.index), path('productos/', views.productos), path('post_url/', views.post_treasure, name='post_treasure'), path('post_url_tamanioscantidades/', views.post_tamanioscantidades, name='post_tamanioscantidades'), ] views.py: def index(request): treasures = Treasure.objects.all() form = TreasureForm() tamanioscantidades_form = TamaniosCantidadesForm() return render(request, 'main_app/index.html', {'treasures': treasures, 'form': form, 'tamanioscantidades_form': tamanioscantidades_form}) html: <div class="row"> <form action="post_url_tamanioscantidades/" method="post"> {% csrf_token %} {{ tamanioscantidades_form.as_p }} <input type="submit" value="Submit"/> </form> </div> -
Can Django Cache Handle Any Variable Type
I am hoping to use Django's Caching functionality, more specifically Memcached to store Zip objects which are created by joining two lists. For example: # Two lists names = ['peter', 'paul', 'mary'] jobs = ['teacher, 'music man', 'chef'] # Combine them to form Zip object combined = zip(names, jobs) # Can be accessed like so: for name, job in combined: print(name, job) I am asking, because I currently am trying to cache the zip object but it does not seem to be working. -
How to convert django template to html template
I would like to know if there is any library/module that can convert my django templates into a regular html file. Let's say i have this django template: index.html {% extends "base.html" %} <p> My name is {{ name }}. Welcome to my site </p> And i want to convert it to something like this: index.html < the content of base.html here > <p> My name is John Doe. Welcome to my site </p> Is there any simple utility to do that ? -
adding a static site (set of html, css, js files) to django admin
I need to add user manual to my django site, and it is a completely different set of (html, css & js) files and i need them to display only for authorized users (logged in users) first I put all of the files in template folder and write a view and a url as bellow view.py def UserManual(request): return render(request, 'usermanual/index.html') urls.py urlpatterns = [ url(r'^admin/user_manual/$',staff_member_required(UserManual), name='admin_user_manual'), but it did not identify the css and js files which was in a sub folder. then i add a path to a nginx conf file # user manual static location /qa/admin/user_manual { alias /usr/.../templates/usermanual; } the the site was displayed as i wanted but it can access to anyone without logging in. what would be the best way to do this without changing user_manual's content? -
How to find max occurence of a record's field in multiple rows?
The problem I am facing right now is related to finding frequency of a record's field in multiple rows. For example, let's assume, we have 6 records. Zone Camera Ip ---- ------ -- Zone 1 Cam 1 192.16..... Zone 2 Cam 2 192.16..... Zone 4 Cam 3 192.16..... Zone 2 Cam 4 192.16..... Zone 2 Cam 5 192.16..... Zone 3 Cam 6 192.16..... Here we want to find Max frequency of Zone or what is the most frequent Zone among the records? (Normally, we can see the Zone 2 happened for 3 times which is the most frequent zone) I used aggregate function to find the Max but it doesn't work that way as it only returns the max pk of the field. Failed example that I tried: qs.aggregate(most_frequent_zone=Max('Zone'),...) Is there any built-in function to handle this scenario? any help is greatly appreciated. -
How do I prevent Python / Django from saving variables in memory between page refreshes?
Does Python / Django save variables between page refresh? Here is my code: class Item(models.Model): id = models.AutoField(primary_key=True) _images_set = [] @csrf_exempt def get_products(request): item = Item() item._images_set.append('Yay') return HttpResponse(json_utilities.get_success()) Every time I refresh the page, and stop the debugging to inspect item, the _images_set grows by 1. I come from the PHP world, where every time a page reloads all the variables are reset. Could someone explain what is going on here? I don't want _images_set to just keep growing and growing, how do I tell python / django to reset all variables at each new page load? -
How to update unittests when migrating from sqlite to postgres (django)
I have a somewhat large django project with many views/models. I recently migrated my project from sqlite3 to postgres locally, and plan to scale it out even further (put postgres on a separate machine, etc). I followed these instructions when I migrated from sqlite to postgres, and it appears to have worked perfectly. (ie, my running app looks identical to when the db was sqlite) My issue is this: When I run my previously-written unittests, the first unittest works and all proceeding unittests fail. Individually, the unittests work fine. I've seen some other posts on stackoverflow that addressed this issue, but the solutions were so unclear. How can I rework my setUp()/ teardown() methods for my unittests so that they will pass with my newly migrated postgres db? Do i need to completely rewrite all unittests? I've seen the pytest-postgresql library, although I'm not entirely sure how to modify my unittests based off of this. My testing suite is set up with different classes that test views. So for example, class View1Tests(TestCase): def setUp(self): c1 = Category.objects.create(id=55555, leaf_node_name="Test Category 1") c2 = Category.objects.create(id=12345, leaf_node_name="Test Category 2") s1 = Search.objects.create(category=c1, username="testuser") s2 = Search.objects.create(category=c2, username="testuser2") def test_view1_success(self): #blablabla def test_view1_fail(self): #blablabla β¦ -
my django app dosnet work after i close ssh
I launched this command via SSH on a Ubuntu server: gunicorn loghmeh.wsgi:application --workers=3 and everything was fine, After a while I quit my terminal. but when i go to my site i saw 502 error , Bad Getway, it seems my process has been killed. after that i connect to server with ssh and witout running any command site was ok . and process wasnt killed. anyone has idea whats the problem? i already tried tmux , screen , nohup nut i had the same result with them. -
How do i implement my sluig in my url instead of primary key in Django?
new bee here, I have created a get_absolute_url in my model that takes {'pk': self.pk} as kwargs. and the urls.py is url(r'^(?P\d+)/$' this I have a slug in my model. The question is how can i inject the slug instead of this primary key its generating. what would be the urls code and the get_absolute_url for slug in my model? Thanks in advance -
How to see graphene-django DEBUG logs
I'm having trouble viewing DEBUG level logs with Graphene and Django. I've set the following in settings.py: LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'handlers': { 'console': { 'class': 'logging.StreamHandler', }, }, 'loggers': { 'django': { 'handlers': ['console'], 'level': 'DEBUG' }, 'django.request': { 'handlers': ['console'], 'level': 'DEBUG' }, }, } However, when I try to look at the logs of my Django server all I see is: β―β―β― kubectl logs -f server-6b65f48895-bmp6w server Operations to perform: Apply all migrations: admin, auth, contenttypes, django_celery_beat, django_celery_results, server, sessions, social_django Running migrations: No migrations to apply. Performing system checks... System check identified no issues (0 silenced). October 08, 2018 - 23:59:00 Django version 2.0.6, using settings 'backend.settings' Starting development server at http://0.0.0.0:8000/ Quit the server with CONTROL-C. "POST /graphql HTTP/1.1" 400 113 "POST /graphql HTTP/1.1" 400 113 "POST /graphql HTTP/1.1" 400 113 "POST /graphql HTTP/1.1" 400 113 "POST /graphql HTTP/1.1" 400 113 "POST /graphql HTTP/1.1" 400 113 "POST /graphql HTTP/1.1" 400 113 "POST /graphql HTTP/1.1" 400 113 "POST /graphql HTTP/1.1" 400 113 "POST /graphql HTTP/1.1" 400 113 How can I view DEBUG level logs to figure out why my server is constantly serving 400s? I have the Django DEBUG environment variable unset. I'm β¦ -
django optimizations cache_page vs template.loaders.cached.Loader
Testing some django optimizations and had some unexpected results between cache_page vs template cached.Loader. I expected cache_page would have been more performant since it's a direct cache lookup rather than going through the full templating layer. But I must be misunderstanding something fundamental because consistently the cached.Loader produces better results. My test case: 1) Loading a simple HTML page with several include templates. 2) Using django 1.10 3) redis as default cache 4) using ab to load test with n=1000, c=20 Enabled the cached.Loader with this in settings.py: 'loaders':[('django.template.loaders.cached.Loader', [ 'django.template.loaders.filesystem.Loader', 'django.template.loaders.app_directories.Loader', ]), ] Enabled the cache_page with this decorator: @cache_page(60 * 15) def home(request): return render(request, "index2.html",{"static_path":static('myapp/')}) I comment and turn on each caching strategy independently. Consistently it looks like: loaders.cached.Loader: ~90 requests/sec @cache_page : ~60 requests/sec Any reason why I'm seeing such drastic gains with cached.Loader ( about 30% ) over the cache_page strategy? -
How to limit querysets based on multiple select boxes in django admin actions?
Overview: Hi, I made an EmailCampaign model/app that allows me to make and send my own campaigns to anyone on my email list, instead of using something like mail chimp. Why? Because I wanted to see if I could, and it was fun. The method below works fine if I use the checkbox to select one campaign, but not 2 or more. If one, I can send_mass_mail() to everyone on my list. But if 2 or more, it will only send to one of the checked boxes. I've read the django admin pages, searched the web, and can't find the answer to my problem. So how can I make the following method limit the selected_campaign variable to only 1 checkbox??? admin.py: @admin.register(EmailCampaign) class EmailCampaignAdmin(admin.ModelAdmin): ... ... actions = ['send_email_campaign'] def send_email_campaign(self, request, queryset): confirmed_list = ConfirmedEmail.objects.all() selected_campaign = request.POST.get('_selected_action', None) # '_selected_action' is the name attribute in the html. Does not work with GET. email_campaign = EmailCampaign.objects.get(id=selected_campaign) # Take all emails in the list, make a separate datatuple for each one, # pass it into django's send_mass_mail(), and message the admin # that it was successful. What I want: For the method above to have something like the following. And β¦ -
display list does not appear in Django admin
I have tow models for cars and images. every thing is working fine except the display list does not appear in the admin. when I remove the line list_display. all fields appear. here is the admin file code: class Images_CarsAdmin(admin.ModelAdmin): list_display=['car_images'] class Meta: model=Images_Cars class Images_CarsInline(admin.TabularInline): model = Images_Cars class Cars_PostsAdmin(admin.ModelAdmin): inline = [ Images_CarsInline, ] list_display = [ 'post_title', 'post_detail', 'date_created', 'date_updated', 'year', 'odometer', 'car_make', 'car_model', ] class Meta: model= Cars_Posts I think I did silly mistake but I do not where -
Jinja2 Django 2.1 Templates are not loading as django templates
I am building a small storefront app with Django 2.1, and I am attempting to use Jinja2 as my template engine. settings.py: TEMPLATES = [ { 'BACKEND': 'django.template.backends.jinja2.Jinja2', 'DIRS': [os.path.join(BASE_DIR, 'templates/jinja2')], 'APP_DIRS': True, 'environment': 'lifenscents.jinja2.environment', 'OPTIONS': { 'environment': 'lifenscents.jinja2.environment', }, }, { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join('templates'), ], 'APP_DIRS': True, 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ] Directory structure . βββ db.sqlite3 βββ my_project β βββ __init__.py β βββ jinja2.py β βββ settings.py β βββ urls.py β βββ wsgi.py βββ manage.py βββ static β βββ css β βββ js βββ store β βββ admin.py β βββ apps.py β βββ __init__.py β βββ migrations β βββ models.py β βββ templates β βββ tests β βββ urls.py β βββ views βββ templates β βββ about.html β βββ base.html β βββ connect.html β βββ index.html βββ tests βββ __init__.py βββ test_site.py Inside the app store βββ admin.py βββ apps.py βββ __init__.py βββ migrations βββ models.py βββ templates β βββ hold.html β βββ jinja2 β βββ front.html βββ tests β βββ __init__.py β βββ test_models.py β βββ test_views.py βββ urls.py βββ views βββ __init__.py βββ products.py products.py from django.http import HttpResponse from django.views.generic.base import TemplateView from ..models import Candle class β¦ -
Caching Zip Object With Django Memcached
I am currently using Django and memcached to keep track of a Zip object like so: activeList_data = cache.get(active_list_key) if not activeList_data: activeTickers, activeName = get_most_active() activeList = zip(activeTickers, activeName) cache.set(1, activeList, cache_time) However, whenever I try to print the cache it just returns a blank list. Any ideas on where I am going wrong? Thanks EDIT: For reference here is my full code activeList_data = cache.get(active_list_key) # returns None if no key-value pair if not activeList_data: activeTickers, activeName = get_most_active() print(activeTickers) if not activeTickers: # If blank list returned (weekends) then use # previous data stored in models tickerList = [] nameList = [] most_active = MostActiveStock.objects.all() for ticker in most_active.ticker: tickerList.append(ticker) for name in most_active.name: nameList.append(name) activeList = zip(activeTickers, activeName) print(1) else: activeTickers = activeTickers[:3] activeName = activeName[:3] activeList = zip(activeTickers, activeName) print(list(activeList)) # Delete all old data MostActiveStock.objects.all().delete() # Store latest data for IEX's weekend blackouts for ticker, name in activeList: most_active_obj = MostActiveStock.objects.create(ticker=ticker, name=name) print(2) cache.set(active_list_key, activeList, cache_time) print(3) else: activeList = activeList_data print(4) print(list(activeList)) context_dict['activeList'] = activeList -
Correct way to address ETA of my Apply.async task?
So a little bit of a background. I am trying to get my task to run at a time an order should be delivered. Say the order is next week, I want the task to run two days (48 hours) before the delivery date. Unless the order is due within 48 hour. I am trying to figure out if this is an okay way to write it. I start off with creating an eta of the delivery date: eta = timezone.make_aware(datetime.datetime.combine(buyer_invoice.purchase_order.deliver_date,time(hour = 22)),timezone.get_current_timezone()) eta = eta + timedelta(days=1) Create a reconcile time for when the order is placed. reconcile_time = timezone.make_aware(datetime.datetime.combine(buyer_invoice.purchase_order.delivery_date,time(hour=RECONCILE_AT_HRS)),timezone.get_current_timezone()) And set a now point. now = timezone.now() To make up for any orders in the past: if now > reconcile_time: reconcile_time = now + timedelta(minutes=5) if now > eta: eta= now + timedelta(minutes= 5) This is what I have working so far. I have now added this next part to make the as needed adjustments. if eta < timedelta(hours = 48) do_reconcile = reconcile_time else: do_reconcile = eta - timedelta(days = 2) Along with my tasks: task_id_reconcile= uuid() buyer_invoice.auto_reconcile_task = auto_reconcilation.apply_async(args=[buyer_invoice,task_id_reconcile], eta=do_reconcile + timedelta(minutes = 1),task_id = task_id_reconcile) buyer_invoice.inventory_reconcile_task = inventory_reconcile.apply_async(args=[buyer_invoice,task_id_inv_reconcile], eta= do_reconcile, task_id = task_id_inv_reconcile) As this β¦ -
Django - How to port scripts to django
Before I ask my question I need to give some context: I wrote a simple python script that read linux's syslog file and search for certain strings. I have other similar scripts like these (scripts that do file system stuff, scripts that interact with other servers and so on). Most of these scripts write simple write stuff to stdout. I would like to port these scripts to a web-server so I could simple browser to https://server/syslog and get the same output that I would get by running the script on the command line interface. According with my research Django seems to be a great choice. I followed some Django tutorials and I was capable of developing some basic django web apps. My question is: Since django does not have a "controller" where should I place the scripts code? My best bet in the view, but according with djangos documentation it does not make sence. Extracted from django doc: In our interpretation of MVC, the βviewβ describes the data that gets presented to the user. Itβs not necessarily how the data looks, but which data is presented. The view describes which data you see, not how you see it. Itβs a β¦ -
Pytest django generic relation old object appeared
I have a model: class Store(models.Model): product = GenericRelation('Product') And celery task that creates a product for store: def my_celery_task(store_obj_pk): store_obj = Store.objects.get(pk=store_obj_pk) Product.objects.create( content_type=ContentType.objects.get_for_model(store_obj), object_id=store_obj.pk, ) And I want to test this task with pytest class TestStoreTask: def test_store_one(self): self.__test_store(store_product={ 'name': 'one' }) def test_store_two(self): self.__test_store(store_product={ 'name': 'two' }) def __test_store(self, store_product): store_obj = Store.objects.create() my_celery_task(store_obj_pk=store_obj.pk) print(store_obj.product.all()) So I expect one object for each test. But when I run both tests in test_store_two I have two objects. And this objects have id 2 and 3. So my output for test_store_one: <QuerySet [<Product: Product object (1)>]> And for test_store_two: <QuerySet [<Product: Product object (2)>, <Product: Product object (3)>]> I don't understand why id was changed and why object appeared in new test case. -
calling django url through javascript with parameters
This is my javascript function where I pass parameters, using alert I checked that function is getting the parameters I want but I am unable to pass them in django url other wise giving a string the url works but not with parameters. function myFunction(a) { var v = a.value; alert(v); location.href="{% url 'new_event' v %}"; //does not works location.href="{% url 'new_event' 'string' %}"; //works } I have checked the value getting is string the way I want but how to pass it? -
How to combine two nested tuples and use in Django's CharField choices?
I have two nested tuples similar to the ones below which will be used in choices in Django's model.CharField(). COMPANIES = ( ('USA', ( ('gm', 'General Motors'), ('tesla', 'Tesla'), ('ford', 'Ford') ) ), ('South Korea', ( ('kia', 'Kia Motors'), ('hyundai', 'Hyundai Motors'), ) ), ('Japan', ( ('nissan', 'Nissan Motors'), ('honda', 'Honda Motors'), ('toyota', 'Toyota Motors'), ) ), ) MANAGERS = ( ('USA', ( ('jack', 'Jack Smith'), ('doyun', 'Doyun Kim'), ('jill', 'Jill Maggie'), ('akari', 'Akari Tanaka'), ) ), ('South Korea', ( ('doyun', 'Doyun Kim'), ('siu', 'Siu Park'), ('jill', 'Jill Maggie'), ) ), ('Japan', ( ('akari', 'Akari Tanaka'), ('jack', 'Jack Smith'), ('haruto', 'Haruto Nakamura'), ) ), ) Currently my working my working fields looks like this: companies = models.CharField(max_length=30, choices=COMPANIES) managers = models.CharField(max_length=50, choices=MANAGERS) But for the fidelity of the data, I don't want to repeat the country names twice and possibly have data like this (or something similar): COMPANY_INFO = ( ('USA', ( ('gm', 'General Motors'), ('tesla', 'Tesla'), ('ford', 'Ford') ), ( ('jack', 'Jack Smith'), ('doyun', 'Doyun Kim'), ('jill', 'Jill Maggie'), ('akari', 'Akari Tanaka'), ) ), ('South Korea', ( ('kia', 'Kia Motors'), ('hyundai', 'Hyundai Motors'), ), ( ('doyun', 'Doyun Kim'), ('siu', 'Siu Park'), ('jill', 'Jill Maggie'), ) ), ('Japan', ( ('nissan', β¦ -
Django Channels Execution Order
I need some help understanding how Django Channels is executing the code. I'm probably missing some understanding of sync vs async as it's not doing what I would expect. In summary I wanted a consumer that would do the following: Connect On message from client in a while loop: execute a long running function (1 sec) call group_send broadcasting results This wasn't working, so I tried to create a very simple consumer to understand what's happening. I create a WebsocketConsumer as I think I want it to run syncronously, i.e. I want each loop to execute and then broadcast the results, then execute again. When I trigger the receive method I see the self.send happen instantly, but both the "2. First Group Call" and the "3. Second Group Call" happen after the long running function. I can see from the timestamps in the console.log that the chat_message() is only being executed for the "2. First Group Call" after the long running function, even though it appears before the long running function in the recieve method. The results in one of the browser console is: received "1. Self send 21:45:22.060500" received "2. 21:45:22.060500 first group call chat start: 21:45:24.529500" received "3. β¦